C Programming Detailed Notes
1. History of C
- Developed by **Dennis Ritchie** in **1972** at **Bell Labs**.
- Based on **B Language** and influenced by **BCPL**.
- Used to develop the **UNIX operating system**.
- **ANSI C (C89)** was standardized in **1989**.
- Further versions: **C99, C11, C18**.
2. Importance of C
- **General-purpose language** used for system programming, embedded systems, etc.
- **Simple and fast** execution.
- **Portable** across different platforms.
- **Foundation for C++, Java, Python**.
- **Supports pointers, dynamic memory allocation, recursion**.
3. Character Set
- **Alphabets**: A-Z, a-z
- **Digits**: 0-9
- **Special Characters**: `+`, `-`, `*`, `/`, `%`, `&`, `|`, etc.
- **Whitespace characters**: Space, tab, newline.
4. Tokens in C
- **Keywords**: int, float, return, if, else, etc.
- **Identifiers**: Variable & function names.
- **Constants**: Fixed values (e.g., 100, 3.14, 'A').
- **Operators**: Symbols like `+`, `-`, `*`, `/`.
- **Special Symbols**: `{}`, `[]`, `()`, `;`, `,`.
- **Strings**: "Hello, World".
5. Constants in C
- **Integer Constants**: Whole numbers (e.g., 10, -5).
- **Floating-point Constants**: Decimal numbers (e.g., 3.14, -0.001).
- **Character Constants**: Single characters ('A', '5').
- **String Constants**: "Hello".
- **Symbolic Constants**: `#define PI 3.14`.
6. Variables in C
- A **named storage location** in memory.
- Must begin with a letter or underscore.
- Cannot be a keyword.
- Example:
```c
int age = 20;
float pi = 3.14;
char grade = 'A';
```
7. Operators in C
- **Arithmetic**: `+`, `-`, `*`, `/`, `%`.
- **Relational**: `==`, `!=`, `<`, `>`, `<=`, `>=`.
- **Logical**: `&&`, `||`, `!`.
- **Assignment**: `=`, `+=`, `-=`, `*=`.
- **Increment/Decrement**: `++`, `--`.
- **Bitwise**: `&`, `|`, `^`, `~`, `<<`, `>>`.
- **Conditional (Ternary)**: `condition ? value1 : value2`.
8. Operator Precedence
- Determines the order of execution.
- **Precedence Table**:
1. `()` (Parentheses)
2. `*`, `/`, `%` (Multiplication, Division, Modulus)
3. `+`, `-` (Addition, Subtraction)
4. `=` (Assignment)
9. Type Conversions
- **Implicit Type Conversion**: Automatically converts smaller data types to larger ones.
- **Explicit Type Conversion (Casting)**:
```c
float x = (float) 10 / 3;
```
10. Mathematical Functions
- **Available in `<math.h>` library**.
- **Common Functions**:
- `sqrt(x)`: Square root
- `pow(x, y)`: Power
- `fabs(x)`: Absolute value
- `ceil(x)`, `floor(x)`: Round up/down
11. Managing Input and Output
- **Formatted Input (`scanf`)**: Reads values.
```c
int num;
scanf("%d", &num);
```
- **Formatted Output (`printf`)**: Displays values.
```c
printf("Number: %d", num);
```
- **Reading and Writing 3-character Input**:
```c
char str[4];
scanf("%3s", str);
printf("%s", str);
```
12. Sample Programs
**Hello World Program**
```c
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
```
**Addition Program**
```c
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
```