Welcome to this comprehensive C programming tutorial! Whether you're a complete beginner or looking to deepen your understanding of C, this guide will take you through the fundamentals and introduce advanced concepts as you progress.
C is a powerful general-purpose programming language that is widely used in system programming, embedded systems, and applications requiring high performance. It is known for its efficiency, close-to-hardware control, and portability, making it a crucial language in the software industry.
To get started with C programming, you'll need to set up a development environment. Here are the steps:
gcc --version
in the terminal or command prompt.Now that your environment is set up, let’s start with the basics. In this section, we'll cover:
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char initial = 'A';
printf("Age: %d, Height: %.1f, Initial: %c\n", age, height, initial);
return 0;
}
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
for (int i = 0; i < 5; i++) {
printf("Count: %d\n", i);
}
return 0;
}
#include <stdio.h>
void greet(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
greet("Alice");
return 0;
}
Once you are familiar with the basics, it's time to explore more advanced features of C:
malloc
, calloc
, and free
.#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int *ptr = numbers;
for (int i = 0; i < 5; i++) {
printf("Number: %d, Address: %p\n", *(ptr + i), (ptr + i));
}
return 0;
}
#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!\n");
fclose(file);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int size = 5;
arr = (int*) malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
arr[i] = i + 1;
printf("Value: %d\n", arr[i]);
}
free(arr);
return 0;
}
Now that you are comfortable with intermediate topics, let’s move on to some advanced C programming concepts:
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
int main() {
struct Student s1 = {"Alice", 20, 85.5};
printf("Name: %s, Age: %d, Grade: %.2f\n", s1.name, s1.age, s1.grade);
return 0;
}
#include <stdio.h>
void add(int a, int b) {
printf("Sum: %d\n", a + b);
}
int main() {
void (*func_ptr)(int, int) = &add;
func_ptr(10, 20);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int size = 10;
arr = (int*) malloc(size * sizeof(int));
if (arr == NULL) {
printf("Memory not allocated.\n");
return 1;
}
for (int i = 0; i < size; i++) {
arr[i] = i * 2;
printf("Value: %d\n", arr[i]);
}
free(arr);
return 0;
}
Congratulations on making it through this C programming tutorial! You’ve covered everything from the basics of C to advanced topics like structures and memory management. Keep practicing and exploring the vast capabilities of C to enhance your programming skills.
Happy coding!