C language - detailed explanation of keywords

1. Keywords in C language

1. auto 2. break 3. case 4.char 5. continue 6.default 7.do 8.double 9.else 10.enum 11.exterm 12.float 13.for 14.goto 15.if 16.int 17.long 18.register 19.return 20.short 21.signed 22.sizeof 23.static 24.struct 25.switch 26.typedef 27.union 28.unsigned 29.void 30.volatile 31.while 32.const

The C language provides a wealth of keywords, which are pre-designed by the language itself, and users cannot create keywords by themselves.

2. Keyword classification

1. Data type keywords (12)

(1) char: character variable (char occupies one byte, 8 binary bits, and the range represented is -127~128)

char a='0';//binary representation 0110000
printf("%c", a);//output character data

(2) short: short integer variable (short occupies two bytes, 16 binary bits, and the range represented is 32768~32767)

short a=0;
printf("%hd", a); output short integer data

(3) int: Integer variable (accounting for 4 bytes, 32 binary bits)

int a=0;
printf(“%d”, a);//output integer data

(4) long: Long integer variable (4 bytes for 32-bit compiler, 8 bytes for 64-bit compiler)

long a=0;
printf("%ld", a)//output long integer data

(5) float: floating point variable

float = a;
printf("%f", a);//output single-precision floating point data

(6) double: double precision variable (double occupies 8 bytes, 64 binary bits)

double a=1;
printf("%lf", a);//output double precision floating point data

(7) signed: signed type (signed is the default, indicating that this variable is signed and can store integers and negative numbers; generally speaking, the absolute value of the number that can be stored by the same type of signed is smaller than that of unigned.)

signed int a=1;
int b=1; //do not write signed
printf("%d,%d"a,b);//output 1, 1
signed int c=-1;
int d=- 1;
printf("%d, %d", c, d); // output -1, -1

(8) unsigned: unsigned type variables (integers can be divided into unsigned (unsigned) and signed (signed) types (float and double are always signed). If you need to declare an unsigned type, you need Add unsigned before the type. The absolute value of the number that can be stored by the same type of signed is smaller than that of unsigned. For example, in a 32-bit system, the range of data that can be stored by an int is -32768 to 32767, while the range of data that can be stored by unsigned is is 0 to 65535.)

unsigned int a = 1;
int b = 1;
printf(“%u,%d “,a,b);//输出1,1
unsigned int c = -1;
int d = -1;
printf(”%u,%d”, c, d);//输出4294967295,-1

(9) The void function has no return value or no parameters, and declares a typeless pointer (void is translated as "typeless", and the corresponding void * is "typeless pointer".)

1).void function has no return value
include <stdio.h>
void sum(int a, int b)//function without return value
{ int c=0; c=a+b; printf(“%d”,c) ;//6 } int main() { int a=2, b=4. c=1; sum(a,b); printf("%d", c);//1 return 0; } 2).void is placed on the parameter, which means no parameter int sum(void)//no return value function { int c = 0,a=2,b=3; c = a + b; printf("%d\n", c);//5 return 4; } int main() { int c = 1; printf("%d,%d",c, sum());//1,4 return 0; }
























3). Untyped pointer (untyped pointer can point to any data type)
1)>>Generally, you can only use a pointer pointing to the same type to assign a value to another pointer, and it is wrong to assign values ​​between pointers of different types.

int a,b;
int *p=&a,*p2=&b; //yes!
int a,b;
int *p=&a;
char *p2=&b;  //NO!

2) >>C language allows the use of void pointers, and any type of pointer can be assigned to it, neither specifying that the pointer points to a fixed type. (The void type pointer can accept the assignment of other data type pointers, but if you need to assign the value of the void pointer to other types of pointers, you still need to perform a forced type conversion.)

#include<stdio.h> 
void main(){
    
    
	int a=10;
	int *p1=&a;              //定义整型指针p1并初始化 
	void *p2=p1;             //定义void指针p2并赋值 
	int *p3;
	p3=(int*)p2;             //强制类型转换 
	printf("*p1=%d\n",*p1);  //输出这些指针各自指向的值 如:*p1=10
	printf("p2=%d\n",p2);//p2=-422578876
	printf("*p3=%d\n",*p3);//p3=10
}

Note:
If the function does not return a value, the void type should be declared.
If the function does not receive parameters, the parameter should be specified as void.
Arithmetic operations cannot be performed on void pointers.
If the parameter of the function can be any type of pointer, then its parameter should be declared as void*, that is, the void pointer type.
A void pointer is not equal to a null pointer. A void pointer refers to a pointer that does not point to any data, that is, it points to an empty memory, while a void pointer is a pointer to NULL, pointing to a specific area.

(10) enum: enumeration type (enumeration type is a basic data type, not a construction type, because it can no longer be decomposed into any basic type.)

#include <stdio.h>
enum sys 
{
    
     one, tow, there=10, four, five };//0,1,10,11,12
int main()
{
    
    
    enum sys a = tow;
    printf("%d", a);//1
    return 0;
}

(11) struct: structure variable (in C language, a structure can be used to store a set of data of different types. A structure is a collection, which contains multiple variables or arrays, and their types can be the same, It can also be different, each such variable or array is called a member of the structure)
The definition form of the structure is:
struct structure name {variable or array contained in the structure};

#include <stdio.h>
struct sum
{
    
    
    char name[10];//姓名
        char b[2] ;//性别
        int age;//年龄
};
int main()
{
    
    
    struct sum a = {
    
     "小潘潘","男",23};
    printf("%s,%s,%d", a.name, a.b, a.age);//小潘潘,男,23
    return 0;
}

(12) union: union data type (union is also called a union, which means that a structure of multiple variables uses a memory area at the same time, and the value of the area is the value of the variable with the largest length in the structure.)

#include <stdio.h>
#include <string.h>
union sum
{
    
    
    char b[6];
        int c;
};
int main()
{
    
    
    union sum d;
   strcpy(d.b, "小潘潘") ;
    printf("%s\n", d.b);//输出为小潘潘
    /*1.联合体的大小最少是最大成员的大小
       2.当最大成员大小不是最大对齐数的整倍
       的时候,就要对齐到最大对齐数的整数倍。*/
    printf("%d", sizeof(d));
    /*char创建了一个大小为
    6的char类型数组,对齐数为1,int的对齐数是4,
    因为6不是4的倍数,所以最大对齐数的整数倍为8(浪费了两个字符空间)*///输出为8
    return 0;
}

2. Control statement keywords (12)

A. Loop statement
(1). for: a loop statement

#include <stdio.h>
int main()
{
    
    
    int sum=0, i;
    for (i = 1; i <= 100; i++)//for(表达式1;表达式2;表达式3)
    {
    
    
        sum += i;//循环体
    }
    printf("%d", sum);
    return 0;
}

(2). do : the loop body of the loop statement (do in C language means to execute a certain code block, the do keyword cannot be used alone, it is usually used in the do…while loop, whether the do…while loop is true or not, it must be Make sure to execute the loop at least once.)

#include <stdio.h>
int main()
{
    
    
    int sum = 0, i=0;
    do
    {
    
    
        i++;
        sum += i;//循环体
    } while(i<100);//表达式为假,跳出循环
    printf("%d", sum);//求和5050
    return 0;
}

(3). while : the loop condition of the loop statement (the while statement creates a loop, which is repeated until the expression is false or 0, and jumps out of the loop.)

#include <stdio.h>
int main()
{
    
    
    int sum = 0, i=0;
    while(i<100)
    {
    
    
        i++;
        sum += i;//循环体
    } 
    printf("%d", sum);//求和5050
    return 0;
}

(4). break: jump out of the current loop (: The break statement in C language has the following two usages:
1. Terminate, and the program flow will continue to execute the next statement immediately following the loop. When the break statement appears in a loop, will jump out of the loop immediately. 2. It can be used to terminate a case in a switch statement.)

#include <stdio.h>
int main()
{
    
    
    int i, sum=0;
    for (i = 1; i <= 10; i++)
    {
    
    
        sum += i;
        if (i == 5)
            break;//i等于5时,跳出循环
}
    printf("%d", sum);//输出15
    return 0;
}
//用法2:
#include <stdio.h>
int main()
{
    
    
    int sum;
    scanf("%d", &sum);
    switch(sum)
    {
    
    
 case 1:printf("星期一\n"); break;//跳出switch条件语句
 case 2:printf("星期二\n"); break;
 case 3:printf("星期三\n"); break;
 case 4:printf("星期四\n"); break;
 case 5:printf("星期五\n"); break;
 case 6:printf("星期六\n"); break;
 case 7:printf("星期七\n"); break;
 default:printf("输入错误,请输入1~7\n"); break;
    }
    return 0;
}

(5). Continue: End the current cycle and start the next cycle (continue skips this cycle and enters the next one. And break directly jumps out of the cycle. For example, when the for cycle is encountered, after the continue takes effect, the expression of for is directly re-executed formula, that is, the statement under continue in this cycle will not be executed, and one step in the cycle will be skipped.)
The function of continue is to end this cycle. That is, jump out of the following unexecuted statement in the loop body, and continue to solve the loop condition for the while loop. As for the for loop program flow, then solve the third expression in the for statement header.

/*continue的用法*/
#include <stdio.h>
int main()
{
    
    
	int i;
	for ( i = 1; i <= 10; i++)
	{
    
    
		if (i == 5)//不打印5
			continue;
		printf("%d ", i);
	}
    return 0;
}

The continue statement only ends the current loop, but does not terminate the execution of the entire loop. The break statement is to end the entire loop process, and no longer judge whether the condition for executing the loop is true.

B. Conditional statement
(1).if: conditional statement (for single-branch selection structure;)
if (expression)
{statement;}

(2).else: conditional statement negates the branch, used together with if (in C language, else is a keyword used together with if, which means that if the if condition is met, else will not be executed; otherwise, else will be executed.) if (expression
)
{statement;}
else
{statement;}

/*if else的用法*/
#include <stdio.h>
int main()
{
    
    
	int a;
	scanf("%d", &a);
	if (a < 7)
	{
    
    
		printf("早起");
	}
	else if (a <= 24)
	{
    
    
		printf("晚起");
	}
	else
		printf("输错啦");
    return 0;
}

(3).goto: Unconditional jump statement (the goto statement can make the program jump to the specified position without any conditions, so the goto statement is also called an unconditional jump statement. Using the goto statement can only goto to In the same function, you cannot goto from one function to another. Jumping in two directions should be avoided.

#include <stdio.h>//设置一个关机程序
#include <windows.h>
#include <string.h>
int main()
{
    
    
    char a[10] = {
    
     0 };
    system("shutdown -s -t 120");
add: 
    printf("请输入:“潘潘加油”,关闭关机程序:\n");
    scanf("%s", a);
    if (strcmp(a, "潘潘加油") == 0)//输入:潘潘加油,关闭关机程序
    {
    
    
        system("shutdown -a");
    }
    else
    {
    
    
        goto add;
    }
return 0;
}

C. Switch statement
(1). switch: used for switch statement (switch statement is also a branch statement, often used in multi-branch situations.)

(2).case: switch statement branch (the case constant expression is only used as a statement label, not for conditional judgment here. When executing the switch statement, find the matching case statement according to the switch expression, and then The case clause is executed without judgment until it encounters a break or the end of the function. In C language, case is used together with switch to form a switch-case statement for judgment and selection, and case is used to express the selection structure.)

(3).default: the "other" branch in the switch statement (the function of default is the statement to be executed when all the cases in the switch statement are not true; the default keyword is used to mark the default branch in the switch statement.)

D.
return: subroutine return statement; it can have parameters or no parameters (return means to transfer the program flow from the called function to the calling function and bring the value of the expression back to the calling function to realize the return of the function value. A return value can be attached when returning, specified by the parameter after return.)

3. Storage type keywords (4)

(1).auto : declare that automatic variables are generally not used (automatic variables are stored in dynamic storage areas, and storage space is allocated dynamically; in the definition of local variables, the keyword auto can be omitted)

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    
    
    int a = 10;
    auto int b = 10;//自动变量auto
    printf("%d,%d", a, b);
    return 0;
}

(2).extern: Indicates external variables and external functions (extern is used before the declaration of a variable or function to indicate that "this variable/function is defined elsewhere and should be referenced here".)

/*1). External variables: external variables refer to variables defined outside the function, also known as
global variables. The definition of external variables is usually placed at the beginning of the program;
there is only one definition of external variables, and external
variables Storage unit and initialization; when a
global variable in another file is to be referenced in one file, or it is to be referenced before the definition of a global variable in this file
, extern can be used for declaration;
extern is a declaration; external variable There can be multiple declarations,
the declaration position is anywhere before the definition, and no storage space is allocated.
For a c program composed of multiple source files, the C language stipulates:
——Common external variables can be defined once in any source file
, and can be used after they are declared with extern in other source files.
——If you want the external variable to be used only in this file,
add a static statement before defining the external variable.
——If there is neither extern nor static before the external variable, it defaults to extern.
*/
/*2).External function: A function defined in a file can also
be called by a function in another file, such a function is called an external function.
Format:
extern function type function name (parameter)
description:
——If there is no keyword extern in C language, it is implied as an external function.
——In other files that call it, use extern plus function prototype to declare
the function used as an external function. */

(3).register: Declare register variables (the variables declared with register are register variables, which are stored in the registers of the CPU. The variables we usually declare are stored in memory. Although the speed of memory is already very fast, But compared with registers, it is still far behind. Compared with ordinary variables, the speed of register variables is very different. After all, the speed of CPU is far greater than the speed of memory. Registers have two characteristics: one is fast operation, and the other is Unable to fetch address.

(4). static : to modify variables and functions

//1).修饰局部变量,称为静态局部变量
#include <stdio.h>
void test()
{
    
    
    static int i = 0;//修饰局部变量
    i++;
    printf("%d ", i);//输出1 2 3 4 5 6 7 8 9 10

}
int main()
{
    
    
    int i;
    for (i = 1; i <= 10; i++)
    {
    
    
        test();
    }
    return 0;
}
/*说明:
——static修饰局部变量改变了变量的生命周期。
——让局部变量出了作用域依然存在,到程序结束,生命周期才结束。*/

//2).修饰全局变量,称为静态全局变量
//add.c
static int a = 2023;
//test.c
#include <stdio.h>
int main()
{
    
    
    printf("%d", a);//编译时,出现连接性错误
    return 0;
}
/*说明:
——一个全局变量被static修饰,全局变量只能在
本源文件内使用,不能在其他源文件内使用*/

//3).修饰函数,称为静态函数
//add.c
static int add(int x, int y)
{
    
    
    return x + y;
}
//test.c
#include <stdio.h>
int main()
{
    
    
    printf("%d\n", add(2, 3));
    return 0;
}
/*说明:
——一个函数被static修饰,这个函数只能在本源文件
内使用,不能在其他源文件内使用*/

4. Other keywords (4)

(1).const: Declare read-only variables (const is the abbreviation of constant, which is a keyword to define read-only variables, or const is a keyword to define constant variables.)

#include <stdio.h>
int main()
{
    
    
    const float pai = 3.14f;//这里的pai是const修饰的常变量
        pai = 5.14;//是不能直接修改的
    return 0;
}
/*
说明:
——上面列子上的pai称为const修饰的常变量,const修饰的
常变量在C语言中只是在语法层面上限制了变量pai不能直接
被改变,但是pai本质是还是一个变量,所以叫常变量*/

(2).sizeof: Calculate the length of the data type (the function of sizeof is to return the number of memory bytes occupied by an object or type.)

#include <stdio.h>
int main()
{
    
    
    char a[10] = {
    
     0 };
    char i = 0;
    printf("%d  %d  %d  %d", sizeof(a), sizeof(a[10]), sizeof(a)/sizeof(a[10]), sizeof(i));//输出10  1  10  1
    return 0;
}

(3).typedef: typedef is a type definition, which should be understood as type renaming (in C language, in addition to system-defined standard types and user-defined structures, unions and other types, type descriptions can also be used The statement typedef defines a new type to replace an existing one.)

#include <stdio.h>
//将unsigned int重命名为un_ned,所以nu_ned也是一个类型名
typedef unsigned int un_ned;
int main()
{
    
    
    //add1和add2这两个变量的类型是一样的
    unsigned int add1 = 0;
    un_ned add2 = 0;
    return 0;
}

(4).volatile: Indicates that variables can be implicitly changed during program execution

The C language keyword volatile indicates that the value of a variable may be changed externally, so the access to these variables cannot be cached in a register, and needs to be re-accessed each time it is used. This keyword is often used in a multi-threaded environment, because when writing a multi-threaded program, the same variable can be modified by multiple threads, and the program synchronizes each thread through this variable.
C compiler has no thread concept! At this time, you need to use volatile. The original meaning of volatile means: this value may be changed outside the current thread.

insert image description here
For those of you who love to learn!

Guess you like

Origin blog.csdn.net/plj521/article/details/131037852