Blog homepage:https://blog.csdn.net/wkd_007
Blog content:Embedded development, Linux, C language, C++, data structure, audio and video
Content of this article:Introducing the 32 keywords of the standard C language
Golden sayings to share:You must try it if you have the chance. In fact, the cost of trial and error is not high, but the cost of missing out is. High
Detailed explanation of 32 keywords in C language (1): data type part (char, short, int, long, float, double, struct, union, enum, void)
Detailed explanation of 32 keywords in C language (2): modified type part (auto, signed, unsigned, static, extern, const, register, volatile)
Detailed explanation of 32 keywords in C language (3): structural statement part (if, else, switch, case, default, do, while, for, break, continue, return, goto)
Detailed explanation of 32 keywords in C language (4): Others (typedef, sizeof)
Directory
- 1. auto, signed, unsigned
-
- 1.1 auto keyword
- 1.2 signed keyword
- 1.3 unsigned keyword
- 2. static keyword
-
- 2.1 static modified variables
- 2.2 static modified function
- 3. extern keyword
- 4. const keyword
-
- 4.1 const modifies read-only variables instead of constants
- 4.2 The difference between const variables and macro constants
- 4.3 const modified pointer
- 4.4 const modifies the parameters and return value of the function
- 5. register keyword
- 6. volatile keyword
There are 32 keywords in C language. I will introduce them mainly in the following four aspects.
Function | Keyword |
---|---|
Types (10) | char, short, int, long, float, double, struct, union, enum, void |
Modification type (8) | auto, signed, unsigned, static, extern, const , register, volatile |
Structural statements (12) | if, else, switch , case, default, do, while, for, break, continue, return, goto |
Others (2) | typedef、sizeof |
We introduced char, short, int, long, float, double, struct, union, enum, void
;
This article mainly introduces auto, signed, unsigned, static, extern, const, register, volatile
which are related to types.
1. auto, signed, unsigned
1.1 auto keyword
After writing C language for so long, you probably have rarely or even never used the auto
keyword. Because in the default state of C language, the compiler defaults to all variables as auto
, so it doesn’t matter whether you add auto
or not.
Therefore, the auto
keyword can be used as if it does not exist! ! ! Leave it alone.
1.2 signed keyword
The signed
keyword is the same as auto
and is rarely used. In the C language, by default, the compiler’s default data is of the signed
type. .
Therefore, the signed
keyword can be used as if it does not exist! ! ! Leave it alone.
signed
means signed, that is to say, this variable can be a positive number or a negative number;
The computer only recognizes 0 and 1, and the-
numbers of negative numbers cannot be stored in memory. It is agreed that the highest bit of the basic type is 1 to represent negative numbers;Numerical values are always stored using complement codes in computer systems:
The complement of a positive number is the same as its original code;
The complement of a negative number: the original code of the absolute value of the number is bit-wise inverted and 1 is added, and finally the highest bit is written as 1.
For example: -5 of char type, its complement is: -5’s absolute value 5 (binary0000 0101
) bitwise inverted to get1111 1010
, plus 1 equals1111 1011
, and then write 1 to the highest bit, it is still1111 1011
(hexadecimal0xFA
). So the complement of -5 is0xFA
.
1.3 unsigned keyword
1. char
and unsigned char
: Under normal circumstances, the simple char
type should only be used for the storage and use of character values; Unsigned char
type variables can only be used for the storage and use of numerical values.
2. All unsigned constants should be suffixed with U: unsigned u_ch= 100U;
3. The result of the operation between the signed
type variable and the unsigned
type variable is the unsigned
type;
4. The unsigned
type is always greater than 0. Never write the following error
code when judging conditions:
// `Error` code demonstration: // https://blog.csdn.net/wkd_007/article/details/133932760 unsigned int i = 100; while(i>=0)//i is unsigned, so the condition is always satisfied {<!-- --> i--; printf("i=%u\ ",i); }
5. When using printf to print unsigned
types, use %u
; unsigned long
uses %lu
.
Please indicate when reprinting: https://blog.csdn.net/wkd_007/article/details/133932760
2. static keyword
The static
keyword can limit the scope of a function or variable; it can also specify which area of memory the variable is stored in;
2.1 static modified variables
1. Global variables modified by static
(static global variables): There is a static area of memory
. The scope is limited to the file in which the variable is defined. Other files are declared using extern. There is no way to use it either. To be precise, the scope starts from the place of definition and ends at the end of the file. The lines of code before the place of definition cannot use it. If you want to use it, you must declare it before. Therefore, it is generally defined directly at the top of the file.
2. Local variables modified by static
(static local variables): defined in the function body, they can only be used in this function, and cannot be used in other functions in the same document. Since variables modified by static always exist in the static area of memory
, even if the function ends, the value of this static variable will not be destroyed, and the value can still be used the next time the function is used. .
Refer to the following code to deepen your understanding:
//static.c //https://blog.csdn.net/wkd_007 #include <stdio.h> static int gs_i; void fun(void) {<!-- --> static int s_i = 0; int i = 0; printf("gs_i=%d s_i=%d i=%d\ ",gs_i + + , s_i + + , i + + ); } int main() {<!-- --> int i=0; for(i=0; i<10; i + + ) {<!-- --> fun(); } return 0; }
2.2 static modified function
static The modified function limits its scope to this file only. In actual development, if the function is only used in this file, add static
at the end, so that there is no fear of having the same name as functions in other files.
3. extern keyword
The extern keyword is generally used to declare global variables
or functions
. Its function is to tell the compiler that the global variable
or Function
is defined outside this file.
This file uses global variables
or functions
declared by extern
, which can be used normally after being declared there in this file.
Refer to the following examples to deepen your understanding: the global variable extern_val
and the function get_extern_val
are defined in extern.c
and in extern_sample.c
If used, extern
statement needs to be added.
//extern.c int extern_val; int get_extern_val() {<!-- --> return extern_val; }
// extern_sample.c #include <stdio.h> extern int extern_val; //Declaration, variables are defined in other files (extern.c) extern int get_extern_val();//Declaration, function definition in other files (extern.c) int main() {<!-- --> extern_val = 0; get_extern_val(); return 0; }
If you are unclear about definitions and declarations, please refer to the article: https://blog.csdn.net/wkd_007/article/details/133622725
4. const keyword
4.1 const modifies read-only variables instead of constants
In C language, const
modifies read-only variables instead of constants; variables modified by const
only indicate that the variable cannot be modified directly, but can be modified through its address. Indirect modification; read-only variables modified with const
cannot be used after the case
keyword as a judgment condition;
Reference code understanding:
#include <stdio.h> int main() {<!-- --> const int ci = 100; printf("ci=%d\ ",ci); // ci=5;// Error: error: assignment of read-only variable ci’ int *pCi = & amp;ci;// warning: initialization discards const’ qualifier from pointer target type [enabled by default] *pCi = 5; // No error is reported, and the value of ci is indirectly modified printf("ci=%d\ ",ci); int i = 0; switch(i) {<!-- --> case 1: break; //case ci:// error: case label does not reduce to an integer constant //break; default: break; } return 0; }
4.2 The difference between const variables and macro constants
From an assembly point of view, the read-only variable defined by
const
only gives the corresponding memory address, rather than the immediate value like the#define
macro definition. , therefore, the read-only variable defined byconst
has only one backup during the running of the program (because it is a global read-only variable and is stored in the static area), while#define
Macro constants defined by macros have several backups in memory. Macro constants are replaced during the pre-compilation stage, while the value of const-modified read-only variables is determined at compile time. Macro constants have no type, while const-modified read-only variables have a specific type.
4.3 const modified pointer
If const
precedes *
, the object pointed to by the pointer cannot be changed;
const
follows *
, then the value of the pointer cannot be changed;
If there is const
before and after *
, then the value of the pointer and the value of the object pointed to by the pointer cannot be modified;
const int *p; // p is mutable, the object pointed to by p is immutable int const *p; // p is mutable, the object pointed to by p is immutable int *const p; // p is immutable, the object pointed to by p is mutable const int *const p; //Both the pointer p and the object pointed to by p are mutable
4.4 const modifies the parameters and return values of functions
void Fun(const int * p);
The above code const modifies the parameters of the function, telling the compiler that *p cannot be changed in the function body, thus preventing some unintentional or mistaken modifications by the user. In actual development, if a function parameter has a pointer and you do not want the object pointed to by the pointer to be modified, you can add const modification.
The const modifier can also modify the return value of a function, and the return value cannot be changed.
5. register keyword
First, let’s understand how data is processed by the CPU?
The data is taken out of the memory and placed in the register first, and then the CPU reads the data from the register for processing. After processing, the data is also stored in the memory through the register. The CPU does not deal directly with the memory.The
register
keyword requests the compiler to store variables in CPU internal registers as much as possible instead of accessing them through memory addressing to improve efficiency.
Note that this is as much as possible, not absolutely. Because a CPU has a limited number of registers.Note:
1. Theregister
variable must be of a type that can be accepted by the CPU register, that is, a single value, and its length should be less than or equal to the length of the integer;
2. Theregister
variable may not be stored in memory, so the address operator& amp;
cannot be used to obtain the address of theregister
variable.
6. volatile keyword
The function of the volatile
keyword is to tell the compiler that the variable may change at any time, and to get the value from memory every time it is used, and not to optimize the code of the variable.
#include <stdio.h> int main(void) {<!-- --> //volatile int i = 10; int i = 10; int j = i; // ① statement int k = i; // ② statement printf("i=%d j=%d k=%d\ ",i,j,k); return 0; }
In the above code, because i
is not assigned a value in the ①② statement, the compiler will think that the value of i
has not changed, and will take out from the memory during the ① statement. The value of i
is assigned to j
. In statement ②, this value is continued to be used to assign value to k
. This optimization can improve efficiency.
If i
is modified with volatile
, then every time i
is used, the compiler will get the value from memory without optimization. This keyword may need to be used in some lower-level codes (such as Linux driver code, Linux kernel source code).
Can a parameter be both const and volatile? For example:
const volatile int i = 10;
Yes, for example a read-only status register. It is volatile because it can be changed unexpectedly. It is const because programs should not try to modify it.
Please indicate when reprinting: https://blog.csdn.net/wkd_007/article/details/133932760
If the article is helpful, please like it, collect it, support it, thank you