C Programming Revision
C Programming Revision
1. Data Types in C
C has several data types:
- int: Stores integers (4 bytes, e.g., 10, -5).
- char: Stores a single character (1 byte, e.g., 'A').
- float: Stores decimal numbers (4 bytes, e.g., 3.14).
- double: Stores large decimal numbers (8 bytes).
- Derived types: Arrays, Pointers, Structures, Unions.
2. Logical Operators
Logical operators are used to combine conditions:
- && (AND): True if both conditions are true.
- || (OR): True if at least one condition is true.
- ! (NOT): Reverses the condition (true -> false).
Example:
if (a > 0 && b > 0) {
printf("Both are positive");
}
3. Arithmetic Operators
Used for mathematical calculations:
- + (Addition)
- - (Subtraction)
- * (Multiplication)
- / (Division)
- % (Modulus, remainder)
Example:
int a = 10, b = 5;
printf("Sum: %d", a + b); // Output: 15
4. Control Statements
Used to control program flow.
- if-else:
if (x > 0) {
printf("Positive");
} else {
printf("Not positive");
}
- switch-case:
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid");
}
- Loops:
for (int i = 0; i < 5; i++) { printf("%d", i); }
while (i < 5) { printf("%d", i); i++; }
do { printf("%d", i); i++; } while (i < 5);