C_Language_Examples
C_Language_Examples
1. Introduction
C is a powerful and widely used programming language. This document provides various
example programs demonstrating different concepts in C programming.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
return 0;
}
4. If-Else Condition
This program checks if a number is even or odd using an if-else condition.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}
6. Functions in C
This program demonstrates the use of functions in C by calculating the square of a number.
#include <stdio.h>
int main() {
int num = 5;
printf("Square of %d is %d\n", num, square(num));
return 0;
}
7. Arrays in C
This program initializes an array and prints its elements.
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
return 0;
}
8. Pointers in C
This program demonstrates how to use pointers in C.
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
return 0;
}
9. Structure in C
This program demonstrates the use of structures in C.
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student student1;
strcpy(student1.name, "John Doe");
student1.age = 20;
return 0;
}
10. Conclusion
These examples demonstrate fundamental concepts of C programming. Understanding
these programs will help in building a strong foundation in C.