1.
Introduction to C Programming
C is a powerful general-purpose programming language developed by Dennis Ritchie in 1972.
It is widely used for system programming, embedded systems, and application development.
C provides low-level access to memory and efficient performance.
2. Structure of a C Program
A C program typically includes header files, main() function, variable declarations, and statements.
Execution of every C program starts from the main() function.
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
3. Data Types in C
C supports basic data types such as int, float, char, and double.
Data types define the type of data a variable can store.
int age = 20;
float salary = 25000.50;
char grade = 'A';
4. Variables and Constants
Variables store data values.
Constants are fixed values that do not change during program execution.
#define PI 3.14
const int MAX = 100;
5. Input and Output Functions
C uses printf() for output and scanf() for input.
These functions are defined in stdio.h header file.
int num;
scanf("%d", &num);
printf("You entered: %d", num);
6. Operators in C
C provides arithmetic, relational, logical, assignment, and bitwise operators.
Operators are used to perform operations on variables and values.
int sum = a + b;
if(a > b) { printf("A is greater"); }
7. Decision Making (if-else)
Decision-making statements allow execution of code based on conditions.
Common statements: if, if-else, nested if.
if(age >= 18) {
printf("Adult");
} else {
printf("Minor");
}
8. Switch Statement
Switch is used when multiple conditions are based on a single variable.
switch(choice) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
default: printf("Invalid");
}
9. Loops in C
Loops execute a block of code repeatedly.
Types: for, while, do-while.
for(int i=0; i<5; i++) {
printf("%d", i);
}
10. Break and Continue
Break exits the loop.
Continue skips current iteration.
for(int i=0;i<5;i++){
if(i==3) break;
}
11. Arrays
Arrays store multiple values of the same data type.
Elements are accessed using index numbers.
int arr[5] = {1,2,3,4,5};
12. Strings
Strings are arrays of characters ending with null character '\0'.
char name[20] = "John";
13. Functions
Functions are blocks of code that perform specific tasks.
They help in modular programming.
int add(int a, int b){ return a+b; }
14. Recursion
Recursion is a function calling itself.
int fact(int n){
if(n==0) return 1;
return n*fact(n-1);
}
15. Pointers
Pointers store memory addresses.
They are powerful features in C.
int x = 10;
int *ptr = &x;
16. Pointer Arithmetic
Pointers can be incremented or decremented.
Useful in arrays and memory handling.
ptr++;
17. Structures
Structures group variables of different data types.
struct Student {
int roll;
char name[20];
};
18. Unions
Union allows storing different data types in same memory location.
union Data {
int i;
float f;
};
19. File Handling
C supports file operations using fopen(), fclose(), fprintf(), fscanf().
FILE *fp = fopen("[Link]", "w");
20. Dynamic Memory Allocation
Functions: malloc(), calloc(), realloc(), free().
int *ptr = (int*)malloc(5*sizeof(int));
21. Command Line Arguments
Arguments passed to main() function.
int main(int argc, char *argv[]) {}
22. Storage Classes
auto, static, extern, register define scope and lifetime.
static int count = 0;
23. Type Casting
Converting one data type to another.
float result = (float)a / b;
24. Preprocessor Directives
#include, #define, #ifdef are examples.
#define MAX 100
25. Error Handling
Handling runtime errors and debugging techniques.
Using return values and perror().
if(fp == NULL) {
perror("Error opening file");
}
26. Bitwise Operators
Operate at bit level: &, |, ^, ~, <<, >>.
int result = a & b;
27. Enumerations
enum defines named integer constants.
enum Day {SUN, MON, TUE};
28. Typedef
typedef creates alias for data types.
typedef unsigned int uint;
29. Best Practices
Write readable code, proper indentation, comments, meaningful names.
30. Conclusion
C programming forms the foundation for many modern languages.
Mastering C helps understand memory management and system-level programming.