How should the C language keywords auto, case, default, and switch be used

Foreword: This article is mainly used for personal review, the pursuit of simplicity, thank you for your reference, communication and handling, and may continue to be revised and improved in the future.

Because it is a personal review, there will be some compression and omission.

One, auto

1. In the c language, the keyword auto is used to declare a variable as an automatic variable. Automatic variables are also called local variables. All local variables are auto by default, and are generally omitted and not written.

Using auto to declare global variables will cause problems. 

2. In C language, only use auto to declare variables, and the default type is integer

二、switch,case,default,break

#include <stdio.h>

int main()
{
	int a = 0;
	scanf("%d", &a);

	switch (a)
	{
		case 0:
			printf("0\n");
            break;
		case 1:
			printf("1\n");
            break;
		case 2:
			printf("2\n");
            break;
		default:
			printf("xxx\n");
            break;
	}
	return 0;
}

The switch statement is a branch statement, switch(), the parentheses of switch can only contain integer expressions.

When the conditions in the parentheses after the switch are met, the statement block after the corresponding case statement can be executed. If break is not written, it will continue to execute until the switch statement ends or a break or return is encountered. (This picture is input 0)

 

The default statement can be placed anywhere in the switch statement. When the conditional judgment of the case statement does not match the conditions after the switch parentheses, the default statement is executed.

The break statement will be discussed later with the continue statement, but here it only stands for jumping out of the switch statement.

Guess you like

Origin blog.csdn.net/weixin_60320290/article/details/124103571