Complete C Programming Tutorial for Beginners (2025)
Welcome! In this tutorial, we will learn C programming from scratch, step by step. Whether you're aiming to learn C for systems programming, competitive coding, or simply strengthening your basics, this tutorial will guide you through the essentials.
1. Introduction to C
C is a general-purpose programming language developed in the early 1970s by Dennis Ritchie. It is known for its performance, portability, and close-to-hardware capabilities. It's widely used for operating systems, embedded systems, and as a foundation for other languages like C++, Java, and Python.
2. Structure of a C Program
A typical C program includes headers, the main function, and some statements. Let’s look at a basic structure:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Explanation:
#include <stdio.h>includes the standard I/O library.main()is the entry point of every C program.printf()prints text to the screen.return 0;ends the program.
3. Variables and Data Types
Variables store data, and data types define what type of data you are working with:
int age = 25;
float height = 5.9;
char grade = 'A';
Common Data Types:
int– Integer numbersfloat– Decimal numberschar– Single charactersdouble– Higher-precision float
4. Operators
Operators perform operations on variables and values:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, <, >, <=, >=
- Logical: &&, ||, !
- Assignment: =, +=, -=, *=, /=
5. Control Statements
Control statements help us control the flow of the program.
If-Else
int age = 18;
if (age >= 18) {
printf("You are eligible to vote.");
} else {
printf("You are not eligible to vote.");
}
Switch
int day = 2;
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Other day");
}
6. Loops
Loops repeat a block of code multiple times.
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++;
}
7. Functions
Functions help organize code into reusable blocks.
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("Sum is: %d", result);
return 0;
}
8. Arrays
Arrays store multiple values of the same data type.
int numbers[5] = {10, 20, 30, 40, 50};
printf("%d", numbers[2]); // Outputs 30
9. Strings
Strings in C are arrays of characters.
char name[] = "Alice";
printf("Hello, %s!", name);
10. Pointers
Pointers store memory addresses and allow powerful low-level access.
int x = 10;
int *p = &x;
printf("Value: %d\\n", *p);
printf("Address: %p", p);
11. Structures
Structures group different types of data into a single unit.
struct Person {
char name[50];
int age;
};
struct Person p1 = {"John", 30};
printf("%s is %d years old.", p1.name, p1.age);
12. File Handling
You can read/write files in C using file pointers.
FILE *fp = fopen("file.txt", "w");
fprintf(fp, "Hello, file!");
fclose(fp);
13. Tips & Best Practices
- Use meaningful variable and function names.
- Always initialize variables before use.
- Comment your code for readability.
- Test and debug your code regularly.
- Use functions to avoid repetition.
