C语言常见关键字怎么使用

发布时间:2022-06-14 13:59:52 作者:iii
来源:亿速云 阅读:194

C语言常见关键字怎么使用

C语言作为一种广泛使用的编程语言,其关键字(Keywords)是语言的核心组成部分。关键字是预定义的保留字,具有特定的含义和用途,不能用作变量名、函数名或其他标识符。本文将介绍C语言中常见的关键字及其使用方法。

1. auto

auto关键字用于声明自动变量,即局部变量。在C语言中,局部变量默认是auto类型的,因此通常不需要显式使用auto关键字。

void example() {
    auto int x = 10;  // 等同于 int x = 10;
    printf("%d\n", x);
}

2. break

break关键字用于跳出循环或switch语句。在循环中,break会立即终止循环;在switch语句中,break会跳出当前的case分支。

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;  // 当i等于5时,跳出循环
    }
    printf("%d\n", i);
}

3. case

case关键字用于switch语句中,表示一个分支。case后面通常跟一个常量表达式,表示当switch表达式的值与该常量相等时,执行该分支的代码。

int x = 2;
switch (x) {
    case 1:
        printf("x is 1\n");
        break;
    case 2:
        printf("x is 2\n");  // 输出 "x is 2"
        break;
    default:
        printf("x is neither 1 nor 2\n");
}

4. char

char关键字用于声明字符型变量。字符型变量通常用于存储单个字符,占用1个字节的内存空间。

char ch = 'A';
printf("%c\n", ch);  // 输出 'A'

5. const

const关键字用于声明常量,表示该变量的值在程序运行期间不能被修改。

const int MAX_VALUE = 100;
// MAX_VALUE = 200;  // 错误:不能修改常量

6. continue

continue关键字用于跳过当前循环的剩余部分,直接进入下一次循环的迭代。

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue;  // 跳过偶数
    }
    printf("%d\n", i);  // 输出奇数
}

7. default

default关键字用于switch语句中,表示当所有case分支都不匹配时执行的代码。

int x = 3;
switch (x) {
    case 1:
        printf("x is 1\n");
        break;
    case 2:
        printf("x is 2\n");
        break;
    default:
        printf("x is neither 1 nor 2\n");  // 输出 "x is neither 1 nor 2"
}

8. do

do关键字用于do-while循环,表示先执行循环体,再检查循环条件。do-while循环至少会执行一次。

int i = 0;
do {
    printf("%d\n", i);
    i++;
} while (i < 5);

9. double

double关键字用于声明双精度浮点型变量。双精度浮点数通常占用8个字节的内存空间,精度高于单精度浮点数。

double pi = 3.141592653589793;
printf("%f\n", pi);

10. else

else关键字用于if语句中,表示当if条件不成立时执行的代码。

int x = 10;
if (x > 20) {
    printf("x is greater than 20\n");
} else {
    printf("x is less than or equal to 20\n");  // 输出 "x is less than or equal to 20"
}

11. enum

enum关键字用于定义枚举类型。枚举类型是一种用户定义的数据类型,用于表示一组命名的整数常量。

enum Color { RED, GREEN, BLUE };
enum Color c = GREEN;
printf("%d\n", c);  // 输出 1

12. extern

extern关键字用于声明外部变量或函数,表示该变量或函数在其他文件中定义。

// file1.c
int globalVar = 10;

// file2.c
extern int globalVar;
printf("%d\n", globalVar);  // 输出 10

13. float

float关键字用于声明单精度浮点型变量。单精度浮点数通常占用4个字节的内存空间。

float pi = 3.14f;
printf("%f\n", pi);

14. for

for关键字用于for循环,表示一个循环结构。for循环通常用于已知循环次数的情况。

for (int i = 0; i < 5; i++) {
    printf("%d\n", i);
}

15. goto

goto关键字用于无条件跳转到程序中的某个标签处。goto语句通常不推荐使用,因为它会使程序的控制流变得难以理解。

int i = 0;
start:
    printf("%d\n", i);
    i++;
    if (i < 5) {
        goto start;
    }

16. if

if关键字用于条件判断,表示当条件成立时执行相应的代码。

int x = 10;
if (x > 5) {
    printf("x is greater than 5\n");  // 输出 "x is greater than 5"
}

17. int

int关键字用于声明整型变量。整型变量通常占用4个字节的内存空间,用于存储整数。

int x = 10;
printf("%d\n", x);

18. long

long关键字用于声明长整型变量。长整型变量通常占用8个字节的内存空间,用于存储较大的整数。

long x = 1000000000L;
printf("%ld\n", x);

19. register

register关键字用于建议编译器将变量存储在CPU寄存器中,以提高访问速度。现代编译器通常会自动优化,因此register关键字的使用已经较少见。

register int x = 10;
printf("%d\n", x);

20. return

return关键字用于从函数中返回值,并结束函数的执行。

int add(int a, int b) {
    return a + b;
}
int result = add(3, 4);  // result = 7

21. short

short关键字用于声明短整型变量。短整型变量通常占用2个字节的内存空间,用于存储较小的整数。

short x = 100;
printf("%d\n", x);

22. signed

signed关键字用于声明有符号的整型变量。默认情况下,intshortlong等类型都是有符号的。

signed int x = -10;
printf("%d\n", x);

23. sizeof

sizeof关键字用于获取变量或数据类型的大小(以字节为单位)。

int x = 10;
printf("%lu\n", sizeof(x));  // 输出 4(假设int类型占用4个字节)

24. static

static关键字用于声明静态变量或函数。静态变量在程序运行期间只初始化一次,且其生命周期贯穿整个程序运行期间。

void example() {
    static int count = 0;
    count++;
    printf("%d\n", count);
}
example();  // 输出 1
example();  // 输出 2

25. struct

struct关键字用于定义结构体类型。结构体是一种用户定义的数据类型,可以包含多个不同类型的成员。

struct Point {
    int x;
    int y;
};
struct Point p = {10, 20};
printf("%d, %d\n", p.x, p.y);  // 输出 10, 20

26. switch

switch关键字用于switch语句,表示一个多分支选择结构。switch语句根据表达式的值跳转到相应的case分支。

int x = 2;
switch (x) {
    case 1:
        printf("x is 1\n");
        break;
    case 2:
        printf("x is 2\n");  // 输出 "x is 2"
        break;
    default:
        printf("x is neither 1 nor 2\n");
}

27. typedef

typedef关键字用于为已有的数据类型定义新的类型别名。

typedef int Integer;
Integer x = 10;
printf("%d\n", x);

28. union

union关键字用于定义联合体类型。联合体是一种特殊的数据类型,允许在相同的内存位置存储不同的数据类型。

union Data {
    int i;
    float f;
    char str[20];
};
union Data data;
data.i = 10;
printf("%d\n", data.i);  // 输出 10

29. unsigned

unsigned关键字用于声明无符号的整型变量。无符号整型变量只能存储非负整数。

unsigned int x = 10;
printf("%u\n", x);

30. void

void关键字用于表示无类型。void通常用于函数的返回类型或参数列表,表示函数不返回任何值或不需要参数。

void printHello() {
    printf("Hello, World!\n");
}
printHello();

31. volatile

volatile关键字用于声明易变变量,表示该变量的值可能会在程序运行期间被意外修改(例如硬件寄存器)。编译器不会对volatile变量进行优化。

volatile int x = 10;
printf("%d\n", x);

32. while

while关键字用于while循环,表示一个循环结构。while循环在每次迭代前检查循环条件,如果条件成立则继续循环。

int i = 0;
while (i < 5) {
    printf("%d\n", i);
    i++;
}

总结

C语言的关键字是编写程序的基础,理解并正确使用这些关键字对于编写高效、可靠的C程序至关重要。本文介绍了C语言中常见的关键字及其使用方法,希望能帮助读者更好地掌握C语言的编程技巧。

推荐阅读:
  1. C语言存储类关键字
  2. C语言中有哪些关键字

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言

上一篇:GoFrame gredis怎么缓存DoVar Conn连接对象实现自动序列化

下一篇:ConditionalOnProperty配置swagger不生效如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》