C Programming Notes
1. Introduction to C
- C is a general-purpose, procedural programming language.
- Developed by Dennis Ritchie in 1972 at Bell Labs.
- Used for system programming, embedded systems, and software development.
2. Basic Structure of a C Program
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
3. Data Types in C
- Basic Types: int, char, float, double
- Derived Types: array, pointer, structure, union
- Void Type: void
4. Variables and Constants
- Declaration: int a; float b; char c;
- Initialization: int a = 10;
- Constants: const int PI = 3.14;
5. Operators in C
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <, >=, <=
- Logical: &&, ||, !
- Bitwise: &, |, ^, ~, <<, >>
- Assignment: =, +=, -=, *=, /=, %=
6. Control Statements
if (condition) {
// Code
} else {
// Code
}
switch (expression) {
case value1:
// Code
break;
default:
// Code
}
7. Loops in C
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
8. Functions in C
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(5, 3);
printf("Sum: %d\n", sum);
return 0;
}
9. Arrays and Pointers
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[0]);
int a = 10;
int *ptr = &a;
printf("%d", *ptr);
10. File Handling
FILE *file = fopen("[Link]", "w");
fprintf(file, "Hello, File!");
fclose(file);
11. Dynamic Memory Allocation
int *ptr = (int*) malloc(5 * sizeof(int));
free(ptr);
12. Compilation and Execution
gcc program.c -o program
./program