#include
double r = sqrt(16);
double p = pow(2, 3);
int a = abs(-5);
Arrays
Array
int arr[5] = {1, 2, 3, 4, 5};
int arr2[] = {10, 20, 30};
int arr3[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Pointers
Pointer
int x = 10;
int *p = &x;
int **pp = &p;
printf("Value of x: %d\n", x);
printf("Value of p: %p\n", p);
printf("Value of pp: %p\n", pp);
Control Flow
Flow
if (x > 0) {
printf("Positive\n");
} else if (x < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
while (x < 10) {
printf("%d ", x++);
}
do {
printf("%d ", x--);
} while (x > 0);
Functions
Function
int add(int a, int b) {
return a + b;
}
void printMessage(char *msg) {
printf("%s\n", msg);
}
int main() {
int result = add(5, 3);
printMessage("Hello, World!");
return 0;
}
Structs
Struct
struct Point {
int x;
int y;
};
struct Student {
char name[50];
int age;
float gpa;
};
struct Point p1 = {1, 2};
struct Student s1 = {"John", 20, 3.5};
Unions
Union
union Data {
int i;
float f;
char str[20];
};
union Data d1;
d1.i = 10;
printf("Integer: %d\n", d1.i);
d1.f = 3.14;
printf("Float: %f\n", d1.f);
strcpy(d1.str, "Hello");
printf("String: %s\n", d1.str);
Enums
Enum
enum Color {
RED,
GREEN,
BLUE
};
enum Color c1 = RED;
printf("Color: %d\n", c1);