C Programming Complete Tutorial (Extended)
Introduction to C Programming
C is a general-purpose programming language developed in the early 1970s by Dennis Ritchie at
Bell Labs. It has influenced many other languages and is widely used for system and application
development.
Basic Structure of a C Program
Example:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
Output:
Hello, World!
Variables and Data Types
Example:
#include <stdio.h>
int main() {
int age = 25;
float salary = 55000.50;
char grade = 'A';
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
return 0;
Output:
Age: 25
Salary: 55000.50
Grade: A
Conditional Statements
Example:
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Positive number");
} else {
printf("Non-positive number");
return 0;
Output:
Positive number
Loops in C
Example (for loop):
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
return 0;
Output:
4
5
Functions in C
Example:
#include <stdio.h>
void greet() {
printf("Welcome to C Programming!");
int main() {
greet();
return 0;
Output:
Welcome to C Programming!
Arrays in C
Example:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d\n", arr[i]);
return 0;
Output:
Pointers in C
Pointers are variables that store the memory address of another variable.
Example:
#include <stdio.h>
int main() {
int x = 10;
int *p = &x;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", &x);
printf("Pointer p holds address: %p\n", p);
printf("Value pointed by p: %d\n", *p);
return 0;
Output:
Value of x: 10
Address of x: 0x... (memory address)
Pointer p holds address: 0x... (same as above)
Value pointed by p: 10
Structures in C
Structures are used to group different types of variables under one name.
Example:
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
struct Student s1 = {1, "John", 85.5};
printf("ID: %d\n", s1.id);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
return 0;
Output:
ID: 1
Name: John
Marks: 85.50
File Handling in C
C allows you to create, open, read, write, and close files.
Example (writing to a file):
#include <stdio.h>
int main() {
FILE *fptr = fopen("sample.txt", "w");
if (fptr == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(fptr, "Hello File!");
fclose(fptr);
return 0;
This program creates 'sample.txt' and writes 'Hello File!' to it.
Example (reading from a file):
#include <stdio.h>
int main() {
char ch;
FILE *fptr = fopen("sample.txt", "r");
if (fptr == NULL) {
printf("File not found!\n");
return 1;
while ((ch = fgetc(fptr)) != EOF) {
putchar(ch);
fclose(fptr);
return 0;
Output:
Hello File!
Dynamic Memory Allocation
C provides malloc(), calloc(), realloc(), and free() for dynamic memory management.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
ptr = (int*) malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
return 1;
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
Output:
12345