Updated March 17, 2023
Introduction to C Keywords
Keywords are known as predefined as well as reserved words that are used in programming holding special meanings to the compiler. They are part of the syntax; we cannot use them as an identifier. Since C is a case-sensitive language, all of the keywords must be written in lowercase format. Below we have a list of all keywords that are allowed in ANSI C.
C Keywords with Examples
Below we have discussed all of them along with their examples.
1. Volatile
This keyword is needed so as to create volatile objects. These volatile objects have the ability to get modified in the unknown or unmentioned method through hardware.
Example:
const volatile ex;
- In this example, ex is considered to be the volatile object.
- The program does not have the ability to change ex since it is a constant variable. But, hardware has the ability to modify it as ex is considered a volatile object.
2. auto
This keyword is used to declare the automatic variables.
Example:
auto int v1;
- This particular definition means that v1 is considered to be the variable of class type storage and data type of int.
- Variables that we declare inside the function bodies are by default considered to be automatic. Every time a function is executed, they get recreated.
- Automatic variables are also called local variables as they are local to the function.
3. char
char keyword is used to declare the character variable. Variables that are of type char are of 1-byte length. They can get signed (it is by default unless we use the compiler option ‘-funsigned-char’ or ‘unsigned’), which implies they have got a range of -128 to 127 and 0 to 255, respectively.
Example:
char a;
In this example, a is considered to be a character type variable.
4. double and float
Both keywords double, as well as float, are needed for declaration of floating type variables.
Example:
float a;
double b;
In this example, a is considered to be the single-precision floating type variable and b is considered to be the double-precision floating type variable.
5. const
We van declare an identifier to be constant through the usage of the const keyword.
Example:
const int r = 5;
6. if and else
We use if and else so as to make decisions in C programming.
Example:
if(a==1)
printf(" a is 1.");
else
printf(" a is not 1.");
In case the value of a is other than 1,
output:
a is not 1.
7. break and continue
The break statement would make the program jump out of the most inner and enclosing loop in an explicit manner. The continue is used for statements skipping certain statements that are inside the loop.
Example:
for (a=1; a<=10; ++a)
{
if (a==3)
continue;
if (a==7)
break;
printf("%d ", a);
}
Output :
1 2 4 5 6
In this example, whenever a is equal to 3, then the continue statement would play its role and skip 3. Whereas whenever a is equal to 7, the break statement would play its role and terminate for a loop.
8. enum
In C programming enumeration types get declared through keyword enum.
Example:
enum deck
{
kings;
queens;
jokers;
};
In this example, an enumerated variable deck is created by having the tags: kings, queens, and jokers.
9. extern
The extern keyword indicates that the identifier has benn defined somewhere else. It also indicates that in fact storage as well as the initial value, or function body has been defined somewhere else, mostly in the separate module.
- Format : extern data-definition;extern function-prototype;
Example:
extern int f;extern void factorial (int a);
10. return
Return is used for exiting the function. It would exit from the current function that is executing immediately and return to the calling routine. It can optionally return value too.
Syntax:
return [expression];
Example:
int ret (int a){ return (a*a);}
11. sizeof
sizeof is used for returning the size of expression or type of it. It is used for returning the size in bytes.
Syntax:
sizeof expressionsizeof (type)
Example:
a = calloc (100, sizeof (int));
12. goto
goto keyword is needed for the transfer of control from one position to another.
Syntax:
goto identifier;
Control gets transferred unconditionally to the location of a local label that is specified by the identifier.
Example:
a: ... goto a;
13. int
int keyword is used for the declaration of the integer type variable.
Example:
int b;
In this example, b is an integer variable.
14. register
This keyword is used for the creation of the register variables that are much faster as compared to the normal variables.
Example:
register int r1;
15. static
This keyword is used for the creation of a static variable. The static variables’ values persist until the end of the program. It tells that the function or the element is only known inside the scope of the current compilation. Also, if we use the static keyword along with the variable which is local to the function, it would allow the last value of the variable to get preserved in successive calls to that function.
Example:
static int v;
16. struct
struct keyword is used for the declaration of the structure. The structure is used for holding the variables of varied data types under one name. Just like the union, it groups the variables into a single record. Also, the struct-type-name is considered to be the optional tag name which points to structure type. The variables of a structure are data definitions, and they are optional. Although both are optional, one of the two must appear.
Example:
struct st {
char n [70];
float m;
int a;
} st1, st2;
17. union
Union keyword is needed for grouping the varied types of a variable under one name.
union st
{
char n[60];
float m;
int a;
}
18. void
This keyword denotes that the function won’t be returning any value.
Example:
void test(int a)
{
.....
}
In this example, the function test() doesn’t return a value as the return type is void.
19. typedef
This keyword is required so as to associate a type along with an identifier in an explicit manner.
Example :
typedef float k;
k lion, tiger;
20. short, long, signed and unsigned
The short, long, signed as well as unsigned keywords are the type of modifiers which alters the meaning of the base data type in order to yield the new type.
Example:
short int smallI;
long int big;
signed int normal;
unsigned int positive;
The valid range of valid data types is listed in the following table :
signed char | -128 to 127 |
signed int | -32768 to 32767 (signed is default) |
signed short int | -32768 to 32767 |
signed long int | -2147483648 to 2147483647 |
unsigned char | 0 to 255 |
unsigned int | 0 to 65535 |
unsigned short int | 0 to 65535 |
unsigned long int | 0 to 4294967295 |
21. for
In total, there exist 3 kinds of loops in C. The for loop in C is written using the keyword for.
Example:
for (a=0; a< 9; ++a)
{
printf("%d ", a);
}
Output
0 1 2 3 4 5 6 7 8
22. switch, case and default
We use switch as well as case statements whenever the block of statements needs to be executed among various blocks.
Example:
switch(expression)
{
case '1':
// statements to execute when 1
break;
case '8':
// statements to execute when 5
break;
default:
// statements to execute when default;
}
23. Do-while loop
do is used along with a while to make a different form of repetition of the statement. This form of the loop makes use of the following syntax:
do statement while (expression)
Example :
a = 1; b = 1;do { b *= a; a++; } while (a <= factorial);
24. while
while it is used for repeating the execution when the condition is true.
Syntax:
while (expression) statement
Example :
while (*z == ' ') z++;
Conclusion
Thus we can conclude that there is a total of 32 keywords, and a constant is a value that doesn’t change throughout the program. Also, a keyword is reserved words by language. Four commonly used data types exist: int, float, char, and void. Each of the data types differs in size as well as range from each other.
Recommended Articles
This is a guide to C Keywords. Here we discuss the introduction and top different Keywords of C with syntax and Examples. You may also look at the following articles to learn more –