In this world, almost every programmer’s first piece of code is Hello World.
The reason is that Dennis Ritchie, the author of the C language, introduced it in his classic work, which has been passed down as a classic for future generations, and other languages have followed suit in homage.
1. Components of a C Language Program
-
Mobile phones have many functions: “power on”, “power off”, “make a call”, “send a text”, “take a photo”, etc.
-
Each function in a mobile phone corresponds to a program segment (function) in a C language program.
-
Among many functions, one will always be executed first; multiple functions cannot be executed simultaneously.
-
To use a mobile phone, you must first execute the power on function.
Thus, a C language program is also composed of many functions and program segments, and among many C language segments, one will always be executed first. This first executed segment is called the “main function”.
-
A C language program consists of multiple “functions”, each with its own functionality.
-
A program has exactly one main function.
-
If a program does not have a main function, it cannot run.
-
When the program runs, the system automatically calls the main function, while other functions need to be manually called by the developer.
-
The main function has a fixed format and structure.
2. Function Definition Format
-
The format of the main function definition:
-
int indicates that the function will return an integer value after execution.
-
main indicates that the name of this function is main.
-
() indicates that this is a function.
-
{} indicates the scope of this program segment.
-
return 0; indicates that the function returns the integer 0 after execution.
int main() { // insert code here... return 0; }
-
The format of other function definitions:
-
int indicates that the function will return an integer value after execution.
-
call indicates that the name of this function is call.
-
() indicates that this is a function.
-
{} indicates the scope of this program segment.
-
return 0; indicates that the function returns the integer 0 after execution.
3. How to Execute Defined Functions
-
The main function (main) will be automatically called by the system, but other functions will not. Therefore, to execute other functions, you must manually call them in the main function.
-
call indicates that we find something named call.
-
() indicates that the thing we are finding named call is a function.
-
; indicates that the function call statement has been completed.
-
Therefore, call(); indicates that we find the call function and execute it.
int main() { call(); return 0; }
-
How to Output Content to the Screen
-
Outputting content is a relatively complex operation, so the system has predefined a function specifically for outputting content called printf. We just need to execute the predefined printf function to output content to the screen.
-
Whenever we need to execute a function, we do so by using the function name + parentheses.
-
The following code means: when the program runs, the system will automatically execute the main function, and during this process, we manually executed the call function and the printf function.
-
From observing the code, we find two problems:
-
We did not tell the printf function what content we want to output to the screen.
-
We cannot find the implementation code for the printf function.
int call(){ return 0; }int main(){ call(); printf(); return 0; }
-
How to Tell the printf Function What to Output
-
Simply write the content to be output inside the parentheses after the printf function.
-
Note: The content written inside the parentheses must be enclosed in double quotes.
-
How to Find the Implementation Code for the printf Function
-
Since the printf function is implemented by the system, you must inform the system where to find the implementation code for the printf function before using it.
-
#include <stdio.h> is how we tell the system to look in the stdio file for the declaration and implementation of the printf function.
#include <stdio.h> int call(){ return 0; }int main(){ call(); printf("hello world\n"); return 0; }
4. How to Run the Written Program
-
Click the hammer to compile the “source code” into an “executable file”.
-
Find the compiled source code, open the terminal (CMD), and run the executable file.
-
Click the run button in the Qt development tool directly.
5. Notes on the Main Function and Other Variations
-
In C language, every complete statement must end with a semicolon.
int main(){ printf("hello world\n"); // If there is no semicolon, a compilation error will occur return 0; } int main(){ // If there is no semicolon, when multiple statements are merged into one line, the system does not know where one complete statement begins and ends printf("hello world\n"); return 0; }
-
In C language, except for comments and areas enclosed in double quotes, Chinese characters cannot appear.
int main(){ printf("hello world\n"); // If the semicolon is a Chinese semicolon, it will cause an error return 0; }
-
A C language program can only have one main function.
int main(){ return 0; } int main(){ // Compilation error, duplicate definition return 0; }
-
A C language program cannot exist without a main function.
int call(){ // Compilation error, because there is only the call function and no main function return 0; } int mian(){ // Compilation error, because the name of the main function is misspelled, which is equivalent to not having a main function return 0; }
-
The int before the main function can be omitted or replaced with void.
#include <stdio.h> main(){ // No error printf("hello world\n"); return 0; } #include <stdio.h> void main(){ // No error printf("hello world\n"); return 0; } #include <stdio.h> void main(){ // No error printf("hello world\n"); return 0; }
-
The return 0 in the main function can be omitted.
int main(){ // No error printf("hello world\n"); }
-
The reason why multiple variations do not cause errors:
-
C language was originally just a specification and standard (such as C89, C11, etc.).
-
The implementation of the standard requires support and implementation from major vendors.
-
However, during the implementation by major vendors, due to issues such as interests and understanding, the standards implemented differ and change.
-
Therefore, you will see that different formats are written in different books; some return int, some return void, and some even have no return value.
-
Therefore, just remember the most standard writing method, no zuo no die.
#include <stdio.h> int main(){ printf("hello world\n"); return 0; }
Syntax errors: The compiler will report errors directly.
Logical errors: There are no syntax errors, but the running result is incorrect.
6. C Language Program Exercises
-
Write a C language program that outputs the following content to the screen in at least two ways:
*** *** ********* ******* **** **
-
Ordinary youth implementation:
printf(" *** *** \n"); printf("*********\n"); printf(" *******\n"); printf(" ****\n"); printf(" **\n");
printf(" *** *** \n*********\n *******\n ****\n **\n");
-
Artistic youth implementation (pretentious, don’t understand it yet):
int i = 0; while (1) { if (i % 2 == 0) { printf(" *** *** \n"); printf("*********\n"); printf(" *******\n"); printf(" ****\n"); printf(" **\n"); }else { printf("\n"); printf(" ** ** \n"); printf(" *******\n"); printf(" *****\n"); printf(" **\n"); } sleep(1); i++; system("cls"); }