Keywords in C Programming | Free Notes |

Keywords in C are reserved words that have predefined meanings and serve specific purposes within the language. These words cannot be used as identifiers (variable names, function names, etc.) because they are already reserved by the C language for specific tasks.

  • Keywords are the preserved words that have special meaning in C language.
  • In C Programming Language Keywords have existing meaning assined to it
  • Keywords cannot be used as a variable name.
  • There are 32 Keywords in C Language.
autodoubleintstruct
elselongswitchcase
breakenumregistertypedef
constexternreturnunion
charfloatshortunsigned
continueforsignedvolatile
defaultgotosizeofvoid
doifstaticwhile

list of 32 keywords in C

  • auto: Specifies automatic storage duration for variables declared within a block.
  • break: Used to terminate the execution of a loop or switch statement.
  • case: Used within a switch statement to define different cases.
  • char: Declares a variable to store character data.
  • const: Declares a variable as constant; its value cannot be modified.
  • continue: Skips the remaining code in a loop iteration and proceeds to the next iteration.
  • default: Defines the default case in a switch statement.
  • do: Starts a do-while loop.
  • double: Declares a variable to store double-precision floating-point numbers.
  • else: Specifies the alternative condition in an if-else statement.
  • enum: Declares an enumeration, which is a user-defined data type consisting of named constants.
  • extern: Declares a variable or function as existing externally, usually used in conjunction with header files.
  • float: Declares a variable to store single-precision floating-point numbers.
  • for: Starts a for loop.
  • goto: Transfers control to a labeled statement.
  • if: Starts an if statement.
  • int: Declares a variable to store integer data.
  • long: Declares a variable to store long integer data.
  • register: Suggests to the compiler to store the variable in a CPU register for faster access.
  • return: Exits a function and optionally returns a value.
  • short: Declares a variable to store short integer data.
  • signed: Specifies signed integer data type.
  • sizeof: Returns the size of a data type or variable in bytes.
  • static: Specifies static storage duration for variables; retains their value between function calls.
  • struct: Declares a structure, which is a user-defined data type consisting of members of different data types.
  • switch: Starts a switch statement to select among multiple cases based on a value.
  • typedef: Creates a new type alias or synonym.
  • union: Declares a union, which is a user-defined data type that can hold different data types in the same memory location.
  • unsigned: Specifies unsigned integer data type.
  • void: Specifies that a function does not return any value, or indicates a pointer to an unspecified type.
  • volatile: Indicates that a variable’s value can be changed unexpectedly, often used in embedded systems programming.
  • while: Starts a while loop.

Example 1

Use of int, char, if, else, printf keywords

#include <stdio.h>

int main() {
    // Declaration and initialization using keywords
    int num1 = 10;
    int num2 = 5;

    // Arithmetic operation using keyword
    int sum = num1 + num2;

    // Output using keyword
    printf("The sum of %d and %d is: %d\n", num1, num2, sum);

    // Conditional statement using keyword
    if (sum > 10) {
        printf("The sum is greater than 10.\n");
    } else {
        printf("The sum is not greater than 10.\n");
    }

    return 0;
}

Output :

Printing even numbers from 1 to 10:
2 4 6 8 10 
Finding the first even number greater than 5:
The first even number greater than 5 is: 6

Example 2:

Using “continue” Statement

#include <stdio.h>

int main() {
    // Skipping printing odd numbers from 1 to 10 using continue statement
    printf("Printing even numbers from 1 to 10:\n");
    for (int i = 1; i <= 10; i++) {
        if (i % 2 != 0) {
            continue; // Skip odd numbers
        }
        printf("%d ", i);
    }
    printf("\n");

    return 0;
}

Output :

Printing even numbers from 1 to 10:
2 4 6 8 10

Example 3

Using “break” Statement :

#include <stdio.h>

int main() {
    // Finding the first number greater than 100 divisible by 7 using break statement
    printf("Finding the first number greater than 100 divisible by 7:\n");
    int num = 101;
    while (1) {
        if (num % 7 == 0) {
            printf("%d\n", num);
            break; // Exit loop once condition is met
        }
        num++;
    }

    return 0;
}

Output :

Finding the first number greater than 100 divisible by 7:
105

Example 4

Using “char”, “for”, “while” keywords :

#include <stdio.h>

int main() {
    // Example using char data type and for loop
    printf("Printing characters from 'A' to 'Z':\n");
    for (char ch = 'A'; ch <= 'Z'; ch++) {
        printf("%c ", ch);
    }
    printf("\n");

    // Example using char data type and while loop
    printf("Printing characters from 'a' to 'z':\n");
    char letter = 'a';
    while (letter <= 'z') {
        printf("%c ", letter);
        letter++; // Increment the character to print the next one
    }
    printf("\n");

    return 0;
}

Output :

Printing characters from 'A' to 'Z':
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
Printing characters from 'a' to 'z':
a b c d e f g h i j k l m n o p q r s t u v w x y z 

Example 5

Using “double” & “float” keywords :

#include <stdio.h>

int main() {
    double doubleVar = 3.14159;  // Double-precision floating-point variable
    float floatVar = 1.234567;   // Single-precision floating-point variable

    printf("Double variable: %lf\n", doubleVar);  // Printing double variable
    printf("Float variable: %f\n", floatVar);     // Printing float variable

    return 0;
}

Output :

Double variable: 3.141590
Float variable: 1.234567

You will find more programming example at the end of this module……

Don't be a Chatur. Be the Rancho, Share it to your Friends also....