打印
[牛人杂谈]

C99的关键字

[复制链接]
263|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
小灵通2018|  楼主 | 2024-3-15 15:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
C 语言中的基本结构、决策和循环相关的关键字 (C99):

基本结构、决策和循环的关键字
auto:建议变量的自动存储持续时间。
break:从循环或 switch 语句中退出。
case:在 switch 语句中定义分支。
char:定义字符类型变量。
const:使任何变量或指针不可修改。
continue:跳过剩余的循环体并继续进行下一次迭代。
default:指定 switch 语句中的默认分支。
do:与 while 一起使用以创建 do-while 循环。
double:定义具有双精度浮点类型的变量。
else:在 if-else 语句中指定备用路径。
枚举:定义枚举,这是一种对命名整数常量进行分组的方法。
float:定义具有单精度浮点类型的变量。
for:启动 for 循环。
goto:跳转到程序中带标签的语句。
if:启动用于决策的 if 语句。
int:声明一个整数类型变量。
long:定义长整型类型变量。
register:建议将变量存储在 CPU 寄存器中。
return:退出函数并选择性地返回一个值。
short:定义短整数类型变量。
signed:指定变量可以保存负值和正值(整数类型的默认值)。
sizeof:确定类型或变量的大小(以字节为单位)。
static:即使在退出作用域后仍保留变量值。
struct:定义一个结构,一种对变量进行分组的自定义数据类型。
switch:启动用于多路径决策的 switch 语句。
typedef:为现有类型创建新名称(别名)。
union:定义一个联合,一种特殊的数据类型,使你能够在同一内存空间中存储不同的数据类型。
unsigned:指定变量只能保存非负值。
void:指定函数不返回任何值或声明泛型指针。
volatile:指示变量可以以程序未明确指定的方式进行更改。
while:启动 while 循环。
这些关键字是 C 语言编程的基础,使您能够定义变量、使用循环和决策结构控制流等。


使用特权

评论回复
沙发
小灵通2018|  楼主 | 2024-3-15 15:05 | 只看该作者
如何在 C 编程中使用每个列出的关键字的简要示例。
// auto
void function() {
    auto int localVar = 0; // localVar has automatic storage duration, typical for local variables.
}

// break
for(int i = 0; i < 10; i++) {
    if(i == 5) break; // Exits the loop when i equals 5.
}

// case, default, switch
int num = 2;
switch(num) {
    case 1:
        // Code for case 1
        break;
    case 2:
        // Code for case 2
        break;
    default:
        // Code if no case matches
        break;
}

// char
char letter = 'A'; // Declares a character variable.

// const
const int MAX = 100; // MAX is a constant, its value cannot be changed.

// continue
for(int i = 0; i < 10; i++) {
    if(i % 2 == 0) continue; // Skips the rest of the loop body for even numbers.
    // Odd numbers code here
}

// do, while
int count = 0;
do {
    // Code to execute
    count++;
} while(count < 5); // Loops until count is less than 5.

// double
double pi = 3.14159; // Declares a double-precision floating-point variable.

// else, if
int a = 5;
if(a > 10) {
    // Code if condition is true
} else {
    // Code if condition is false
}

// enum
enum colors {red, green, blue};
enum colors shirt = red; // Assigns the first value of enum colors to shirt.

// float
float temperature = 36.6f; // Declares a single-precision floating-point variable.

// for
for(int i = 0; i < 10; i++) {
    // Loop code here
}

// goto
label:
    // Code to jump to
goto label; // Jumps to the label.

// int
int age = 30; // Declares an integer variable.

// long
long distance = 1234567890L; // Declares a long integer variable.

// register
register int fastVar = 5; // Suggests that fastVar should be stored in a register for faster access.

// return
int add(int x, int y) {
    return x + y; // Returns the sum of x and y.
}

// short
short height = 170; // Declares a short integer variable.

// signed
signed int temperature = -20; // Explicitly declares a signed integer.

// sizeof
int size = sizeof(int); // Gets the size of int type in bytes.

// static
void counter() {
    static int count = 0; // count retains its value between function calls.
    count++;
}

// struct
struct Person {
    char name[50];
    int age;
};
struct Person person1; // Declares a variable of type Person.

// typedef
typedef unsigned int uint;
uint counter = 100; // Uses uint as an alias for unsigned int.

// union
union Data {
    int i;
    float f;
    char str[20];
};
union Data data; // Declares a variable of type Data, which can hold an int, a float, or a char array.

// unsigned
unsigned int positive = 300; // Declares an unsigned integer, ensuring it's non-negative.

// void
void myFunction() {
    // A function that returns no value.
}

// volatile
volatile int shared = 0; // Tells the compiler that the value of shared can be changed unexpectedly.

// while
int i = 0;
while(i < 5) {
    // Code to execute while i is less than 5.
    i++;
}



使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

117

主题

1444

帖子

4

粉丝