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;
}
3. Data Types & Variables
Built-in types: char, int, float, double.
Modifiers: signed, unsigned, short, long.
Variables must be declared before use.
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.
7. Arrays & Strings
Array: fixed-size collection: type name[size];
Strings: char arrays terminated by '\0'.
Operations via loops or library functions (<string.h>).
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().
9. Structures & Unions
struct: group of related variables.
union: memory-shared members.
Use typedef for convenience.
10. File I/O
FILE *fp;
fopen(), fclose(), fprintf(), fscanf(), fread(), fwrite(), fseek(), ftell().
Use modes: "r", "w", "a", "rb", "wb".
11. Preprocessor
#include: include headers.
#define: macros.
#ifdef, #ifndef, #endif for conditional compilation.
Use #pragma once to avoid multiple inclusions.
12. Command-line Arguments
main(int argc, char *argv[])
argc: count of arguments, argv: array of argument strings.
13. Standard Library
Header files: <stdio.h>, <stdlib.h>, <string.h>, <math.h>, <ctype.h>, etc.
Common functions: printf(), scanf(), exit(), malloc(), free(), strlen(), strcpy().
14. Example Program
Swap two numbers using pointers:
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
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;
}