Derived Data Types
Arrays: A collection of variables of the same type.
Pointers: Variables that store the memory address of another variable.
Structures: Grouping different types of variables together.
Unions: Like structures but with shared memory for all members.
User-Defined Data Types
typedef: Allows creating a new name for an existing data type.
enum: Enumeration type, useful for defining named integer constants.
c
Copy code
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum of elements: %d\n", sum);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[6] = {3, 29, 15, 92, 48, 6};
int max = arr[0];
for (int i = 1; i < 6; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("Largest element: %d\n", max);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("Reversed array: ");
for (int i = 4; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
c
Copy code
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[5] = {64, 25, 12, 22, 11};
bubbleSort(arr, 5);
printf("Sorted array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[7] = {3, 5, 7, 3, 7, 5, 3};
int target = 3, count = 0;
for (int i = 0; i < 7; i++) {
if (arr[i] == target) {
count++;
}
}
printf("Frequency of %d: %d\n", target, count);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr1[3] = {1, 2, 3}, arr2[3] = {4, 5, 6}, merged[6];
for (int i = 0; i < 3; i++) {
merged[i] = arr1[i];
merged[i + 3] = arr2[i];
}
printf("Merged array: ");
for (int i = 0; i < 6; i++) {
printf("%d ", merged[i]);
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int isSorted = 1;
for (int i = 0; i < 4; i++) {
if (arr[i] > arr[i + 1]) {
isSorted = 0;
break;
}
}
if (isSorted) {
printf("The array is sorted.\n");
} else {
printf("The array is not sorted.\n");
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
int transpose[3][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
transpose[j][i] = matrix[i][j];
}
}
printf("Transposed matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[6] = {12, 35, 1, 10, 34, 1};
int first = -1, second = -1;
for (int i = 0; i < 6; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
printf("Second largest element: %d\n", second);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[6] = {2, 3, 5, 8, 10, 11};
int even = 0, odd = 0;
for (int i = 0; i < 6; i++) {
if (arr[i] % 2 == 0) {
even++;
} else {
odd++;
}
}
printf("Even count: %d, Odd count: %d\n", even, odd);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Address of a: %p\n", ptr);
printf("Value of a: %d\n", *ptr);
return 0;
}
c
Copy code
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 5, b = 10;
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
c
Copy code
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, *arr;
printf("Enter the number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("You entered: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("Array elements using pointer: ");
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
c
Copy code
#include <stdio.h>
void printString(char *str) {
while (*str != '\0') {
printf("%c", *str);
str++;
}
}
int main() {
char str[] = "Hello, World!";
printf("String: ");
printString(str);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 42;
int *p = &a;
int **pp = &p;
printf("Value of a: %d\n", **pp);
printf("Address of p: %p\n", pp);
return 0;
}
c
Copy code
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*funcPtr)(int, int) = add;
int result = funcPtr(5, 10);
printf("Result of addition: %d\n", result);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(ptr + i));
}
return 0;
}
c
Copy code
#include <stdio.h>
void modifyArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
modifyArray(arr, 5);
printf("Modified array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
c
Copy code
#include <stdio.h>
struct Point {
int x, y;
};
void updatePoint(struct Point *p, int newX, int newY) {
p->x = newX;
p->y = newY;
}
int main() {
struct Point p1 = {10, 20};
printf("Before update: x = %d, y = %d\n", p1.x, p1.y);
updatePoint(&p1, 30, 40);
printf("After update: x = %d, y = %d\n", p1.x, p1.y);
return 0;
}
c
Copy code
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p = {10, 20};
printf("Point: (%d, %d)\n", p.x, p.y);
return 0;
}
c
Copy code
#include <stdio.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student students[3] = {{1, 85.5}, {2, 90.0}, {3, 78.5}};
for (int i = 0; i < 3; i++) {
printf("Student %d: ID = %d, Marks = %.2f\n", i + 1, students[i].id, students[i].marks);
}
return 0;
}
c
Copy code
#include <stdio.h>
struct Address {
char city[30];
int zipCode;
};
struct Employee {
int id;
struct Address addr;
};
int main() {
struct Employee emp = {101, {"New York", 10001}};
printf("Employee ID: %d\n", emp.id);
printf("City: %s, Zip Code: %d\n", emp.addr.city, emp.addr.zipCode);
return 0;
}
c
Copy code
#include <stdio.h>
struct Rectangle {
int length;
int width;
};
int area(struct Rectangle r) {
return r.length * r.width;
}
int main() {
struct Rectangle rect = {5, 10};
printf("Area of Rectangle: %d\n", area(rect));
return 0;
}
c
Copy code
#include <stdio.h>
struct Circle {
float radius;
};
int main() {
struct Circle c = {5.5};
struct Circle *ptr = &c;
printf("Radius: %.2f\n", ptr->radius);
ptr->radius = 7.0;
printf("Updated Radius: %.2f\n", c.radius);
return 0;
}
c
Copy code
#include <stdio.h>
#include <stdlib.h>
struct Person {
char name[30];
int age;
};
int main() {
struct Person *p = (struct Person *)malloc(sizeof(struct Person));
if (p == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Enter name: ");
scanf("%s", p->name);
printf("Enter age: ");
scanf("%d", &p->age);
printf("Person: %s, Age: %d\n", p->name, p->age);
free(p);
return 0;
}
c
Copy code
#include <stdio.h>
struct Student {
char name[30];
float marks[3];
};
float calculateAverage(float marks[], int size) {
float sum = 0;
for (int i = 0; i < size; i++) {
sum += marks[i];
}
return sum / size;
}
int main() {
struct Student s = {"Alice", {85.5, 90.0, 78.5}};
printf("Student: %s, Average Marks: %.2f\n", s.name, calculateAverage(s.marks, 3));
return 0;
}
c
Copy code
#include <stdio.h>
struct Flags {
unsigned int isOn : 1;
unsigned int isReady : 1;
unsigned int isComplete : 1;
};
int main() {
struct Flags f = {1, 0, 1};
printf("Flags: isOn = %d, isReady = %d, isComplete = %d\n", f.isOn, f.isReady, f.isComplete);
return 0;
}
c
Copy code
#include <stdio.h>
struct Book {
char title[50];
float price;
};
int main() {
struct Book b1 = {"C Programming", 29.99};
struct Book b2 = b1; // Copy structure
printf("Book 1: %s, $%.2f\n", b1.title, b1.price);
printf("Book 2: %s, $%.2f\n", b2.title, b2.price);
return 0;
}
c
Copy code
#include <stdio.h>
struct Student {
char name[30];
int roll;
};
union Data {
int i;
float f;
};
int main() {
struct Student s = {"John", 101};
union Data d;
d.i = 10;
printf("Student: Name = %s, Roll = %d\n", s.name, s.roll);
printf("Union: Integer = %d\n", d.i);
d.f = 5.5;
printf("Union: Float = %.2f\n", d.f);
return 0;
}
c
Copy code
#include <stdio.h>
union Data {
int i;
float f;
char c;
};
int main() {
union Data d;
d.i = 10;
printf("Integer: %d\n", d.i);
d.f = 3.14;
printf("Float: %.2f\n", d.f);
d.c = 'A';
printf("Character: %c\n", d.c);
return 0;
}
c
Copy code
#include <stdio.h>
union Data {
int i;
char c[4];
};
int main() {
union Data d;
d.i = 0x12345678;
printf("Integer: %x\n", d.i);
printf("Bytes: %x %x %x %x\n", d.c[0], d.c[1], d.c[2], d.c[3]);
return 0;
}
c
Copy code
#include <stdio.h>
struct Rectangle {
int length;
int width;
};
union Shape {
struct Rectangle rect;
int radius;
};
int main() {
union Shape s;
s.rect.length = 10;
s.rect.width = 5;
printf("Rectangle - Length: %d, Width: %d\n", s.rect.length, s.rect.width);
s.radius = 7;
printf("Circle Radius: %d\n", s.radius);
return 0;
}
c
Copy code
#include <stdio.h>
union Converter {
int i;
float f;
};
int main() {
union Converter c;
c.i = 1065353216; // Binary representation of 1.0 in IEEE 754
printf("Integer: %d\n", c.i);
printf("Float: %.1f\n", c.f);
return 0;
}
c
Copy code
#include <stdio.h>
enum DataType { INT, FLOAT, CHAR };
union Data {
int i;
float f;
char c;
};
int main() {
union Data d;
enum DataType type = INT;
d.i = 42;
if (type == INT) {
printf("Integer: %d\n", d.i);
}
return 0;
}
c
Copy code
#include <stdio.h>
union Data {
int intData;
float floatData;
char str[20];
};
int main() {
union Data d;
d.intData = 100;
printf("Integer: %d\n", d.intData);
d.floatData = 98.76;
printf("Float: %.2f\n", d.floatData);
sprintf(d.str, "Hello");
printf("String: %s\n", d.str);
return 0;
}
c
Copy code
#include <stdio.h>
union Data {
int id;
double salary;
};
int main() {
union Data emp;
printf("Size of union: %lu bytes\n", sizeof(emp));
emp.id = 101;
printf("Employee ID: %d\n", emp.id);
emp.salary = 50000.75;
printf("Employee Salary: %.2f\n", emp.salary);
return 0;
}
c
Copy code
#include <stdio.h>
struct Employee {
char name[30];
union {
int id;
double salary;
} info;
};
int main() {
struct Employee e;
e.info.id = 101;
printf("Employee ID: %d\n", e.info.id);
e.info.salary = 50000.75;
printf("Employee Salary: %.2f\n", e.info.salary);
return 0;
}
c
Copy code
#include <stdio.h>
union MixedData {
int id;
float marks;
char grade;
};
int main() {
union MixedData data;
data.id = 101;
printf("ID: %d\n", data.id);
data.marks = 95.5;
printf("Marks: %.2f\n", data.marks);
data.grade = 'A';
printf("Grade: %c\n", data.grade);
return 0;
}
c
Copy code
#include <stdio.h>
union Data {
int i;
float f;
};
void printData(union Data d, char type) {
if (type == 'i') {
printf("Integer: %d\n", d.i);
} else if (type == 'f') {
printf("Float: %.2f\n", d.f);
}
}
int main() {
union Data d;
d.i = 42;
printData(d, 'i');
d.f = 3.14;
printData(d, 'f');
return 0;
}
User-Defined Data Types
typedef: Allows creating a new name for an existing data type.
enum: Enumeration type, useful for defining named integer constants.
c
Copy code
#include <stdio.h>
typedef int Integer;
int main() {
Integer a = 10, b = 20;
printf("Sum: %d\n", a + b);
return 0;
}
c
Copy code
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
int main() {
Point p = {10, 20};
printf("Point: (%d, %d)\n", p.x, p.y);
return 0;
}
c
Copy code
#include <stdio.h>
typedef int (*Operation)(int, int);
int add(int a, int b) {
return a + b;
}
int main() {
Operation op = add;
printf("Result: %d\n", op(5, 10));
return 0;
}
c
Copy code
#include <stdio.h>
typedef unsigned int UInt;
int main() {
UInt a = 3000000000;
printf("Unsigned Integer: %u\n", a);
return 0;
}
c
Copy code
#include <stdio.h>
typedef enum {
RED, GREEN, BLUE
} Color;
int main() {
Color c = GREEN;
if (c == GREEN) {
printf("Color is GREEN\n");
}
return 0;
}
c
Copy code
#include <stdio.h>
typedef unsigned long long ULL;
int main() {
ULL largeNumber = 18446744073709551615ULL;
printf("Large Number: %llu\n", largeNumber);
return 0;
}
c
Copy code
#include <stdio.h>
typedef int Array[5];
int main() {
Array arr = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
c
Copy code
#include <stdio.h>
typedef struct {
char name[30];
int age;
} Person;
typedef Person* PersonPtr;
int main() {
Person p = {"Alice", 25};
PersonPtr ptr = &p;
printf("Name: %s, Age: %d\n", ptr->name, ptr->age);
return 0;
}
c
Copy code
#include <stdio.h>
typedef enum { FALSE, TRUE } Bool;
int main() {
Bool isValid = TRUE;
if (isValid) {
printf("The value is TRUE\n");
}
return 0;
}
c
Copy code
#include <stdio.h>
typedef struct {
int roll;
char name[30];
} Student;
typedef struct {
Student students[3];
int count;
} Classroom;
int main() {
Classroom c = {{{1, "John"}, {2, "Alice"}, {3, "Bob"}}, 3};
for (int i = 0; i < c.count; i++) {
printf("Student %d: %s\n", c.students[i].roll, c.students[i].name);
}
return 0;
}
These programs illustrate the versatility and utility of typedef for creating more readable and maintainable code in C. Let me know if you'd like detailed explanations or variations of these examples!
c
Copy code
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
int main() {
enum Color c = RED;
printf("Color: %d\n", c); // Outputs 0 (RED)
return 0;
}
c
Copy code
#include <stdio.h>
enum Day { MON = 1, TUE, WED, THU, FRI, SAT, SUN };
int main() {
enum Day today = FRI;
printf("Today is: %d\n", today); // Outputs 5 (FRI)
return 0;
}
c
Copy code
#include <stdio.h>
enum Status { OK, WARNING, ERROR };
void checkStatus(enum Status s) {
switch (s) {
case OK:
printf("Status: OK\n");
break;
case WARNING:
printf("Status: WARNING\n");
break;
case ERROR:
printf("Status: ERROR\n");
break;
}
}
int main() {
enum Status s = WARNING;
checkStatus(s);
return 0;
}
c
Copy code
#include <stdio.h>
enum Permissions {
READ = 1,
WRITE = 2,
EXECUTE = 4
};
int main() {
int userPermission = READ | EXECUTE;
if (userPermission & READ) printf("User has read permission.\n");
if (userPermission & WRITE) printf("User has write permission.\n");
if (userPermission & EXECUTE) printf("User has execute permission.\n");
return 0;
}
c
Copy code
#include <stdio.h>
enum Fruit { APPLE, BANANA, CHERRY, ORANGE };
const char *fruitNames[] = {"Apple", "Banana", "Cherry", "Orange"};
int main() {
enum Fruit f = CHERRY;
printf("Fruit: %s\n", fruitNames[f]);
return 0;
}
c
Copy code
#include <stdio.h>
enum Operation { ADD, SUBTRACT, MULTIPLY, DIVIDE };
int calculate(enum Operation op, int a, int b) {
switch (op) {
case ADD: return a + b;
case SUBTRACT: return a - b;
case MULTIPLY: return a * b;
case DIVIDE: return b != 0 ? a / b : 0;
}
return 0;
}
int main() {
printf("Result: %d\n", calculate(ADD, 10, 5));
return 0;
}
c
Copy code
#include <stdio.h>
enum Gender { MALE, FEMALE, OTHER };
struct Person {
char name[30];
enum Gender gender;
};
int main() {
struct Person p = {"Alice", FEMALE};
printf("Name: %s, Gender: %d\n", p.name, p.gender);
return 0;
}
c
Copy code
#include <stdio.h>
enum ErrorCode { SUCCESS = 0, FILE_NOT_FOUND, OUT_OF_MEMORY, INVALID_INPUT };
void handleError(enum ErrorCode code) {
switch (code) {
case SUCCESS:
printf("Operation successful.\n");
break;
case FILE_NOT_FOUND:
printf("Error: File not found.\n");
break;
case OUT_OF_MEMORY:
printf("Error: Out of memory.\n");
break;
case INVALID_INPUT:
printf("Error: Invalid input.\n");
break;
}
}
int main() {
handleError(OUT_OF_MEMORY);
return 0;
}
c
Copy code
#include <stdio.h>
typedef enum { WINTER, SPRING, SUMMER, FALL } Season;
int main() {
Season currentSeason = SUMMER;
printf("Current season is: %d\n", currentSeason);
return 0;
}
c
Copy code
#include <stdio.h>
enum Weekday { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
int main() {
enum Weekday today = WEDNESDAY;
if (today == WEDNESDAY) {
printf("It's midweek!\n");
}
return 0;
}