English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This tutorial briefly introduces all the keywords in C programming.32keywords.
Keywords in C Programming | |||
---|---|---|---|
auto | break | case | char |
const | continue | default | do |
double | else | enum | extern |
float | for | goto | if |
int | long | register | return |
short | signed | sizeof | static |
struct | switch | typedef | union |
unsigned | void | volatile | while |
The auto keyword declares an automatic variable. For example:
auto int var1;
This statement indicates var1is a variable of storage class auto, type int.
By default, variables declared in the function body are automatic. They are recreated each time the function is executed.
Since automatic variables are local variables of the function, they are also called local variables. To learn more information, please visitC Storage Class.
The break statement immediately terminates when it encounters the innermost loop. It is also used to terminate switch statements.
The continue statement skips the following statements in the loop for iteration.
for (i=1;i<=10;++i){ if (i==3) continue; if (i==7) break; printf("%d ", i); }
Output result
1 2 4 5 6
when i equals3the continue statement takes effect and skips3. When i equals7The break statement takes effect and terminates the for loop. To learn more information, please visitC break and continue statements
When a statement block must be executed in multiple blocks, use switch and case statements. For example:
switch(expression) { case '1': //When1Execute some statements break; case '5': //When5Execute some statements break; default: //Some statements to be executed by default }
AccessC switch statementto learn more information.
The char keyword declares a character variable. For example:
char alphabet;
Here, alphabet is a character type variable.
For more information, please visitC Data Types.
You can use the const keyword to declare identifiers as constants.
const int a = 5;
For more information, please visitC Variables and Constants.
int i; do { printf("%d ", i); i++; } while (i<10)
For more information, please visitC do ... while loop
The keywords double and float are used to declare floating-point type variables. For example:
float number; double longNumber;
Here, number is a single-precision floating-point variable, and longNumber is a double-precision floating-point variable.
For more information, please visitC Data Types.
In C language programming, if and else are used for decision-making.
if (i == 1) printf("i is 1.") else prinf("i is not 1.")
If the value of i is not1, then the output is:
i is not 1
For more information, please visitC if ... else statement.
Enumeration types are declared using the enum keyword in C language programming. For example:
enum suit { hearts; spades; clubs; diamonds; };
In this case, create an enumeration variable suit containing labels: hearts, spades, clubs, and diamonds.
For more information, please visitC enum.
The extern keyword declares that a variable or function has external linkage outside the declared file.
For more information, please visitC Storage Types.
There are three types of loops in C programming. The for loop is written using the C language keyword for. For example:
for (i=0; i< 9;++i){ printf("%d ", i); }
Output result
0 1 2 3 4 5 6 7 8
For more information, please visitC for loop.
The goto statement is used to transfer control of the program to a specified label. For example:
for(i=1; i<5; ++i) { if (i==10) goto error; } printf("i is not10;"); error: printf("Error, the count cannot be10;");
Output result
Error, the count cannot be10.
For more information, please visitC goto.
The int keyword is used to declare integer type variables. For example:
int count;
Here, count is an integer variable.
For more information, please visitC Data Types.
The keywords short, long, signed, and unsigned are type modifiers, which can change the meaning of basic data types to produce new types.
short int smallInteger; long int bigInteger; signed int normalInteger; unsigned int positiveInteger;
Data type | range |
---|---|
short int | -32768 to 32767 |
long int | -2147483648 to214743648 |
signed int | -32768 to 32767 |
unsigned int | 0 to 65535 |
The return keyword terminates the function and returns a value.
int func() { int b = 5; return b; }
The function func() will5Return to the calling function. For more information, please visitC User-Defined Function.
The sizeof keyword evaluates the size of data (variable or constant).
#include <stdio.h> int main() { printf("齝es.", sizeof(char)); }
For more information, please visitC Operators.
Output result
1 bytes.
The register keyword creates a register variable that is much faster than a normal variable.
register int var1;
The static keyword creates a static variable. The value of a static variable persists until the end of the program. For example:
static int var;
The struct keyword is used to declare a structure. A structure can save different types of variables under a single name.
struct student{ char name[80]; float marks; int age; }s1, s2;
For more information, please visitC structure.
The typedef keyword is used to explicitly associate a type with an identifier.
typedef float kg; kg bear, tiger;
Unions are used to group different types of variables under a single name.
union student { char name[80]; float marks; int age; }
For more information, please visitC union.
The void keyword indicates that there is no value or no value at all.
void testFunction(int a) { ..... }
In this case, the testFunction() function cannot return a value because its return type is void.
The volatile keyword is used to create volatile objects. Volatile objects can be modified by the hardware in an unspecified way.
const volatile number
Here, number is a volatile object.
Since number is a constant, the program cannot change it. However, since it is a volatile object, the hardware can change it.