Keywords in C Programming with List and Explanation

Keywords used in the C programming language are building blocks that can define the structure and also the functionality of a program. These keywords are also reserved keywords that have some special meaning, and one cannot use them as variable names or identifiers.

It is important to learn how keywords in C, like if, return, and int, work as they control the flow of your program. They also define data types and manage memory.

Let's discuss here all types of keywords in C and why these keywords are essential while writing code.

Blogging Illustration

Keywords in C Programming with List and Explanation

image

What Are Keywords in C?

We will first look at the exact meaning of keywords used in C.

These keywords are one of the reserved words that have some predefined meaning and also function within the C language. One cannot use these words as variable names, identifiers, pr function names as they are integral to the syntax of the language.

They also help in defining the structure of a code, control flow of the program, manage memory, and also specify the data types sling with that.

In the C programming language, int, return, and if are commonly used keywords. Each of these keywords has a specific set of purposes, and one should know how to use them correctly for writing C programs.

List of Keywords in C

There are in total of 32 keywords in the C language. Below in the table, these keywords are mentioned along with their meaning, which will help you know exactly when to use what keyword :

KeywordMeaning
autoThis will declare the automatic local type of variables
BreakJust excerpts from the loops or any other switch statements in particular.
caseWill help.In defining a case in a switch statement
charThis keyword will declare a character variable
constWill help in declaring constant as well as immutable variables across all.
continueThe current iteration of the loop will be skipped, and it will proceed to the next one.
defaultThe default case in the switch statement will be specified over here
doA block of code will be executed at least once in a do-while loop
doubleDouble double-precision floating-point variable will be declared over here easily.
elseThe next alternative branch in an if-else statement will be presented here.
enumHelp in declaring an enumeration that is a list of integer constants, in particular.
externA global variable or function will be defined elsewhere, too.
floatA floating-point variable will also be declared here.
forA loop will be started that will repeat until a condition is false.
gotoA control to the labeled statement will be transferred here.
ifA condition will be specified to execute a block of code.
intAn integer variable will be declared here.
longA long integer value is defined here.
registerHelps in declaring a variable that is stored in a register itself for quick access.
shortA short integer variable will be declared here.
signedThe signed variable is declared to hold both positive and negative values simultaneously.
sizeofWill return the size which can be in bytes of a datatype or variable.
staticOne can declare a variable with a static lifetime or functions having limited visibility.
structIt will help in declaratively structured collection of different data types.
switchBased on the value of a variable multi-way branch is seen.
typedefFor an existing data type, a new name is just defined
unionSimply declares a union, which is a data type that can store different sets of data types in the same memory location.
unsignedA variable is declared that holds only non non-negative set of values.
voidJust signifies that a function does not return a value at all.
volatileThis indicates that the value of a variable can be changed unexpectedly
whileA loop is started gat repeats while a condition is found to be true.

Important note: Depending on the version that you are using so the number of keywords can vary. ANSI C has 32 keywords, C11 has 44, and C23 is set to introduce many more that will bring the total count to 54.

Additional Keywords present in C11 :

In this C11, new keywords are added mentioned below :

  • _Alignas - Helps in specifying the alignment of a variable or type.
  • _Alignof - Just returns alignment of a particular type.
  • _Atomic - Will declare atomic types
  • _ Bool - Returns Boolean data type
  • _Complex - Declares a complex set of numbers.
  • _Generic - Will provide genetic programming capabilities to a great extent.
  • _Imaginary - Imaginary numbers are declared with the help of this.
  • _Noreturn - A function is declared that does not return any value.
  • _Static_assert - Compile-time assertions are provided with the help of this.
  • _Thread_local - Thread local. Storage is declared here in the best possible way.

Different Types of Keywords in C are mentioned here :

  • Data type keywords - Mainly used to define the type of data that this variable holds. Example - int, float, char, double, void, _bool, _complex.
  • Control flow related keywords - Helps in managing the flow of the program. Example - case, if, else, for, while, do, goto.
  • Storage class specifiers -Help in defining the scope, visibility, and lifetime of variables.Example - auto, static, register, extern, _Thread, _local.
  • Type qualifiers - Help in easily modifying properties of a particular variable.Example - constructor, volatile, restrict, _automatic.
  • Function-related keywords - Mainly used to define or control functions.Example - return, _noreturn, and inline are some best examples that suit this criterion.
  • Others - Additional keywords or special-purpose ones.Example - sizeof, struct, typedef, union, enum, _Alignas, _Static_assert

Keywords in C with some interesting Examples :

Now we all have an idea about keywords in the C programming language :

  • Char - The char keyword in the C language is simply used to declare a char variable. This character is stored as a single byte in the memory, and that will hold a single character. This is typically used in ASCII coding.
  • Example -

                        #include 
    
                        int main() {
                            char c = 'a';  // Declares a char variable
                            printf("%c", c);  // Prints the value of the char variable
                            return 0;
                        }
    
                        Output :
                                a
                                
  • Int - This int keyword is used to declare some of the integer variables, which will store whole numbers that too without decimal points. Depending on the system architecture, it will take 2 or 4 bytes of memory to store.
  • Example -

                    #include 
    
                    int main() {
                        int num = 25;  // Declare an integer variable
                        printf("%d", num);  // Print the integer value
                        return 0;
                    }
                    Output : 
                    25
                            
  • Float - All floating-point variables can be declared as float variables. They store decimal values. This is mostly used for a more precise set of calculations than the integer.
  • Example -

                   #include 
    
                    int main() {
                        float pi = 3.14;  // Declare a float variable
                        printf("%.2f", pi);  // Print the float value with two decimal points
                        return 0;
                    }
                    Output : 
                    3.14
    
                            
  • If - This if keyword will help in creating a conditional statement, which just allows code to be executed only if a certain set of conditions is true.
  • Example -

                  #include 
    
                int main() {
                    int x = 10;  // Declare a variable
                    if (x > 0) {
                        printf("Positive number");
                    }
                    return 0;
                }
                Output: 
                Positive number
    
                            
  • Else - This statement is simply used in conjunction with the if statement. It just specifies a block of code that will execute if the condition is false.
  • Example -

                 #include 
    
                int main() {
                    int x = -5;
                    if (x > 0) {
                        printf("Positive");
                    } else {
                        printf("Not Positive");
                    }
                    return 0;
                }
                Output : 
                Not Positive
                            
  • For - The for loop will create a loop statement that will run a specific number of times. They just contain initialization, condition, as well as increment condition.
  • Example -

                    #include 
    
                    int main() {
                        for (int i = 0; i < 5; i++) {
                            printf("%d\n", i);
                        }
                        return 0;
                    }
                    Output : 
                    0
                    1
                    2
                    3
                    4
                            
  • Return - This return keyword is used as an exit function, and optionally, it will return a value to the calling function.
  • Example -

                    #include 
    
                    int main() {
                        int result = sum(5, 10);
                        printf("Sum = %d\n", result);
                        return 0;
                    }
    
                    int sum(int a, int b) {
                        return a + b;  // Return sum to the main function
                    }
                    Output : 
                    Sum = 15
    
                            
  • while - This while condition is mainly used to create a loop and execute as long as the condition is true.
  • Example -

                   #include 
    
                    int main() {
                        int x = 0;
                        while (x < 5) {
                            printf("%d\n", x);
                            x++;
                        }
                        return 0;
                    Output : 
                    0
                    1
                    2
                    3
                    4
                            
  • switch - This keyword is used to select one of many code blocks that will execute based on the value of a variable.
  • Example -

                   #include 
    
                int main() {
                    int x = 2;
                    switch (x) {
                        case 1: printf("One\n");
                                break;
                        case 2: printf("Two\n");
                                break;
                        default: printf("Other\n");
                    }
                    return 0;
                }
                Output : 
                Two
                            
  • Do - This keyword is simply used with a while loop that will execute a block of code only once before checking the condition.
  • Example -

                   #include 
    
                    int main() {
                        int x = 0;
                        do {
                            printf("%d\n", x);
                            x++;
                        } while (x < 5);
                        return 0;
                    }
                    Output : 
                    0
                    1
                    2
                    3
                    4
                            
  • Break - The break keyword is mainly used to exit the loop or even the switch statement prematurely when the condition is true.
  • Example -

                   #include 
    
                int main() {
                    for (int i = 0; i < 5; i++) {
                        if (i == 3) break;
                        printf("%d\n", i);
                    }
                    return 0;
                }
                Output : 
                0
                1
                2
                            

    Conclusion

    Be aware and update yourself with some trending skills, and also be job-ready. Enroll in a C programming course in Noida and get the best. Please contact us if you have any questions.

Placed Students

Our Clients

Partners

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses