List of Keywords in C Language
C language is based on many keywords that act as the “keywords” of the language. These keywords are used to create commands and instructions for the computer to follow and provide a fundamental structure for writing code. It is important for users of this language to know, understand, and be aware of these keywords to write and compile their code correctly.
1: auto
This keyword is used to declare automatic variables, which are local variables that are created and destroyed automatically when a function is called and returned.
int main() {
auto num=2;
printf("the number is: %d\n", num);
{
auto num=6;
printf("the number is: %d\n", num);
}
printf("the number is: %d\n", num);
return 0;
}
The auto keyword is used in this code to print the values of the variable “num” in various scopes. The value of num first changes to 2 globally, then to 6 within brackets, and back to 2 outside of them.
Output
2: if-else
The list of keywords in C Language includes words such as “if”, which is an instruction to perform an action only if a certain condition is met. This condition can range from the value of a variable to a comparison of two values. Another important keyword which is normally used with ‘if’ is “else”. Else instructs the program to take an alternative course of action.
#include <stdlib.h>
int main() {
int n = 7;
if (n % 2 == 0) {
printf("%d is an even number", n);
} else {
printf("%d is an odd number", n);
}
return 0;
}
In this program, the integer is checked to see if it is even or odd, and if it is, the print statement is printed in the if statement; otherwise, the else keyword is used to print the message “n is an odd number“.
Output
3: while
Another keyword often used in C language is “while”. Until a condition is met, a loop, which is a set of instructions, is created using the while operator. This loop is often used to process a set of values or items in a list.
int main(){
int a=7;
while(a<=10){
printf("%d \n",a);
a++;
}
return 0;
}
This program prints the value of the integer until it reaches 10, using the ‘while’ loop.
Output
4: switch, case, break
switch is one of the decision controls statements available in the C language, and it is frequently employed in circumstances when the user must select among many possibilities. In a switch statement, the case keyword is used to specify a particular case. The break keyword is used to stop a loop or switch statement from running.
int main(){
int a = 9;
switch (a) {
case 1: printf("I am One\n");
break;
case 2: printf("I am Two\n");
break;
case 3: printf("I an Three\n");
break;
case 4: printf("I am Four\n");
break;
case 5: printf("I am Five\n");
break;
default: printf("I am default\n");
}
}
This program evaluates the value of a variable and determines which case is appropriate based on the value. As ‘a‘ has a value of 9, the default scenario in this program is for a value of 9, and as a result, the output ‘I am default‘ will be printed.
Output
5: static
static variables can retain their value even after being used outside of their intended context. static variables do not need to be initialized anew in the new scope since they retain their existing value in the previous scope.
int func()
{
static int count = 0;
count++;
return count;
}
int main()
{
printf("%d\n", func());
printf("%d\n", func());
return 0;
}
This program prints the value of the static integer count in the ‘func()’ function, and the value of count will be printed in the main() function showing the scope capacity of the static variable.
Output
6: struct
struct keyword is used to define a structure, which is a user-defined data type consisting of a collection of related variables. A struct is a composite data type declaration that creates a list of variables that are physically grouped together and assigned a single name in a block of memory. By using a single pointer or the struct-declared name, which gives the same location, it is possible to access the different variables.
#include <string.h>
struct Book {
char name[50];
int price;
} Book1;
int main() {
strcpy(Book1.name, "Odessy");
Book1.price = 500;
printf("Name: %s\n", Book1.name);
printf("Price of the Book: %d\n", Book1.price);
return 0;
}
The code creates a structure called “Book” with the characteristic’s “name” and “price” which are then entered in a structure instance before being printed.
Output
7: void
void literally means “empty” or “blank“. The data type void in C is used to show no data. A function that returns nothing in C can also utilize void as the return type. Have a look at the code fragment below, which employs the return type void.
void sum(int a, int b){
printf("The function prints the sum of its parameters: %d", a + b);
}
int main() {
sum(4, 54);
return 0;
}
This program prints the output in the void function ‘sum()’, and then the sum() function is called from the main() function. You can see in this code that the sum() function has no return type.
Output
8: goto
Lastly, “goto” is an instruction that program control directly jumps to a certain target line, disregarding any following code in-between.
int main()
{
int num,a=1;
printf("Enter the number to print the table?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,a,num*a);
a++;
if(a<=10)
goto table;
}
This program prints the table of the number entered by the user, which was 6, using the ‘goto’ statement.
Output
9: const
Constant variables can be declared by using the “const” keyword before the variable’s data type. Just one initialization can be made for the constant variables.
int main() {
const int y = 3;
printf("\nThe value of variable y : %d", y);
return 0;
}
In this code, we are initializing a constant integer 3, and then printing its value using the printf() statement.
Output
10: int
The keyword “int” is used in a type declaration in the C programming language to assign an integer type to a variable. The type’s ability to represent integers does not, however, imply that it can represent all integers. The C implementation you pick will decide the fixed size of an int variable.
int main() {
int num = 32;
printf("\nThe value of num : %d", num);
return 0;
}
In this code, we are initializing an integer ‘num’ 32, and then printing its value using the printf() statement.
Output
Some other keywords that are used in C are:
Keywords | Description |
---|---|
char | Char is used to declare a character data type. |
enum | Enum declare enumeration types. |
extern | Extern keyword declares an external linkage a variable or a function might have outside its file. |
unsigned | Unsigned is a type modifier that changes the meaning of a base data type to produce a new type. Its value ranges from 0 to 65535. |
volatile | Volatile keyword declares volatile objects. |
short | Short is a type modifier that changes the meaning of a base data type to produce a new type. Its value ranges from -32768 to 32767. |
typedef | Typedef is used to associate a type with an identifier explicitly. |
union | Union groups different variables of different data types under a single name. |
sizeof | Sizeof tells the size of a constant or a variable. |
signed | Signed is a type modifier that changes the meaning of a base data type to produce a new type. Its value ranges from -32768 to 32767. |
register | Register is used to introduce register variables. |
long | Long is a type modifier that changes the meaning of a base data type to produce a new type. Its value ranges from -2147483648 to 214743648. |
for | For is a keyword used to define a for loop. |
float | Float is used to declare a decimal number with fewer digits. |
double | Double is used to declare a decimal number with twice as precision as float. |
do | Do is a keyword used to specify the statements that are to be executed when the condition is true in the While loop. |
Conclusion
C language is an extremely versatile language, and its keyword list makes up the basis of its syntax. Understanding and mastering this list of keywords will enable users to write efficient, clear, and reliable code that can suit their needs. Thus, it is important for any aspiring or existing developers to have a thorough understanding of the list of keywords in C language.