C Programming: From Basics to Advanced
1. Introduction to C
- What is C?
- C is a general-purpose, procedural programming language developed in 1972 by Dennis
Ritchie.
- It is widely used for system programming, creating operating systems, and embedded software.
- Features of C:
- Low-level access to memory
- Simple set of keywords
- Clean style and structured language
2. Getting Started
- Setting Up the Environment:
- You need a C compiler (like GCC, clang, or Visual Studio Code with extensions).
- Write your first "Hello, World!" program.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
- Explanation:
- #include <stdio.h>: Preprocessor command that includes the standard input/output library.
- int main(): The main function where the program starts execution.
- printf(): Prints output to the console.
- return 0: Exits the program.
3. C Basics
- Variables and Data Types:
- C provides several data types:
- int: Integer
- float: Floating-point numbers
- char: Single characters
- double: Double-precision floating-point numbers
- Example:
int a = 10;
float b = 3.14;
char c = 'A';
- Operators:
- Arithmetic Operators: +, -, *, /, %
- Relational Operators: ==, !=, <, >, <=, >=
- Logical Operators: &&, ||, !
- Example:
int x = 5, y = 2;
int sum = x + y; // 7
4. Control Structures
- If-else:
if (a > b) {
printf("a is greater than b\n");
} else {
printf("b is greater than or equal to a\n");
- Switch Case:
switch (choice) {
case 1: printf("You chose 1\n"); break;
case 2: printf("You chose 2\n"); break;
default: printf("Invalid choice\n");
- Loops:
- For Loop:
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
- While Loop:
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
- Do-While Loop:
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
5. Functions
- Function Basics:
- A function is a block of code that performs a specific task.
- Syntax:
return_type function_name(parameters) {
// function body
- Example:
int add(int x, int y) {
return x + y;
}
int main() {
int result = add(5, 3);
printf("Sum: %d\n", result);
return 0;
- Pass by Value:
- Function parameters are passed by value, meaning changes inside the function do not affect
the original variables.
6. Arrays
- Arrays store multiple values of the same type.
- Example:
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
- Multidimensional Arrays:
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
7. Pointers
- What is a Pointer?
- A pointer is a variable that stores the address of another variable.
int a = 10;
int *p = &a; // p stores the address of a
- Dereferencing a Pointer:
printf("Value of a: %d\n", *p); // prints the value of a
- Pointer Arithmetic:
- Pointers can be incremented or decremented to point to the next or previous memory locations.
8. Structures
- What is a Structure?
- A structure is a collection of variables under one name.
- Example:
struct Person {
char name[50];
int age;
float height;
};
struct Person person1;
- Accessing Structure Members:
person1.age = 25;
printf("Age: %d\n", person1.age);
9. Dynamic Memory Allocation
- malloc, calloc, realloc, free:
- Allocate memory dynamically using functions from stdlib.h.
int *ptr = (int*)malloc(sizeof(int) * 5); // Allocate memory for 5 integers
free(ptr); // Free the allocated memory
10. File Handling
- Opening and Reading Files:
FILE *fptr = fopen("file.txt", "r");
if (fptr == NULL) {
printf("Error opening file\n");
} else {
char c;
while ((c = fgetc(fptr)) != EOF) {
printf("%c", c);
fclose(fptr);
11. Advanced Topics
- Recursion:
- A function that calls itself.
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
- Function Pointers:
- Pointers that point to functions.
void (*funcPtr)(int);
- Preprocessor Directives:
- #define, #include, and other macros.
- Command-Line Arguments:
int main(int argc, char *argv[]) {
printf("Program name: %s\n", argv[0]);
12. Best Practices
- Always free dynamically allocated memory.
- Follow proper code indentation and comment your code.
- Use header files to organize code in large projects.