C Language Notes
C Language Notes
1. Introduction
C is a general-purpose, procedural programming language developed by Dennis Ritchie in the early
1970s. It is powerful for system programming, embedded systems, and applications needing
performance.
2. Program Structure
Every C program has one or more functions. Execution starts from main().
Basic template:
#include <stdio.h>
int main() {
// code
return 0;
}
4. Operators
Arithmetic: +, -, *, /, %.
Relational: ==, !=, >, <, >=, <=.
Logical: &&, ||, !.
Bitwise: &, |, ^, ~, <<, >>.
Assignment and Increment/Decrement operators.
5. Control Flow
Decision: if, if-else, switch.
Loops: for, while, do-while.
Jump: break, continue, goto.
6. Functions
Declaration and definition:
return_type func_name(parameter_list) {
// code
}
Parameters passed by value. Use pointers for pass-by-reference.
Prototype declarations are recommended.
8. Pointers
Stores memory addresses. Declaration: type *ptr;
Use & to get address, * to dereference.
Pointers and arrays: name of array is pointer to first element.
Dynamic memory: malloc(), calloc(), realloc(), free().
11. Preprocessor
#include: include headers.
#define: macros.
#ifdef, #ifndef, #endif for conditional compilation.
Use #pragma once to avoid multiple inclusions.
#include <stdio.h>
#include <stdlib.h>
int main() {
int x = 5, y = 10;
printf("Before swap: x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("After swap: x=%d, y=%d\n", x, y);
return 0;
}