Welcome to the C programming tutorial. This guide covers the basic concepts of C programming, including writing a simple program, using variables, and handling user input.
To start, let’s write a simple "Hello, World!" program in C. Here’s how:
#include
int main() {
printf("Hello, World!\n");
return 0;
}
This code will display Hello, World! when compiled and run.
Variables are used to store data. Here's an example of using an integer variable:
#include
int main() {
int num = 5;
printf("The value of num is: %d\n", num);
return 0;
}
When you run this code, it will output:
The value of num is: 5You can also take input from the user using the scanf() function:
#include
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
return 0;
}
If the user enters 25, the output will be:
You are 25 years old.In C, you can use if statements to make decisions based on conditions:
#include
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
return 0;
}
If the user enters 20, the output will be:
You are an adult.You've now written basic C programs! With practice, you'll get better at using variables, functions, and control flow structures. Continue exploring C's features to become a proficient C programmer.