C programming Keywords: A list and Explanation of Usage
Keywords in C refer to a set of reserved words with predefined meanings that are used to write programs in the C programming language. These keywords cannot be used as identifiers or variable names, as they have a specific function within the language. There are many C programming Keywords.In this aticle all keywords are explained with programming examples.
Keywords play an essential role in any programming language as they define the structure or implementation of any program. The important part is to know about keywords that will aid the expertise of several imperatives in programming to the learner. In this article, we will let you see the list of keywords in c programming with their usage and explanation.
int | volatile | void | unsigned | union | typedef | switch | struct |
for | sizeof | signed | short | auto | while | long | register |
if | goto | static | float | extern | enum | else | double |
do | default | const | continue | char | case | break | return |
All these words are the 32 reserved keywords of c language. Its usage makes the programming language readily and more convenient to understand.
Keywords are predefined. These reserved words have special meanings to the compiler. Let’s dive deep into the keywords with their explanation and usage.
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
Reserved keywords
1. while
While is a loop that iterates a specific block of code until a particular condition turns true.
To get the picture of the while loop, let’s move on to its flowchart.
C program to demonstrate the use of while keyword
#include<stdio.h> int main(){ int x=1; //initializing the value of x//loop will execute until the value of x turns greater than 15while(x<=15){ printf("%d \t",x); x++; //in each iteration ++operator increment the value of x by 1.} return 0; }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Explanation
In the above example, the value of x is initialized as 1, and the control of the program jumps to the expression of while; if it is true, then it runs the code written in while till the condition fails.
2. static
A keyword that can be used with variables and functions in c programming is the static keyword. While ordinary variable has their scope limited to a definite extent, the scope of static variable is limited throughout the program.
Syntax of static keyword for variable
Static datatype variable_name;
Syntax of static keywords for functions
Static type_of_function function_name()
3. if
In c programming,” if” is a decision-making statement that is used when a condition is evaluated as true only.
If the condition mentioned in the if block evaluates as true, then the code written in the if block gets executed.
Example –
#include <stdio.h>int main(){ int u = 500; int v = 1000; if (u<v) { printf(" v is greater than u"); } return 0;}
Output
v is greater than u
4. else
else keyword is used with if in the decision-making statement of c programming. If the condition evaluates as true code of the if block will execute and in the case of false, the else part will execute.
Example
#include <stdio.h>int main(){ int u,v; printf("enter two values"); scanf("%d%d",&u,&v); if (u<v) { printf(" %d is greater than %d",v,u); } else { printf("%d is less than %d",v,u); }}
Output
enter two values20
4
4 is less than 20
Also read: If-else Statement in C
5. volatile
volatile keyword in c programming is a qualifier just like the const keyword. It creates a volatile object that the hardware of the machine can modify. When a variable is described as volatile, it reports to the compiler that its value can be changed at any time without any involvement of the compiler.
The volatile keyword can be used with pointers, register, etc.
Syntax to declare a variable as volatile.
volatile int variable_name;
Int volatile variable_name;
6. sizeof
sizeof is a unary operator in c programming that is used to calculate the size of any operand or data type.
Program to demonstrate the use of sizeof operator
#include <stdio.h>int main(){ printf("with the help of sizeof operator, size of some data types are given below."); printf("%lu\n", sizeof(float)); printf("%lu\n", sizeof(char)); printf("%lu\n", sizeof(double)); printf("%lu", sizeof(int)); return 0;}
Output
with the help of the sizeof operator, the size of some data types is given below.4
1
8
4
7. goto
goto is a jump statement in c programming that is used to shift the control of the program from one part to another part of the program.
Syntax
goto label;
…
…
Label: statement ;
Program to demonstrate the use of goto statement in c
#include <stdio.h> int main() { int n,x=1; //declaring variable printf("enter any number:"); scanf("%d",&n); //taking input from user printf("table of %d is given below:\n",n); table_: printf("%d x %d = %d\n",n,x,n*x); x++; //increment value of x by 1 if(x<=5) goto table_; //shifting the control of the program to label.}
Output-
enter any number:9
table of 99 is given below:
99 x 1 = 99
99 x 2 = 198
99 x 3 = 297
99 x 4 = 396
99 x 5 = 495
8. void
In c programming, void stands for nothing, if a function return no value then it can be declared as a void function. A void function doesn’t accept any parameter and has no value or type.
9. for
In c programming, for is used to iterate a part or code of the program several times as per the condition given in the loop.
Syntax:
for (initialization statement; expression; update statement) {
// code
}
Program to demonstrate the use of for loop
Program to demonstrate the use of for loop #include <stdio.h>int main() { int n, i, sum = 0; printf("how many times do you want to print statement "); scanf("%d", &n); //for loop terminates when n is less than i for(i = 1; i <= n; ++i) { printf("it is for loop.\n");}
Output
how many times do you want to print statement 4
it is for loop.
it is for loop.
it is for loop.
it is for loop.
10. int
int is a keyword that is used to declare the type of a variable. Here int stands for an integer, meaning it can have both positive and negative numbers but no decimal number.
If you use an integer value in your program, the variable must be declared an integer.
Size:4 bytes
Range:-32768 to 32767
Syntax
int variable_name;
int a; -32768 to 32767
Here, a is a variable of integer type.
Int a,b;
One can declare multiple variables as well at a time.
Example
Program to demonstrate the use of int
#include<stdio.h> int main(){ int x; {x=2;printf(" x holds an integer value that is:%d ",x); } return 0; }
Output-
x holds an integer value that is:2
11. signed
the signed keyword is a type modifier used for a variable that can also take negative, zero, and positive integers.
signed char
Size: 1
Range:-128 to 127
12. unsigned
the unsigned keyword is a type modifier that is used for a variable that can take only positive integers and zero.
unsigned int
Size: 4
Range: 0 to 65535
unsigned char
Size: 1 byte
Range:0 to 255
Syntax
unsigned char variable_name= value;
13. short
Considering c programming, short is a keyword that is used to declare a variable that holds the value of a signed integer.
Size: 2 bytes
Range: -32768 to 32767
unsigned short
Size: 2 bytes
Range: 0 to 65535
14. long
the long keyword is a type modifier that is used for a variable that can take integers with bigger values.
Size: 4 bytes
Range: -2147483648 to 214743648
15. extern
In c programming, an extern keyword is used to declare a global variable. Here extern stands for external, a variable declared as extern is a global variable that can be accessed anywhere in the c programming.
Syntax of enum in c programming
extern data_type variable_name;
16. char
char stands for the character, which is a data type. Char allowed a variable to store a single character in it.
Size: 1 byte
Range: -128 to 127
#include"stdio.h" void main() {
char ex_; ex_ = 'a' ; printf("%c \n",ex_); }
Output
a
17. float
Float is a data type used to declare a variable with a decimal value. It represents the floating-point number in c with 6 decimal digits in precision.
Size: 4 bytes
Range:3.4E +/- 38 (7 digits)
example-
#include<stdio.h>#include<string.h>int main() { float var_a = 11.11; printf("The float value : %f",var_a); return 0;}
Output
The float value: 11.110000
18. double
double is a data type that is used to declare a variable that holds 15 decimal digits in precision. Additionally, it represents the floating-point number in c.
Size – 8 bytes
Range 1.7E-308 to 1.7E+308
Syntax
double name_of_variable;
Example
#include<stdio.h>#include<string.h>int main() { double var_a = 114.1156; printf("The double value : %f",var_a); return 0;}
Output
The double value: 114.115600
19. const
In c programming, any variable that is declared as const its value will not be changed in the execution of the program. In simple words, the value of a variable is constant in the whole program, and it can’t be changed.
Program to demonstrate the use of the const keyword in c
#include<stdio.h> int main(){ const int my_number=626; printf("my favourite number is: %d",my_number); return 0; }
Output
my favourite number is: 626
20. typedef
typedef is a keyword in c programming that can be used with arrays, structures, and pointers. Typedef is used to redefine the existing data type’s name for user convenience.
Syntax
typedef data_type New_name;
21. register
If a variable is stored in a register instead of a CPU, then it is easy to access. With the use of the register keyword, one can store the frequently used variable in the register for faster accessibility.
Syntax
register data_type variable_name;
22. enum
In c programming, an enum is a data type defined by the user that is used to allow names to the constant integrals or integers. Enum is the short term for the word enumeration.
Syntax to declare an enum
enum name_of_enum{constant1,constant2,constant3,constantn};
23. switch
One can run a specific code using a switch case statement among different available choices. The switch statement takes an expression(must be an integer or character), and if it evaluates as true, then the case corresponding to that value gets executed.
Flowchart
Program to choose any subject using switch case
#include <stdio.h>int main() { char a; //declaring character variable printf("Choose any category between (A, B): "); scanf("%c", &a); switch(a) { case 'A': printf("You have selected biotechnology"); break; case 'B': printf("You have selected computer science"); break; // value of a doesn't match any case constant that is A, B default: printf("oops! invalid choice"); } return 0;}
Output-
Choose any category between (A, B): B
You have selected computer science
Output-
Choose any category among (A, B): C
oops! invalid choice
24. case
The case is a keyword in c that is used with the switch statement.
The switch statement takes arguments, and if it evaluates as true, the case corresponding to that value gets executed.
Refer to the above example of the switch keyword to understand the case in c programming.
25. default
default keyword is used in the switch case, which is the decision control statement of c programming. Considering the switch statement, when an expression entered by the user doesn’t match any case, the code under the default statement gets executed.
In the example of the switch case, the default statement executes when the user enters C, which is an invalid choice.
26. break
break keyword is used to stop the execution of code block in switch case and terms of the loop like for, do, while it terminates the loop by ending the execution of specific code block.
Example
#include<stdio.h> #include<stdio.h> void main () { int x; for(x = 0; x<15; x++) { printf("%d ",x); if(x == 11) break; } printf("\n when the value of x turns 11, the loop ends here!!"); }
Output
0 1 2 3 4 5 6 7 8 9 10 11
when the value of x turns 11, the loop ends here!!
27. continue
The continue keyword is used as the loop control statement; if the condition mentioned in the loop is evaluated as true, then the continue keyword compels the program’s control to run the next iteration of the loop. With the use of the continue keyword, lines of the code can be skipped.
28. union
union is a data type available in c programming that aids in storing different data types at the same location in memory. It is similar to structure, while it has different memory locations for different data types.
Syntax of union
union name_of_union{ datatype variable1; datatype variable;};
29. do
Concerning c programming, do is a keyword that is used with the while as a control flow statement. Do- while loop is used when the user wants to execute a loop at least once.
Syntax
do{ statements;}while(condition);
30. struct
struct stands for structure, in which we can group the variable of different data types like int, float, char, double, etc.
In terms of an array is a collection of similar types of data elements, while a structure is a collection of different types of data elements. In structure, different variables of the different data types are called the structure’s members.
Using the struct keyword, one can create the structure.
Syntax
struct structure_name{ //structure declaration int variable_name; //members of structure char variable_name; double variable_name;};
Program to demonstrate the use of the struct keyword
#include<stdio.h>struct structExample { int a; char c;};
int main() { // Create a structure variable of myStructure called s //to access the members of structExample using the "." operator struct structExample s; s.a = 100; s.c = 'z'; //Assigning values to the member of structure structExample
// Print values printf("my favourite number %d\n", s.a); printf("My favourite letter: %c\n", s.c);
return 0;}
Output
my favourite number 100
My favourite letter: z
31. return
In c programming, return is a keyword that ends or terminates a function and returns the desired output. When a function is ready to return the result to the function calling it, the return keyword is used.
#include <stdio.h> void print_() // void method print_(){ printf("Return Statement is executed"); } int main() // main function of the program { print_(); // Calling print_() function return 0; }
Example
A program to demonstrate the use of return keyword in c
#include <stdio.h>
void print_() // void method print_()
Output
Return Statement is executed
32. auto
auto is a keyword in c programming. Auto stands for automatic, which declares the variable automatically. An auto variable has the automatic storage duration the same as local variables.
Syntax of auto in c programming
auto int variable_name;
Here int is the data type of variable, and auto is the storage class that belongs to it.
Example-
#include<stdio.h>int main(){ auto int n = 5; { auto int n = 20; printf(" num2 %d", n); } printf("\n"); printf("num1 %d", n); return 0;}
Output
num2 20
num1 5
Conclusion
In a nutshell, all these keywords and their usage are essential in building great programming skills. This article will aid support to your c programming learning journey.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio