C Programming - Detailed Notes
1. Introduction to C:
- C is a general-purpose, structured programming language developed by Dennis Ritchie in 1972 at
Bell Labs.
- It is widely used for system software, operating systems, compilers, and embedded systems.
- Features: Simple, fast, portable, structured, supports low-level memory manipulation. 2. Structure
of a C Program:
A basic program in C:
#include
int main() {
printf("Hello, World!");
return 0;
}
- Every C program starts execution from main().
- Header files provide predefined functions.
3. Data Types in C:
1. Basic: int, float, char, double
2. Derived: array, pointer, structure, union
3. Enumeration: enum
4. Void: represents no value 4. Variables and Constants:
- Variable: A name that refers to a memory location (e.g., int x = 10;)
- Constants: Fixed values that do not change during execution (e.g., #define PI 3.14)
5. Operators in C:
1. Arithmetic (+, -, *, /, %)
2. Relational (==, !=, >, <, >=, <=)
3. Logical (&&, ||, !)
4. Assignment (=, +=, -=, *=, /=)
5. Increment/Decrement (++, --)
6. Bitwise (&, |, ^, <<, >>)
6. Control Statements:
- Conditional: if, if-else, nested if, switch
- Looping: for, while, do-while
- Jump: break, continue, goto, return 7. Functions:
- A function is a block of code that performs a specific task.
- Syntax: returnType functionName(parameters) { … }
- Types: Library functions (printf, scanf, etc.), User-defined functions.
- Call by value vs Call by reference.
8. Arrays:
- Collection of similar data types stored in contiguous memory.
- Syntax: int arr[5] = {1,2,3,4,5};
- Multidimensional arrays: int mat[2][2];
9. Strings:
- Sequence of characters ending with null ('\0').
- Example: char str[] = "Hello";
- Common functions: strlen(), strcpy(), strcat(), strcmp()
10. Pointers:
- A pointer is a variable that stores the address of another variable.
- Syntax: int *p; p = &x;
- Important for dynamic memory allocation and functions.
11. Structures and Unions:
- Structure: Collection of different data types under one name.
- Union: Similar to structure but shares the same memory location.
12. File Handling:
- Used to store data permanently.
- Functions: fopen(), fclose(), fprintf(), fscanf(), fgetc(), fputc()
13. Dynamic Memory Allocation:
- malloc(), calloc(), realloc(), free()
14. Preprocessor Directives:
- #include, #define, #ifdef, #ifndef, #endif
15. Advantages of C:
- Fast execution
- Portability
- Rich library
- Closer to hardware