Hello everyone, today we will talk about the basics of the C language. As an old yet powerful programming language, C is widely used in operating systems, embedded development, game engines, and more. Even in today’s era of various high-level languages, learning C remains an essential skill for programmers. So, how can we quickly get started with C? Let’s begin with the most basic code and gradually guide you into the world of C.
First, open your compiler (such as Code::Blocks, VSCode, or the gcc command line tool), create a new C file named <span>hello.c</span>
, and then enter the following code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
This code is the classic “Hello, World!” program, which is the first piece of code that everyone learning programming will encounter. Let’s analyze its structure line by line:
<span>#include <stdio.h></span>
is a header file that includes the function declarations of the C standard input-output library, such as <span>printf</span>
.<span>int main()</span>
is the entry point of the program; all C programs start executing from the <span>main</span>
function.<span>printf("Hello, World!\n");</span>
is the output function responsible for printing “Hello, World!” to the screen, where <span>\n</span>
represents a newline.<span>return 0;</span>
indicates that the program ends normally, with a return value of <span>0</span>
usually indicating success.
Hello, World!
Variables and Data Types
Next, let’s learn how to declare variables and manipulate data. C is a strongly typed language, meaning each variable must have its type specified before use. Here is a simple example:
#include <stdio.h>
int main() {
int age = 25; // Integer type
float height = 1.75; // Float type
char grade = 'A'; // Char type
printf("Age: %d\n", age);
printf("Height: %.2f meters\n", height);
printf("Grade: %c\n", grade);
return 0;
}
After running, you will see:
Age: 25
Height: 1.75 meters
Grade: A
Here, <span>%d</span>
, <span>%.2f</span>
, and <span>%c</span>
are format specifiers used for integer, float, and char respectively.
Conditional Statements and Logical Judgments
In programming, it is often necessary to execute different code based on different conditions. C provides <span>if</span>
, <span>else if</span>
, and <span>else</span>
statements for conditional judgment. Let’s look at a small example:
#include <stdio.h>
int main() {
int score;
printf("Please enter your score:");
scanf("%d", &score); // Get score from user input
if (score >= 90) {
printf("Excellent\n");
} else if (score >= 75) {
printf("Good\n");
} else if (score >= 60) {
printf("Pass\n");
} else {
printf("Fail\n");
}
return 0;
}
In this program, we use the <span>scanf</span>
function to get the user-input score and output the corresponding evaluation based on the score range. After running, if you input 80, the program will output:
Please enter your score: 80
Good
Loop Statements
In addition to conditional judgments, loops are also an important concept in programming. C has three main loop structures: <span>for</span>
, <span>while</span>
, and <span>do while</span>
. Here is an example using a <span>for</span>
loop to print numbers from 1 to 10:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
After running, the output will be:
1 2 3 4 5 6 7 8 9 10
The <span>for</span>
loop structure consists of three parts:
-
Initialization: <span>int i = 1</span>
means starting from 1. -
Condition check: <span>i <= 10</span>
means continue looping while<span>i</span>
is less than or equal to 10. -
Execution after each loop: <span>i++</span>
means incrementing<span>i</span>
by 1.