Back to C Programming
2026-07-125 min read

Example of C Keywords

Learn Example of C Keywords step by step with clear examples and exercises.

Title: Mastering C Keywords: A full guide for C Programming

Why This Matters

Understanding C keywords is crucial for any aspiring C programmer. These are the building blocks of the language, and mastering them will help you write efficient, clean, and error-free code. Knowledge of C keywords is essential for acing coding interviews, solving real-world programming problems, and debugging complex issues that might arise during software development.

Prerequisites

Before diving into the world of C keywords, it's important to have a solid understanding of the following concepts:

  1. Basic computer programming concepts (variables, data types, operators, etc.)
  2. Familiarity with the C syntax and basic I/O operations
  3. Understanding of control structures like loops and conditional statements
  4. Knowledge of functions and their usage in C programming

Core Concept

C keywords are special words that have a specific meaning within the C programming language. They are used to declare variables, define data types, control program flow, and perform various operations. This section will cover some of the most commonly used C keywords along with examples and explanations for each one.

Keywords List

  1. auto: (deprecated) specifies that a variable's storage class is automatic
  2. break: terminates the current loop iteration
  3. case: used in switch statements to define cases
  4. char: data type for character variables
  5. const: defines a constant value that cannot be modified
  6. continue: skips the current loop iteration and moves to the next one
  7. default: used in switch statements as the default case
  8. do: begins a do-while loop
  9. double: data type for floating-point numbers
  10. else: used with if statements to define the alternative block of code when the condition is false
  11. enum: defines an enumerated data type
  12. extern: specifies that a variable or function exists outside the current file
  13. float: data type for floating-point numbers (single precision)
  14. for: begins a for loop
  15. goto: unconditionally jumps to a labeled statement
  16. if: begins an if statement
  17. inline: specifies that the function should be inlined during compilation
  18. int: data type for integer variables
  19. long: modifier for integer and floating-point data types (for larger values)
  20. register: suggests that a variable should be stored in a CPU register
  21. return: terminates the current function and returns a value (optional)
  22. short: modifier for integer data type (for smaller values)
  23. signed: specifies that a data type can represent negative numbers
  24. sizeof: operator to get the size of a data type or variable in bytes
  25. static: specifies that a variable has static storage class or a function is static
  26. struct: defines a structure data type
  27. switch: begins a switch statement
  28. typedef: creates an alias for an existing data type
  29. union: defines a union data type
  30. unsigned: specifies that a data type cannot represent negative numbers
  31. void: data type for functions without a return value
  32. volatile: specifies that the value of a variable may change unexpectedly
  33. while: begins a while loop

Variables and Constants

int i = 10; // integer variable 'i' initialized with value 10
const int CONST_VAL = 20; // constant integer 'CONST_VAL' with value 20
char c = 'a'; // character variable 'c' initialized with ASCII value for 'a'

Control Structures

for (int i = 0; i < 10; i++) {
printf("%d ", i); // prints the numbers from 0 to 9
}

if (i > 5) {
printf("i is greater than 5\n"); // checks if 'i' is greater than 5
} else {
printf("i is less than or equal to 5\n"); // alternative block of code when the condition is false
}

Loops

int i = 0;
while (i < 10) {
printf("%d ", i); // prints the numbers from 0 to 9 in a while loop
i++;
}

do {
printf("%d ", i); // prints the numbers from 0 to 9 in a do-while loop
i++;
} while (i < 10);

Functions

void printHello() {
printf("Hello, World!\n"); // function definition without return value
}

int addNumbers(int a, int b) {
return a + b; // function definition with a return value
}

Worked Example

Let's create a simple C program that calculates the sum of two numbers using functions and control structures.

#include <stdio.h>

int addNumbers(int a, int b) {
return a + b;
}

void main() {
int num1 = 5;
int num2 = 7;
int sum = addNumbers(num1, num2);

printf("The sum of %d and %d is: %d\n", num1, num2, sum);
}

Common Mistakes

  1. Forgetting semicolons (;) at the end of statements can lead to syntax errors.
  2. Using variables before they are declared can result in undefined behavior.
  3. Incorrectly using control structures like loops and conditional statements can cause infinite loops or incorrect program flow.
  4. Mixing up data types can lead to unexpected results and potential runtime errors.
  5. Forgetting to include necessary header files (like `` for I/O operations) can result in compilation errors.

Practice Questions

  1. Write a C program that calculates the factorial of a number using recursion.
  2. Implement a binary search algorithm in C and test it with an array containing sorted integers.
  3. Create a function that swaps the values of two variables without using a temporary variable.
  4. Write a C program that finds the largest and smallest numbers in an array.
  5. Implement a function that checks if a given number is prime or not.

FAQ

What is the purpose of the auto keyword in C?

  • The auto keyword was used to specify that a variable's storage class is automatic, but it has been deprecated since C99 and should not be used in modern C programming.

Can I use floating-point numbers with the int data type in C?

  • No, using floating-point numbers with the int data type can lead to unexpected results due to data truncation or rounding errors. Always use the appropriate data type for your needs (e.g., float, double).

What is the difference between the signed and unsigned keywords in C?

  • The signed keyword specifies that a data type can represent negative numbers, while the unsigned keyword specifies that a data type cannot represent negative numbers.

How do I declare an array in C?

  • To declare an array in C, specify the data type followed by square brackets containing the array size (e.g., int arr[10]).

What is the purpose of the typedef keyword in C?

  • The typedef keyword allows you to create an alias for an existing data type, making your code more readable and easier to understand (e.g., typedef unsigned int uint;).