China Naming Network - Ziwei knowledge - 10 minutes to learn the basic grammar knowledge of C language.

10 minutes to learn the basic grammar knowledge of C language.

I believe you have seen the effect of the first sample code. The effect of this program is very simple, which is to output a line of characters "Hello World" on the screen.

# include & ltstdio.h & gt

int main()

{

printf(" Hello World \ n ");

Returns 0; ?

}

According to this short code, we can learn some basic grammar knowledge of C language.

1. Main functions

First of all, the main function, a standard main function is as follows:

int main()? // ? This is the main function

{

Returns 0; ? // ? Main function return value

}

The translation of main into Chinese is the main and most important meaning, and in C language, it represents a main function. Later, we will discuss what is a function in a computer language and the significance of the main function in a C language program. )

The double slash is followed by a note, similar to the teacher's note in the exercise book.

Generally speaking, comments are used to mark the purpose of this code or explain ideas. Because comments will not be compiled into code, no matter what comments are added, it will not have any impact on the actual operation of the code.

Like the code above, the main function and the return value of the function are marked with comments.

2. What is a function?

Let's talk about what a function is first. In everyone's cognition, function is a noun in the field of mathematics, which may be shown in the following figure:

However, this is only a function in the field of mathematics, which is completely different from the function in programming languages.

In a programming language, you can think of a function as a box, which has the following characteristics:

0000 1.? At the beginning of execution, the function can enter some values.

00002.? During execution, functions can do some things.

00003.? After execution, this function can return some values.

Let's see what our main function does in these three features.

0000 1.? The main function has no input.

00002.? The main function prints a line of words to the screen.

00003.? The main function returns 0.

Where int represents the return value type of the function and int is the abbreviation of integer.

Main is the function name, and the parenthesis () after main is the input parameter, which is currently empty.

Return is followed by the return value of the function, which is 0. And 0 is an integer, which corresponds to the int before the function name.

Let's summarize the writing formula of the function.

Function return value type Function name (function input parameter value)?

{

Do something.

Return function returns the value;

}

3. Write your own function

We might as well strike while the iron is hot and write a function of adding two integers according to the formula for writing functions above. What this function needs to do is to input two integers and return the result of their addition.

Well, since this function is used to calculate addition, let's name this function add. Of course, the function name of a custom function can be written according to your own preferences, even if it is written as aaaaa. However, in order to make the function name semantic and easy for people to read and understand, we generally use English as the function name.

// ? This code is called the function definition of add function.

int add(int a,int b)

{

Return a+b;

}

Ok, we have finished writing an add function. This code is called the function definition of the add function.

4. The main function is the entrance of the whole C language program.

We define an add function ourselves, so how to use it? Can the add function be run directly?

The answer is no.

All C language codes have an initial entry, which is the main function main. After entering the main function, you can call other functions through the main function.

This also means that each C language code can only have one and only one main function.

We modified the code a little, and now the code is as follows.

# include & ltstdio.h & gt

int add(int a,int b)?

{

Return a+b;

}

int main()

{

Int result;

result = add(2,3);

Printf("%d ",result);

Returns 0;

}

When the program runs, it will first enter the main function main. Then call the add function we just wrote. We passed two values to the add function, integers 2 and 3.

According to the definition of the function, when we call two parameters, we must also pass the two parameters, and the types need to be consistent, otherwise the compilation will report an error.

So naturally, we will think, who called the main function? Must the return value of the main function be int?

The main function is automatically called at the beginning of the program, and there is no need to actively call the main function in the program.

The return value of the main function will be returned to the program that called this program.

C language standard stipulates that the main function has a return value, which must be int. If the program ends normally, the return value is generally set to 0.

5. To call a function, you must first know the function.

Let's see how the compiler understands the identifier add.

The compiler will start with the code and read it from top to bottom.

The compiler first saw the definition of function and described a function called add. Then, you need to use add in main, because the compiler already knows the definition of add, so the compiler can compile normally.

But what if the function definition and function call are reversed?

First, the compiler sees the add identifier, and the compiler will wonder, what is add? The compiler can't understand what add is. Therefore, the compiler will report an error and stop compiling.

6. What is a variable?

After calculating the add function, you need to do something to accept the value returned by add. So, we declare a variable of int integer before add.

What is a variable? You can think of it as an empty box, which can hold any other value consistent with its type.

It turned out to be just a name we gave it. Of course, you can name it at will. For example, he or Xiangzi can do it.

We put the 5 returned by add into the result. Therefore, the value contained in the result is 5.

= equal sign, this is an assignment operator in C language. I believe you have found that it has the function of putting the right value into the left variable. Like functions, the equal sign here is very different from the equal sign in mathematics, and does not mean equality.

Assignment operator is an operator that can put the value on the right side of a symbol into the variable on the left.

The following figure shows the process that the add function accepts 2,3 as input, returns 5, and assigns an assignment number = to the result.

Then can we write it like this? Remove int result; This one.

int main()

{

result = add(2,3);

Printf("%d ",result);

Returns 0;

}

The answer is no, variables must be declared before use.

When the compiler sees the result identifier, but has never seen the definition of result, of course, it will wonder what kind of variable it is, and it may not even be a variable but a function. In this way, the compiler can only give a hint of compilation error with regret and end the compilation.

Int result;

You must declare that there is a variable of type int named result, as shown above. Next, the editor will notice that the result identifier is a variable of type int. In the following code, you can use this result variable happily.

7. What is a literal constant?

So, do you need to declare values like 2, 3?

No, they are constants and cannot be changed. Once written, they are considered to be integer constants.

Similarly, literal constants of strings do not need to be declared, such as "Hello World\n". Wrapped in double quotation marks, we think it is a string to distinguish it from numerical values.

Variables can be changed by assigning values, but constants cannot be changed, so they cannot be assigned values.

2 = 3; // ? mistake

"Hello" = "World"; // ? mistake

8.printf function

# include & ltstdio.h & gt

int main()

{

printf(" Hello World \ n ");

Returns 0; ?

}

By now, you should have understood most of the meaning of this code. Let's go one step further and analyze it all.

Like add, printf is also a function, but this is not a user-defined function, but a function that comes with the system.

We pass the string constant "Hello World\n" to the printf function. When we run the code, we see this line of characters on the screen. Obviously, the function of printf is to output strings to the console.

Printf consists of the word print and the first letter f of the word format, which means formatted printing.

The output of early computers mainly depended on connecting printers to print characters on paper. At present, most of the computer output is realized on the screen. But the word print has been preserved. Sometimes we will continue to use the saying of printing, but in fact, we are outputting the characters in the console on the screen.

More usage of printf function

int main()

{

Int result;

result = add(2,3);

Printf("%d ",result);

Returns 0;

}

Let's compare these two pieces of code again. When we wrote Hello World, we only passed 1 parameters to printf, but why can we pass 2 parameters to it after the add function? Do the number and types of parameters defined by the function need to be the same as when calling the function?

Printf is a very special function. It is a variable parameter function, so it can accept variable number and type of input parameters. Here we don't need to care too much about how to write a variable parameter function, we just need to use it temporarily.

Through the following examples, we can simply understand more uses of the printf function.

Used to print an integer: printf("%d ",integer);

printf("%d ", 12345);

Used to print two integers: printf("%d\n%d\n ",integer 1, integer 2);

// \n means line feed, that is, continue printing from the next line.

printf("A=%d\nB=%d\n ", 123,456);

Let's summarize printf's formula:

Printf("XXX occupied 1 XXX occupied 2xxxx occupied 3 ",replace 1, replace 2, replace 3);

The first parameter of printf must be a string (enclosed in double quotation marks). Where the placeholder is represented by type%. For example, the placeholder for an integer is% d, and the following replacement parameters will replace the previous placeholder in turn. Printf is a variable-length parameter function. As long as the placeholder of the first string parameter is written correctly, you can add as many substitution parameters as you want.

9.#include command

The printf function is not a function defined by us, but a function that comes with the system. This function is written in the file stdio.h If we want to use printf, we must first make the compiler understand printf.

We assume that the function definition of printf is written in the file stdio.h, and the code of the file stdio.h can be copied into our code with the #include command.

TIPS:printf function does not define TIPS:stdio.h, but contains the function declaration of printf function. But at this stage, you can understand that the function definition of printf is written in stdio.h