C Programming Complete Practical Guide
30-40 Programs · Each program on a separate page · Code, Output, Explanation
Generated for the user request
Table of Contents
1. Basic Input/Output - Print "Hello, World!"
2. Variables and Data Types - Simple Integer Addition
3. Operators - Arithmetic Operators
4. Conditional Statements - if-else (Even or Odd)
5. Conditional Statements - switch-case (Weekday)
6. Loops - for loop (1 to 10)
7. Loops - while loop (factorial of 5)
8. Loops - do-while loop (menu example)
9. Arrays - 1D Array (Sum of elements)
10. Arrays - 2D Array (Matrix addition)
11. Strings - String length and print
12. Strings - Palindrome check (string)
13. Functions - Function to add two numbers
14. Recursion - Recursive factorial
15. Pointers - Pointer basics
16. Pointers - Swap using pointers
17. Dynamic Memory - malloc and free (array)
18. Structures - Structure and access
19. Unions - Union example
20. Enums & Typedef - enum and typedef
21. File Handling - Write and read file
22. Command-line Arguments - argc and argv
23. String Functions - strcmp and strcat
24. Searching - Linear search in array
25. Sorting - Bubble sort (ascending)
26. Math & Stdlib - Using math.h (power)
27. Bitwise Operators - Bitwise operations
28. Preprocessor - Macros and constants
29. Advanced Pointers - Pointer to pointer
30. File I/O (binary) - Write/read binary file
Program 1: Print "Hello, World!"
Topic: Basic Input/Output
Code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Output:
Hello, World!
Explanation:
Prints Hello, World! to the console using printf.
Page 3
Program 2: Simple Integer Addition
Topic: Variables and Data Types
Code:
#include <stdio.h>
int main() {
int a = 5, b = 7;
int sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Output:
Sum = 12
Explanation:
Adds two integers and prints the result.
Page 4
Program 3: Arithmetic Operators
Topic: Operators
Code:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a+b = %d\n", a+b);
printf("a-b = %d\n", a-b);
printf("a*b = %d\n", a*b);
printf("a/b = %d\n", a/b);
printf("a%%b = %d\n", a%b);
return 0;
}
Output:
a+b = 13
a-b = 7
a*b = 30
a/b = 3
a%b = 1
Explanation:
Demonstrates basic arithmetic operators including modulus.
Page 5
Program 4: if-else (Even or Odd)
Topic: Conditional Statements
Code:
#include <stdio.h>
int main() {
int n = 9;
if (n % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
Output:
Odd
Explanation:
Checks whether a number is even or odd using if-else.
Page 6
Program 5: switch-case (Weekday)
Topic: Conditional Statements
Code:
#include <stdio.h>
int main() {
int day = 3;
switch(day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
default: printf("Other\n");
}
return 0;
}
Output:
Wednesday
Explanation:
Uses switch to map an integer to a weekday.
Page 7
Program 6: for loop (1 to 10)
Topic: Loops
Code:
#include <stdio.h>
int main() {
for(int i=1;i<=10;i++)
printf("%d ", i);
printf("\n");
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Explanation:
Prints numbers 1 through 10 using a for loop.
Page 8
Program 7: while loop (factorial of 5)
Topic: Loops
Code:
#include <stdio.h>
int main() {
int n = 5, i = 1;
long fact = 1;
while (i <= n) { fact *= i; i++; }
printf("5! = %ld\n", fact);
return 0;
}
Output:
5! = 120
Explanation:
Calculates factorial using a while loop.
Page 9
Program 8: do-while loop (menu example)
Topic: Loops
Code:
#include <stdio.h>
int main() {
int x=0;
do {
x++;
printf("%d ", x);
} while (x < 3);
printf("\n");
return 0;
}
Output:
1 2 3
Explanation:
Executes the loop body at least once using do-while.
Page 10
Program 9: 1D Array (Sum of elements)
Topic: Arrays
Code:
#include <stdio.h>
int main() {
int arr[] = {2,4,6,8,10};
int sum=0;
for(int i=0;i<5;i++) sum += arr[i];
printf("Sum = %d\n", sum);
return 0;
}
Output:
Sum = 30
Explanation:
Sums elements of a 1D array.
Page 11
Program 10: 2D Array (Matrix addition)
Topic: Arrays
Code:
#include <stdio.h>
int main() {
int a[2][2] = {{1,2},{3,4}};
int b[2][2] = {{5,6},{7,8}};
int c[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c[i][j] = a[i][j] + b[i][j];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++) printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
Output:
6 8
10 12
Explanation:
Adds two 2x2 matrices element-wise.
Page 12
Program 11: String length and print
Topic: Strings
Code:
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello";
printf("String: %s\nLength: %lu\n", s, strlen(s));
return 0;
}
Output:
String: Hello
Length: 5
Explanation:
Uses strlen from <string.h> to get string length.
Page 13
Program 12: Palindrome check (string)
Topic: Strings
Code:
#include <stdio.h>
#include <string.h>
int main(){
char s[] = "radar"; int i, j, flag=1;
for(i=0, j=strlen(s)-1; i<j; i++, j--) if(s[i]!=s[j]){flag=0; break;}
printf(flag?"Palindrome\n":"Not Palindrome\n");
return 0;
}
Output:
Palindrome
Explanation:
Checks if a string reads the same forward and backward.
Page 14
Program 13: Function to add two numbers
Topic: Functions
Code:
#include <stdio.h>
int add(int x,int y){return x+y;}
int main(){
int r = add(3,4);
printf("Result = %d\n", r);
return 0;
}
Output:
Result = 7
Explanation:
Defines and calls a simple function returning sum.
Page 15
Program 14: Recursive factorial
Topic: Recursion
Code:
#include <stdio.h>
int fact(int n){ if(n<=1) return 1; return n*fact(n-1);}
int main(){ printf("6! = %d\n", fact(6)); return 0; }
Output:
6! = 720
Explanation:
Computes factorial using recursion.
Page 16
Program 15: Pointer basics
Topic: Pointers
Code:
#include <stdio.h>
int main(){
int x = 10; int *p = &x;
printf("x = %d, *p = %d\n", x, *p);
return 0;
}
Output:
x = 10, *p = 10
Explanation:
Demonstrates declaring a pointer and dereferencing it.
Page 17
Program 16: Swap using pointers
Topic: Pointers
Code:
#include <stdio.h>
void swap(int *a,int *b){int t=*a;*a=*b;*b=t;}
int main(){int x=2,y=3; swap(&x,&y); printf("x=%d y=%d\n", x, y); return 0; }
Output:
x=3 y=2
Explanation:
Swaps two integers using pointer parameters.
Page 18
Program 17: malloc and free (array)
Topic: Dynamic Memory
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int n = 5;
int *a = (int*)malloc(n * sizeof(int));
for(int i=0;i<n;i++) a[i]=i+1;
for(int i=0;i<n;i++) printf("%d ", a[i]);
printf("\n");
free(a);
return 0;
}
Output:
1 2 3 4 5
Explanation:
Allocates array dynamically, prints and frees it.
Page 19
Program 18: Structure and access
Topic: Structures
Code:
#include <stdio.h>
struct Student{ char name[20]; int roll; };
int main(){ struct Student s = {"Avinash", 101}; printf("Name: %s\nRoll: %d\n", [Link],
[Link]); return 0; }
Output:
Name: Avinash
Roll: 101
Explanation:
Defines a structure and initializes it.
Page 20
Program 19: Union example
Topic: Unions
Code:
#include <stdio.h>
union Data{ int i; float f; char str[20]; };
int main(){ union Data d; d.i = 10; printf("i = %d\n", d.i); d.f = 2.5; printf("f = %g\n",
d.f); return 0; }
Output:
i = 10
f = 2.5
Explanation:
Shows that union members share memory; last assigned value is meaningful.
Page 21
Program 20: enum and typedef
Topic: Enums & Typedef
Code:
#include <stdio.h>
typedef enum {RED, GREEN, BLUE} Color;
int main(){ Color c = GREEN; printf("%d\n", c); return 0; }
Output:
Explanation:
Defines an enum type and prints its integer value.
Page 22
Program 21: Write and read file
Topic: File Handling
Code:
#include <stdio.h>
int main(){
FILE *f = fopen("[Link]","w");
fprintf(f, "Hello File\n"); fclose(f);
char buf[50]; f = fopen("[Link]","r"); fgets(buf,50,f); printf("%s", buf); fclose(f);
return 0;
}
Output:
Hello File
Explanation:
Writes to a file and reads the content back.
Page 23
Program 22: argc and argv
Topic: Command-line Arguments
Code:
#include <stdio.h>
int main(int argc, char *argv[]){
for(int i=0;i<argc;i++) printf("%s\n", argv[i]);
return 0;
}
Output:
(When run as: ./[Link] hello)\n./[Link]\nhello
Explanation:
Prints program name and arguments passed from the command line.
Page 24
Program 23: strcmp and strcat
Topic: String Functions
Code:
#include <stdio.h>
#include <string.h>
int main(){ char a[20] = "Hi"; char b[] = " there"; strcat(a,b); printf("%s\n", a);
printf("cmp=%d\n", strcmp("abc","abc")); return 0; }
Output:
Hi there
cmp=0
Explanation:
Concatenates strings and compares them.
Page 25
Program 24: Linear search in array
Topic: Searching
Code:
#include <stdio.h>
int main(){ int a[]={2,4,6,8,10}; int key=6; int pos=-1; for(int i=0;i<5;i++)
if(a[i]==key){pos=i; break;} printf("Position=%d\n", pos); return 0; }
Output:
Position=2
Explanation:
Finds the index of a key using linear search.
Page 26
Program 25: Bubble sort (ascending)
Topic: Sorting
Code:
#include <stdio.h>
int main(){ int a[]={5,1,4,2,8}; int n=5; for(int i=0;i<n-1;i++) for(int j=0;j<n-i-1;j++)
if(a[j]>a[j+1]){int t=a[j];a[j]=a[j+1];a[j+1]=t;} for(int i=0;i<n;i++) printf("%d ", a[i]);
printf("\n"); return 0; }
Output:
1 2 4 5 8
Explanation:
Sorts an array using bubble sort algorithm.
Page 27
Program 26: Using math.h (power)
Topic: Math & Stdlib
Code:
#include <stdio.h>
#include <math.h>
int main(){ double r = pow(2,3); printf("2^3 = %.0f\n", r); return 0; }
Output:
2^3 = 8
Explanation:
Uses pow from math.h to compute powers.
Page 28
Program 27: Bitwise operations
Topic: Bitwise Operators
Code:
#include <stdio.h>
int main(){ unsigned int x=5,y=3; printf("x&y=%u\n", x&y); printf("x|y=%u\n", x|y);
printf("x^y=%u\n", x^y); printf("~x=%u\n", ~x); return 0; }
Output:
x&y=1
x|y=7
x^y=6
~x=4294967290
Explanation:
Shows bitwise AND, OR, XOR and NOT (on unsigned int; NOT shows wraparound).
Page 29
Program 28: Macros and constants
Topic: Preprocessor
Code:
#include <stdio.h>
#define PI 3.14
#define SQR(x) ((x)*(x))
int main(){ printf("PI=%.2f\n", PI); printf("SQR(4)=%d\n", SQR(4)); return 0; }
Output:
PI=3.14
SQR(4)=16
Explanation:
Demonstrates macro constants and function-like macros.
Page 30
Program 29: Pointer to pointer
Topic: Advanced Pointers
Code:
#include <stdio.h>
int main(){ int x=5; int *p=&x; int **pp=&p; printf("x=%d, *p=%d, **pp=%d\n", x, *p, **pp);
return 0; }
Output:
x=5, *p=5, **pp=5
Explanation:
Shows multiple levels of indirection using pointer to pointer.
Page 31
Program 30: Write/read binary file
Topic: File I/O (binary)
Code:
#include <stdio.h>
int main(){ FILE *f = fopen("[Link]","wb"); int a[]={1,2,3,4}; fwrite(a, sizeof(int), 4,
f); fclose(f); int b[4]; f=fopen("[Link]","rb"); fread(b, sizeof(int), 4, f); for(int
i=0;i<4;i++) printf("%d ", b[i]); printf("\n"); fclose(f); return 0; }
Output:
1 2 3 4
Explanation:
Writes integers to a binary file and reads them back.
Page 32