The basic structure of C language program

A basic knowledge of C language learning

1) My course content is curated, some knowledge may not mention scenarios, but certainly not useless knowledge. Knowledge course not mentioned can look, but do not have in-depth study of little value.

2) Do not learn too fast, haste makes waste truth we should understand that learning is focused on training feeling to write the program.

3) Do not take notes, do not remember keyword, function parameters do not remember, forget forget, forget that because there is no actual combat, so to project combat course of time, forget the content is very natural to think of it all .

4) Do not look at using a mobile phone video, I watched wanted to sleep, the learning process is watched and write, did not look out of the programmer. Kuo, you know? Art of war is not a device that reads empty guy, just reading to see the video do not write programs, and Kuo is no different.

5) programmers fingering is very important, tilted his legs, mouth chattered smoke, waving a finger, a slovenly appearance, it can not become programmers.

6) problems encountered in the process of learning, if not thirty minutes to solve, do not struggle, ask in the group. However, the opposition has encountered a problem without thinking questioning.

Before 7) to start learning C language, you must first be familiar with Linux environment, to acquire common Linux commands and basic usage of vi.

8) masters are from the rookie to start, as long as you press the step by step video tutorials to learn, will be able to become a good programmer.

Two, C language program development process

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

Here Insert Picture Description

Third, install the C language compiler

Linux C language compiler is gcc, log in as root, 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 installed gcc, will be updated to the latest version, so the above command execution regardless of how many times, no problems.

Prerequisites to install gcc is that the server must have access to the Internet.

Fourth, the basic structure of the program C

C program includes the following components:

1) Notes (caption)

2) Pretreatment instruction

3) inlet main function

4) The main body of the function

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 above this program.

1, program notes

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

These are annotated C program (caption), the purpose of comments is to improve the readability of the program is to the programmer, not to look at the computer, it will be ignored when the compiler to compiler.

/ * * / For multiline text annotation, / * beginning * / is the end.

// for a line of text annotation can be an exclusive line can be placed after the code.

2, preprocessing directives

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

This line is (compile) pre-processing instructions that tell the C
language compiler before compilation to include stdio.h file, the program uses the printf function, if it does not include the header file, there will be a warning or a compile-time error.

3, the main function of the inlet

int main()

the main function is main, program execution begins here. In the same program, main function can only have one.

4, the main body of the function

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

  return 0; // main函数返回

The main function of the code member of the pair of braces, the main function of the present application has two lines of code.

The first line of code calls the function printf function printf function is to output content to the screen, here is the output line of text, ending with the text \ n newline, more \ n can output multiple wrap.

The second line return 0; a front function returns, the program exits.

Note that, main body of the function of blank lines and spaces per line of the preceding code is to improve the readability of the program, has no effect on the functions of the program.

Fifth, compile and execute C programs

In the Linux command line compiler source code book1.c with gcc.

gcc -o book1 book1.c

After compilation, the generated object program book1 (executable program name is specified by the -o parameter), plus ./ execute it.

./book1

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

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

running result
Here Insert Picture Description

Sixth, knowledge summary

/ ** / multi-line comments.

// a single line comment.

#include preprocessing directives comprise other files.

Main starting point for program execution.

{} Function body, the start and end of a block of statements.

() Function parameters in parentheses.

"" String in double quotes.

\ N newline.

; Semicolon, end of a line of code.

1) These are the C language syntax conventions, rules, can not be changed, can not be questioned, must comply;

2) C language strictly case-sensitive;

3) Chinese full-width punctuation C language does not recognize, would compile-time error;

4) C program with a semicolon ";" indicates the end of a language, you can write multiple statements on one line.

5) If a line of code is only a semicolon ";", indicating an empty statement does nothing.

Seven, after-school job

1) Please write a simple C program to realize it does not matter what function the code as possible.

2) there is a pit, China often fall into the program, but the program has not the United States, is this what pit?

3) output on the screen in The Book of Songs "I'm a silly bird", the source named book2.c, implementation of the results is as follows:

Here Insert Picture Description

4) Use the symbol "*" spell a big letter H, the source name in the middle screen book3.c, perform the following effects:

Here Insert Picture Description

Eight, copyright notice

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

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Guess you like

Origin www.cnblogs.com/wucongzhou/p/12501748.html
Recommended