Back to C Programming
2026-07-126 min read

C Keywords and Identifiers

Learn C Keywords and Identifiers step by step with clear examples and exercises.

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

Why This Matters

Understanding C keywords and identifiers is crucial for any aspiring C programmer. These are the building blocks of every C program, and mastering them will help you write cleaner, more efficient code. In addition, knowing these concepts can help you debug errors, understand complex programs, and prepare for coding interviews or exams.

Prerequisites

Before diving into C keywords and identifiers, it's essential to have a basic understanding of the following:

  1. Familiarity with the C programming language syntax and structure
  2. Understanding of data types, variables, and operators in C
  3. Basic knowledge of control structures like if-else statements and loops (for and while)
  4. Familiarity with functions and function declarations
  5. Knowledge of pointers and memory management in C

Core Concept

Keywords

C keywords are predefined words that have special meanings within the language. They are used to declare variables, define functions, control flow, and manage memory. Some common C keywords include:

  1. int: integer data type
  2. float: floating-point data type
  3. char: character data type
  4. void: no data type (used for function return types or as a placeholder for functions that don't return values)
  5. if, else, and elif: conditional statements
  6. for, while, and do-while: loop structures
  7. switch and case: multiple conditional statements
  8. break and continue: modifying loop behavior
  9. return: ending a function and returning a value
  10. sizeof: determining the size of data types or variables
  11. typedef: creating new names for existing types
  12. static: restricting the scope or lifetime of a variable or function
  13. register: suggesting that a compiler should place a variable in a register instead of memory
  14. const: declaring a constant (immutable) variable
  15. volatile: indicating that a variable's value may be modified by means outside the program's control

Identifiers

Identifiers are user-defined names used to identify variables, functions, and labels in C programs. They can consist of letters (A-Z, a-z), digits (0-9), underscores (_), and the at sign (@). However, identifiers cannot start with a digit or an underscore.

C identifiers are case-sensitive, meaning myVariable is different from myvariable. It's good practice to use meaningful names for your identifiers, as it makes your code easier to read and understand.

Naming Rules

  1. Identifiers cannot be the same as C keywords
  2. Identifiers cannot contain spaces or special characters (except underscores)
  3. Identifier names are limited to 31 characters
  4. The first character of an identifier cannot be a digit
  5. Certain reserved words, such as true and false, cannot be used as identifiers
  6. It is recommended to use a consistent naming convention (e.g., camelCase, snake_case, or Hungarian notation) for your variables and functions

Worked Example

Let's create a simple C program that declares variables, uses keywords, and demonstrates proper naming conventions:

#include <stdio.h>

int main() {
int myInteger = 10;
float myFloat = 3.14;
char myCharacter = 'A';

printf("The value of myInteger is %d\n", myInteger);
printf("The value of myFloat is %.2f\n", myFloat);
printf("The value of myCharacter is %c\n", myCharacter);

return 0;
}

In this example, we have declared three variables: myInteger, myFloat, and myCharacter. We've used the keywords int, float, and char to define their data types. The program then prints the values of these variables using the printf() function.

This program also follows a consistent naming convention, using camelCase for variable names.

Common Mistakes

  1. Forgetting semicolons (;): Semicolons are required at the end of every statement in C. Forgetting a semicolon can lead to syntax errors or unexpected behavior.
  2. Naming conflicts: Avoid naming your identifiers the same as C keywords or other identifiers within the same scope. This can lead to confusion and errors when trying to access variables or functions.
  3. Case sensitivity: Remember that C is case-sensitive, so be consistent with your variable and function names. Using inconsistent casing can make it difficult to read and understand your code.
  4. Identifiers starting with digits: Identifiers cannot start with a digit. This can lead to syntax errors when trying to compile your code.
  5. Reserved words as identifiers: Certain reserved words, such as true and false, cannot be used as identifiers because they have special meanings within the C language. Using these words as identifiers can lead to unexpected behavior or syntax errors.
  6. Inconsistent naming conventions: Using inconsistent naming conventions for your variables and functions can make it difficult to read and understand your code, especially in larger projects with multiple contributors.
  7. Using unclear or misleading names for identifiers: Using unclear or misleading names for identifiers can make it difficult to understand the purpose of a variable or function at a glance. It's important to use descriptive names that accurately reflect the purpose and contents of your variables and functions.

Practice Questions

  1. Declare a variable named myAge of type int. Assign it the value 25.
int myAge = 25;
  1. Write a program that calculates the area of a rectangle with length length and width width. Use appropriate data types for your variables.
#include <stdio.h>

int main() {
int length, width;
printf("Enter the length: ");
scanf("%d", &length);
printf("Enter the width: ");
scanf("%d", &width);
int area = length * width;
printf("The area of the rectangle is %d\n", area);
return 0;
}
  1. Write a program that checks if a number is even or odd using the modulus operator (%).
#include <stdio.h>

int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}
return 0;
}

FAQ

  1. Can I use spaces in my identifiers?
  • No, C does not allow spaces in identifiers. However, you can use underscores (_) to separate words within an identifier.
  1. What happens if I forget a semicolon (;)?
  • If you forget a semicolon, the compiler may generate errors or produce unexpected results when attempting to compile and run your code.
  1. Can I use special characters like !@#$%^&*() in my identifiers?
  • No, C does not allow most special characters (except underscores) in identifiers. However, some compilers may support certain special characters if they are enclosed in double quotes.
  1. What is the maximum length of an identifier in C?
  • The maximum length of an identifier in C is 31 characters.
  1. Why can't I use certain reserved words as identifiers?
  • Certain reserved words, such as true and false, cannot be used as identifiers because they have special meanings within the C language. Using these words as identifiers can lead to unexpected behavior or syntax errors.
  1. What is the purpose of the typedef keyword in C?
  • The typedef keyword allows you to create new names for existing types, making your code more readable and easier to understand. For example:
typedef unsigned int uint;
uint myVariable = 10;

In this example, we've created a new name (uint) for the unsigned int data type. This can make it easier to read and write code that uses this data type.

  1. What is the purpose of the static keyword in C?
  • The static keyword can be used to restrict the scope or lifetime of a variable or function. When applied to a variable, it means that the variable retains its value between function calls. When applied to a function, it means that the function is only accessible within the file where it is defined.
  1. What is the purpose of the const keyword in C?
  • The const keyword can be used to declare a constant (immutable) variable. Once a variable is declared as const, its value cannot be changed during runtime.
  1. What is the purpose of the volatile keyword in C?
  • The volatile keyword indicates that a variable's value may be modified by means outside the program's control, such as hardware or other threads. This tells the compiler not to optimize the variable's access, ensuring that it is always accessed directly from memory.