[牛人杂谈] C99的关键字

[复制链接]
1161|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 编程中使用每个列出的关键字的简要示例。
  1. // auto
  2. void function() {
  3.     auto int localVar = 0; // localVar has automatic storage duration, typical for local variables.
  4. }

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

  9. // case, default, switch
  10. int num = 2;
  11. switch(num) {
  12.     case 1:
  13.         // Code for case 1
  14.         break;
  15.     case 2:
  16.         // Code for case 2
  17.         break;
  18.     default:
  19.         // Code if no case matches
  20.         break;
  21. }

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

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

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

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

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

  39. // else, if
  40. int a = 5;
  41. if(a > 10) {
  42.     // Code if condition is true
  43. } else {
  44.     // Code if condition is false
  45. }

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

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

  51. // for
  52. for(int i = 0; i < 10; i++) {
  53.     // Loop code here
  54. }

  55. // goto
  56. label:
  57.     // Code to jump to
  58. goto label; // Jumps to the label.

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

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

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

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

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

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

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

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

  80. // struct
  81. struct Person {
  82.     char name[50];
  83.     int age;
  84. };
  85. struct Person person1; // Declares a variable of type Person.

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

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

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

  98. // void
  99. void myFunction() {
  100.     // A function that returns no value.
  101. }

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

  104. // while
  105. int i = 0;
  106. while(i < 5) {
  107.     // Code to execute while i is less than 5.
  108.     i++;
  109. }



您需要登录后才可以回帖 登录 | 注册

本版积分规则

158

主题

1732

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部