Getting to know the keywords of C language for the first time

Table of contents

auto:

break:

 case,default:

char,short,int,void,long,float,double:

if,else,while,for,do while,struct:

enum:

signed,unsigned:

 typedef:

register:

return:

static:


Hello everyone, today I will talk about the usage of common keywords in C language, then I will officially enter today's study\( ̄︶ ̄)/


Before talking about the specific usage of keywords, let me first explain a concept. Keywords are provided by C language and cannot be created by yourself . Pay special attention to the fact that keywords cannot be variable names!

Next, look at the picture below and list some keywords. I will pick some common and commonly used ones and explain them to you one by one.


auto:

You may be unfamiliar with this keyword when you see it. In fact, this keyword declares a variable as an automatic variable . auto is the most widely used type in C language. When defining a variable in a function, if it is not declared as other Type variables are all automatic variables, but the type specifier auto is generally omitted, so it is not common to see, but it is widely used. As shown below:

 When defining the int type num1, auto is added, and when defining the int type num2, double and char types, auto is omitted, but they are all automatic variables in essence, and the printed results are as shown in the figure above, and there is no obvious change.

Auto also has the following situations, for example:

auto num;

Omit the data type like this, only use auto to modify the variable, and the default variable in C language is int type

 Of course, through the monitoring window, it can be clearly seen that num belongs to the type of int. See the screenshot below for details.


break:

This keyword can be said to be very familiar, and it is often seen in the following switch statements and loop statements . Today, I will briefly talk about its usage to lay a good foundation for the following statements.

The main function of break is to break out of the current loop (for, while, do while) or switch statement.

(Since there are no blogs about loops yet, here is a simple switch statement as a demonstration.)

When break is used in the switch statement, the program can jump out of the switch and execute the statement after the switch; if there is no break statement, the following statement will continue to be executed, as shown below:

The switch statement has a break situation:

 The case where the switch statement does not have a break:

 After observation, it can be found that when there is a break, inputting a number will print the corresponding value, but when there is no break, not only the corresponding value will be printed, but also all subsequent values ​​will be printed, then I add break after case 2, the program Is it what we think?

 Obviously, it is completely consistent with our conclusion. Do you have a preliminary understanding of the function of break?

Regarding the use of break in loop statements, we will talk about ٩(๑•̀ω•́๑)۶ later when talking about loop statements


 case,default:

The main usage of these two keywords is in the switch statement , where the integer constant expression that needs to be input in the switch statement is behind the case , and default refers to all the integer constants that appear after the case. appeared as follows:

If you enter the numbers that have appeared after the case:

If you enter a number that has not appeared after the case:

 From the above two examples, it can be seen that if the number corresponding to the case is entered, the corresponding content will be printed. If the case has not appeared after the input, the content after default will be printed. This is the content of these two keywords. The main usage.


char,short,int,void,long,float,double:

These keywords are our common C language data types . If you have any questions, please pay attention to my blog about C language data types. The explanation is more detailed.


if,else,while,for,do while,struct:

These are the loop statements, branch statements, and keywords in the structure that will be learned later. I look forward to the explanation of these statements later.


enum:

This is a keyword related to enumeration, which was not mentioned in the previous blog, so here is a supplement

enum is used to define enumerated types, for example:

First of all, let me emphasize the format. Each enumeration constant and the following enumeration constants are separated by commas , the last one is not, and there must be a semicolon after the curly braces ! I also want to explain here: enumeration constants are generally expressed in uppercase letters !

Among them, MON, TUE, WED, THU, FRI, SAT, SUN are the enumerated constants we defined

With these enumeration constants, we can assign values ​​to the next enumeration variables, for example:

enum Day one = FRI;

 Here, the variable one is assigned a value of FRI, and they are equivalent. This result can be clearly seen from the first print result in the figure below

 The enumeration constant has a specific value . If it is not specifically declared, it starts from 0. For example, the second print result, the first four are 0, 1, 2, 3 in sequence

But if there is a specific declaration, the value represented by the next enumeration constant declared will increase in order. For example, if we slightly modify the code in the above figure, we can clearly observe this rule:

 The above is the specific content of the enumeration constant ╰(*´︶`*)╯


signed,unsigned:

These two keywords are very simple, signed means signed , and unsigned means unsigned .


 typedef:

This keyword is a type renaming, for example:

 Define the type of a as an unsigned integer. If the name is too long, you can use typedef to rename it to u_int, which is more convenient. You can roughly understand what it means by looking at the picture.


register:

register keyword

int num1 = 1;
register int num2 = 2;

It means that it is recommended to put 2 in the register, and everyone can understand it a little bit.


return:

Return is return. In most of the main functions we write, return 0 will appear; this keyword means return value .


static:

This keyword generally modifies variables and functions

When modifying local variables: do not destroy

Static local variables are not destroyed when out of the local scope, and still exist when entering the function next time

When modifying global variables: a global variable itself has external link attributes, but after being modified by static, it becomes an internal link attribute, which can only be used inside the source file and cannot be used inside other files

When modifying a function: a function itself has an external link attribute, but after being modified by static, it becomes an internal link attribute, which can only be used inside the source file and cannot be used inside other files

Here we only talk about the situation when modifying local variables, which is relatively common and easy to grasp, let’s go to the code

Without static:

 Add static:

 Everyone must have found that the results are completely different. For code without static, after each for loop, the life cycle will end, and the variables will be destroyed, so the value calculated last time cannot be maintained (if you don’t know about the life cycle, you can pay attention to it. The blog I posted first met the data structure of C language, the life cycle one)

After adding static, the variable num will not be destroyed when it goes out of the local scope, and it will still exist when entering the my_test function next time, so the above two different results will appear. Do you understand (´▽`)ノ♪


 This is the whole content of the keywords of C language for the first time today. Thank you for watching. See you in the next blog.

(*^▽^*)o♪

Guess you like

Origin blog.csdn.net/m0_64411530/article/details/122210666