Skip to main content

Keywords in C Language with example

Here is a comprehensive list of keywords in the C programming language:

auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while

Note that these keywords have predefined meanings in the C language and cannot be used as identifiers (variable names, function names, etc.) in your programs.

Certainly! Here are some of the keywords in the C programming language along with examples:

1. auto: Declares a local variable with automatic storage duration.
   Example: auto int x = 5;

2. break: Terminates the current loop or switch statement.
   Example:
   ```c
   for (int i = 0; i < 10; i++) {
       if (i == 5) {
           break;  // terminates the loop when i reaches 5
       }
   }
   ```

3. case: Defines a constant value within a switch statement.
   Example:
   ```c
   switch (x) {
       case 1:
           printf("Value is 1");
           break;
       case 2:
           printf("Value is 2");
           break;
       default:
           printf("Value is neither 1 nor 2");
   }
   ```

4. const: Declares a constant variable that cannot be modified.
   Example: const int MAX_VALUE = 100;

5. continue: Jumps to the next iteration of a loop.
   Example:
   ```c
   for (int i = 0; i < 10; i++) {
       if (i == 5) {
           continue;  // skips the rest of the loop body and moves to the next iteration
       }
   }
   ```

6. do: Starts a do-while loop.
   Example:
   ```c
   int i = 0;
   do {
       printf("%d ", i);
       i++;
   } while (i < 5);
   ```

7. else: Represents an alternative branch in an if statement.
   Example:
   ```c
   if (x > 0) {
       printf("Positive");
   } else {
       printf("Non-positive");
   }
   ```

8. enum: Defines an enumerated data type.
   Example:
   ```c
   enum Color {
       RED,
       GREEN,
       BLUE
   };
   enum Color selectedColor = GREEN;
   ```

9. extern: Declares a variable or function that is defined in another source file or external to the current scope.
   Example: extern int globalVariable;

10. float: Represents a floating-point data type.
    Example: float pi = 3.14159;

11. for: Starts a for loop with initialization, condition, and increment/decrement expressions.
    Example:
    ```c
    for (int i = 0; i < 10; i++) {
        printf("%d ", i);
    }
    ```

12. if: Represents a conditional statement.
    Example:
    ```c
    if (x > 0) {
        printf("Positive");
    }
    ```

13. int: Represents an integer data type.
    Example: int age = 25;

14. return: Terminates the current function and returns a value.
    Example:
    ```c
    int sum(int a, int b) {
        return a + b;
    }
    ```

15. sizeof: Returns the size in bytes of a data type or variable.
    Example: sizeof(int);

16. static: Declares a variable or function with static storage duration.
    Example:
    ```c
    static int counter = 0;
    static void incrementCounter() {
        counter++;
    }
    ```

17. switch: Starts a switch statement with multiple possible execution paths based on a variable's value.
    Example:
    ```c
    switch (choice) {
        case 1:
            printf("First option selected");
            break;
        case 2:
            printf("Second option selected");
            break;
        default:
            printf("Invalid option");
    }
    ```

18. while: Starts a while loop with a condition.
    Example:
    ```c
    int i = 0;
    while (i < 5) {
        printf("%d ", i);
        i++;
    }
    ```

These are some of the keywords in the C programming language. Each keyword serves a specific purpose and has its own rules and usage within the language. Understanding and using these keywords correctly is essential for writing valid and effective C programs.

Comments