C Language Notes
1. Introduction to C
C is like the 'assembly line' of programming languages, designed to give you control and
efficiency, perfect for building the operating systems and complex programs that many
higher-level languages depend on.
- Developed by Dennis Ritchie in 1972.
- General-purpose, procedural, and efficient.
- Offers low-level memory access and is widely used for system programming.
2. Structure of a C Program
A C program is like a blueprint, where each element has a specific role:
- Header files bring in tools and predefined functions, like a toolbox.
- The main function, `main()`, is the starting point for your program.
- Each line of code ends with a semicolon, just like each instruction in a manual needs a clear
ending.
3. Data Types
Think of data types as containers with specific shapes for holding different values:
- `int`: Integer numbers, similar to whole-number markers.
- `float`: Real numbers, perfect for fractions.
- `double`: Precision numbers for detailed measurements.
- `char`: Single characters, like letters in a word.
- Derived types like arrays and pointers are containers holding multiple items or addresses.
4. Variables and Constants
Variables are like labeled jars you store values in:
- Variables can change their contents.
- Constants (`const` or `#define`) are jars with seals—they can't be changed once filled.
5. Operators
Operators act like tools:
- Arithmetic operators (`+`, `-`, etc.) perform basic math.
- Logical operators (`&&`, `||`) evaluate true/false conditions.
- Assignment operators (`=`, `+=`) set or update values.
6. Input and Output
`scanf` is like asking for input from the user, and `printf` displays output:
- `scanf` stores user inputs into variables.
- `printf` prints output, letting users see the result.
7. Control Structures
Control structures guide the program flow:
- `if` statements are like traffic lights, deciding which path to take.
- `for`, `while`, `do...while` loops repeat actions until a condition is met.
- `switch` is like a sorting station, routing based on values.
8. Functions
Functions are mini-programs or 'helpers':
- Define them with `return_type function_name(parameters) { code }`
- They can be called anytime and help avoid repetitive code.
9. Arrays
Arrays are like shelves, holding multiple values of the same type in an organized manner:
- Single-dimensional arrays hold one row of values.
- Multi-dimensional arrays, like matrices, hold values in rows and columns.
10. Strings
Strings are arrays of characters that represent words or phrases, with a null `\0` character
at the end.
- C provides functions for managing strings: `strcpy`, `strcat`, `strlen`, `strcmp`.
11. Pointers
Pointers are like addresses on a map pointing to the location of a variable in memory:
- `*` dereferences the pointer, accessing the value at the address.
- `&` is used to find the address of a variable.
12. Structures
Structures are like custom boxes that hold different types of data in a single unit:
- Defined with `struct`, they combine different data types.
- You can access members with the `.` operator.
13. Unions
Unions are like shared lockers; they store one value at a time:
- Memory is shared by all members, meaning only one can be stored at a time.
14. File Handling
File handling is like opening a cabinet, reading from or writing to files.
- `fopen`, `fclose` manage file access.
- `fprintf`, `fscanf` are used to write to and read from files.
15. Dynamic Memory Allocation
Dynamic memory allocation allows for flexible memory management:
- `malloc`, `calloc` allocate memory.
- `realloc` adjusts allocated memory size.
- `free` deallocates memory.
16. Preprocessor Directives
Preprocessor directives are like prep work, setting things up before code runs:
- `#include` brings in libraries.
- `#define` creates constants or macros.
- Conditional directives like `#ifdef` control compilation.
17. Common Library Functions
Libraries contain useful functions for various tasks:
- Math functions (`pow`, `sqrt`).
- Character functions (`isalpha`, `isdigit`).
- String functions (`strcpy`, `strcat`).
18. Error Handling
Errors are categorized to simplify debugging:
- Syntax errors, like grammatical mistakes in code.
- Runtime errors happen during execution.
- Logical errors are flaws in the program’s logic.
- Debugging tools and techniques help fix these errors.