The basic structure of C language program (very suitable for novices)

1. The learning method of C language basic knowledge

1) The content of my course is carefully organized. Some knowledge points may not mention application scenarios, but they are definitely not useless knowledge. Knowledge not mentioned in the course can be understood, but there is no need to study in depth, which is of little significance.

2) Don't be greedy for learning, and you should understand that the key to learning is to cultivate the feeling of writing programs.

3) Don’t take notes, don’t memorize keywords, don’t memorize function parameters, if you forget, you can forget, because you have not experienced actual combat, so when it comes to the project actual combat course, it is natural to forget the content and remember everything .

4) Don’t use your mobile phone to watch videos. You just want to sleep when you watch. The process of learning is to watch and write, and you don’t see the programmer. Zhao Kuo, you know? It's the guy who reads the military books by air and doesn't make a difference. He only reads books and watches videos without writing programs, which is no different from Zhao Kuo.

5) The programmer's fingering is very important. You can't become a programmer if you cock Erlang's legs, smoke in your mouth, and wave a finger in Zen.

6) If the problems encountered in the learning process cannot be solved in 30 minutes, don't be entangled, and ask everyone in the group. However, it is opposed to the way of asking questions without thinking about them.

7) Before starting to learn the C language, you must be familiar with the Linux environment and master the basic usage of Linux commands and vi.

8) Masters all start from rookies, as long as you follow the video tutorials step by step to learn, you will surely become an excellent programmer.

Second, the process of C language program development

At this stage, the C program we write is relatively simple, and the program development process is as follows:

Insert picture description here

Three, install the C language compiler

The C language compiler under Linux is gcc, log in as the root user and execute the following command to install it:

yum  -y  install  gcc

Note that if your server does not have gcc installed, the above command will install the latest version of gcc, if you have already installed gcc, it will be updated to the latest version, so no matter how many times you execute the above command, there is no problem.

The prerequisite for installing gcc is that the server must have access to the Internet.

Fourth, the basic structure of the C program

The C program mainly includes the following parts:

1) Notes (explanatory text)

2) Preprocessing instructions

3) Main function entry

4) Main function body

Example (book1.c)

/*
 *  程序名:book1.c,此程序用于演示C程序的基本结构。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/

// 预处理指令,把头文件stdio.h包含进来。
#include <stdio.h>     // standard input output

// 主函数main,这里是程序执行的起点。
int main()
{
    
    
  // 调用printf函数在屏幕上输出文字。
  printf("我心匪石,不可转也。我心匪席,不可卷也。威仪棣棣,不可选也。\n");

  return 0; // main函数返回
}

Next we explain the above procedure.

1. Notes on the program

/*
 *  程序名:book1.c,此程序用于演示C程序的基本结构。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
// 预处理指令,把头文件stdio.h包含进来。
// standard input output
// 主函数main,这里是程序执行的起点。
// 调用printf函数在屏幕上输出文字。
// main函数返回

The above is the comment (explanatory text) of the C program. The purpose of the comment is to improve the readability of the program. It is for the programmer to see, not for the computer. The compiler will ignore it when compiling the program.

/* */ is used to comment multi-line text, /* is the beginning, */ is the end.

// Used to comment a line of text, it can be on its own line, or it can be placed after the code.

2. Preprocessing instructions

#include <stdio.h>     // standard input output

This line is a (compilation) preprocessing instruction, telling the C
language compiler to include the stdio.h file before compiling. The printf function is used in the program. If its header file is not included, a warning or error will occur during compilation.

3. The entrance of the main function

int main()

main is the main function, and the program is executed from here. In the same program, there can only be one main function.

4. Main function body

  printf("我心匪石,不可转也。我心匪席,不可卷也。威仪棣棣,不可选也。\n");

  return 0; // main函数返回

The code of the main function body is in a pair of curly braces. The main function body of this program has two lines of code.

The first line of code calls the printf function. The function of the printf function is to output the content to the screen. Here it is to output a line of text. The \n at the end of the text means a line break. Multiple \n can output multiple line breaks.

The second line of code return 0; indicates that the main function returns and the program exits.

Note that the blank lines in the body of the main function and the spaces in front of each line of code are to improve the readability of the program and have no effect on the function of the program.

Five, compile and execute the C program

Use the gcc command to compile the source program book1.c under the Linux command line.

gcc -o book1 book1.c

After the compilation is completed, the target program book1 will be generated (the executable program name is specified by the -o parameter), and ./ executes it.

./book1

The result of the execution is to output a line of text on the screen.

我心匪石,不可转也。我心匪席,不可卷也。威仪棣棣,不可选也。

running result
Insert picture description here

Six, knowledge summary

/**/ Multi-line comments.

// Single line comment.

#include contains preprocessing instructions for other files.

The starting point of main program execution.

{} The beginning and end of the function body and statement block.

() The parameters of the function are placed in parentheses.

The "" string is enclosed in double quotes.

\n Newline character.

; Semicolon, the end of a line of code.

1) The above are all grammatical conventions of the C language, which are rules, cannot be changed, cannot be questioned, and must be followed;

2) C language is strictly case sensitive;

3) Chinese full-width punctuation marks cannot be recognized by C language, and an error will be reported during compilation;

4) C programs use a semicolon ";" to indicate the end of a language, and multiple statements can be written on one line.

5) If a line of code only has a semicolon ";", it means an empty statement and does nothing.

Seven, homework

1) Please write the simplest C program, it doesn't matter what function it implements, the less code the better.

2) There is a pit that Chinese programs often fall into, but American programs do not. What kind of pit is this?

3) Output the Book of Songs "I'm a Stupid Bird" on the screen, the source program is named book2.c, and the execution effect is as follows:

Insert picture description here

4) Please spell out a big letter H with the symbol "*" in the middle of the screen, the source program is named book3.c, and the execution effect is as follows:

Insert picture description here

8. Copyright Statement

C language technology network original article, please indicate the source of the article, the author and the link to the original text.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If this article is helpful to you, please like and support, or forward my article in your blog, thank you! ! !
If the article has typos, or content errors, or other suggestions and comments, please leave a message for correction, thank you very much! ! !

Guess you like

Origin blog.csdn.net/qq_43403759/article/details/108420379