Mastering the C programming language requires practice and real-world examples. This comprehensive guide from The Coding College offers a wide range of practical C code examples to help you understand key concepts, improve your skills, and apply them in real-world scenarios.
Why Practice with C Examples?
- Learn by Doing: Theory alone isn’t enough. Hands-on practice solidifies your understanding.
- Debugging Skills: Identifying and fixing issues in examples improves problem-solving skills.
- Real-World Relevance: Examples help bridge the gap between learning and practical application.
Table of Contents
- Hello, World!
- Input and Output Examples
- Decision-Making Examples
- Loop Examples
- Array and String Examples
- Function Examples
- File Handling Examples
1. Hello, World!
The simplest program to get you started in C.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
2. Input and Output Examples
Taking User Input
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
return 0;
}
Printing a Table
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("5 x %d = %d\n", i, 5 * i);
}
return 0;
}
3. Decision-Making Examples
Check if a Number is Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Find the Largest Number Among Three
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
printf("%d is the largest number.\n", a);
} else if (b >= a && b >= c) {
printf("%d is the largest number.\n", b);
} else {
printf("%d is the largest number.\n", c);
}
return 0;
}
4. Loop Examples
Print Numbers from 1 to 10
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
return 0;
}
Factorial of a Number
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d is %d.\n", num, factorial);
return 0;
}
5. Array and String Examples
Reverse an Array
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("Original array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\nReversed array: ");
for (int i = 4; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
Concatenate Two Strings
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
6. Function Examples
Find the Square of a Number
#include <stdio.h>
int square(int n) {
return n * n;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Square of %d is %d.\n", num, square(num));
return 0;
}
7. File Handling Examples
Writing to a File
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
fprintf(file, "Hello, File Handling in C!");
fclose(file);
printf("Data written to file.\n");
return 0;
}
Reading from a File
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
char line[100];
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
return 0;
}
Conclusion
These examples provide a strong foundation for understanding and applying key concepts in C programming. Regular practice with these snippets will enhance your coding skills and prepare you for real-world projects. At The Coding College, we’re committed to helping you master programming through practical examples and expert guidance.