如何在 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++;
}
|