Detailed explanation of C simple program

foreword

This article will be based on the following simple code, sparrow anatomy, hoping to help readers truly understand the C language program.

#include <stdio.h>
int main(void)			/*一个简单C程序*/
{
    
    
	int num;			//定义一个名为num的变量
	num = 14;			//为num赋一个值
	printf("What is your favorite day in April ? "); //使用printf()函数
	printf("14\n");
	printf("My favorite day is %d\n", num);
	return 0;
}

1. Quick Summary

# include <stdio.h>		//包含另一个文件

This line tells the compiler to include the contents of stdio.h in the current function. stdio.h is a standard part of the C compiler package, providing support for keyboard input and screen output .

int main(void)		//函数名

A C program includes one or more functions, which are the basic modules of a C program. This line of code indicates that there is a function named main(). The parentheses indicate that main() is a function name (compared to the f(x) function in mathematics). Int indicates that the main() function will return an integer, and void indicates that there are no parameters in main().

Note : The curly braces on the left indicate the beginning of the function definition, and the curly braces on the right indicate the end of the program

int num;	//(声明) 该声明表示将使用一个名为num的变量,而num为int(整数)类型
	num=14;    //(赋值表达式语句)把14赋值给变量num
printf("What is your favorite day in April ? ");

This line calls the printf() function to print on the screen What is your favorite day in April? (the cursor stays on the same line)

printf("14\n");

This line calls the printf() function again, adding "14" after the content printed by the previous statement. The code \n is a shift character, which tells the computer to start a new line, and the cursor will move to the next line.

printf("My favorite day is %d\n", num);

Finally, call printf() again to embed the value of num into the content enclosed in double quotes and print it.
%d tells the computer in what form to output the value of num and where to print it .

return 0; 	//返回0给操作系统

2. Details of the program

1. #include directives and header files

The function of #include <stdio.h> is equivalent to copying and copying , inputting all the content in the stdio.h file at the location of the line.
Tips:
#include This line of code is a C preprocessing instruction .
All C compiler packages provide a stdio.h file. The meaning of the file name is: standard input output (standard input/output header file), which supports keyboard input and screen output .

2. main() function

main() is called the main function, and it is the entry point for all programs to run. [There is only one main function in a C language program . The function of () is used to identify that main() is a function.

3. Notes

Comments come in two flavors:

  • C language comment style / xxxxxx /
    defect: does not support nesting
  • C++ comment style
    can comment one line or multiple lines (recommended)

4. Curly braces, function body and block

Curly braces enclose the function main(). In general, all C functions use { } to mark the beginning and end of the function body. The C language stipulates that only { } plays this role and cannot be omitted.
Curly braces can also combine multiple statements in a function into one unit or block.

5. Declaration

int num;		//声明

Declarations are one of the most important functions of functions. Two things are done in the above declaration (1. there is a variable called num in the function 2. int indicates that num is a variable).
int is a keyword of C language, which represents a basic data type of C language.
Tips:
Before C99, the C language stipulated that the variable declaration must be at the top of the block , that is, no other statements could be before the declaration.

int main () //C99之前语法
{
    
    
	int cats;
	int dogs;
	cats=23;
	dogs=12;
	//其他语句

Following the C++ convention in newer C99 and C11, declarations can be placed anywhere in a block.

int main()	//目前的C语法规则
{
    
    
	int cats;
	cats=23;	//赋值语句可以放在声明语句前
	int dogs;
	dogs=12;

6. Assignment

num=1;		//赋值表达式语句(把1赋值给num)

Assignment is one of the basic operations of C language. When executing the declaration statement (int num;), the compiler reserves a certain size space for the variable num in the computer memory, and stores the value in the previously reserved space when executing the assignment expression (num=14;) . Since num can be assigned different values, num is called a variable. At the same time, the assignment statement is to assign the value on the right side to the left side.

7. printf() function

	printf("What is your favorite day in April ? "); 
	printf("14\n");
	printf("My favorite day is %d\n", num);

These three lines all call a standard function printf() of C language. The content in the parentheses is the information passed from the main() function to the printf() function . parameters; formal parameters, or formal parameters, are variables in a function that store values). The printf() function looks at the contents of the double quotes and prints the argument to the screen .
Comparing the third line of code, it is found that %d in the output parameter is replaced by the number 14, and 14 is the value of num. %d is equivalent to a placeholder, indicating the location of the output num. % reminds the program that a variable will be printed there, and d indicates that the variable will be printed as a decimal integer . The f in the printf() function name reminds the user that this is a formatted print function .

8. return statement

return 0;

The int in the function header int main(void) indicates that the main() function should return an integer. In the C language, a function with a return value must have a return statement, and return is the value to be returned. If you forget to omit the return statement, the program will put back 0 after running , so the return statement can be omitted here. (It is not recommended to write this way, develop a good programming habit, and prevent the statement from being forgotten in other functions with return values)

end

This is the end of the article in this film, hoping to help readers further understand the C program. If you think it is helpful to you, remember to like and pay attention. thank you for your support! !

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/130046337
Recommended