notes
notes
1. Basic Syntax
• Variables: Declare variables using types like int, float, char, double, etc.
c
CopyEdit
int x = 10;
float y = 5.5;
• Comments:
c
CopyEdit
// Single line comment
/* Multi-line
comment */
• Print/Scan Functions:
c
CopyEdit
printf("Hello, World!\n"); // Printing to console
scanf("%d", &x); // Reading input
2. Data Types:
3. Control Structures:
• if-else:
c
CopyEdit
if (x > 10) {
printf("Greater than 10\n");
} else {
printf("Less or equal to 10\n");
}
• switch-case:
c
CopyEdit
switch (x) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Default case\n");
}
• Loops:
o for loop:
c
CopyEdit
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
o while loop:
c
CopyEdit
while (x < 10) {
printf("%d ", x);
x++;
}
4. Functions:
• Defining a function:
c
CopyEdit
int add(int a, int b) {
return a + b;
}
• Function call:
c
CopyEdit
int result = add(5, 3);
5. Arrays:
• Declaration:
c
CopyEdit
int arr[5] = {1, 2, 3, 4, 5};
• Accessing:
c
CopyEdit
printf("%d", arr[2]); // Outputs: 3
6. Pointers:
• Declaration & Usage:
c
CopyEdit
int x = 10;
int *ptr = &x;
printf("%d", *ptr); // Outputs: 10
c
CopyEdit
int arr[] = {1, 2, 3};
int *p = arr;
printf("%d", *(p + 1)); // Outputs: 2
7. Structures:
• Definition:
c
CopyEdit
struct Person {
char name[50];
int age;
};
8. Memory Management:
c
CopyEdit
int *ptr = (int *)malloc(sizeof(int) * 5);
free(ptr);
9. File Handling:
c
CopyEdit
FILE *file = fopen("file.txt", "r");
char ch = fgetc(file);
fclose(file);