Skip to content
StudyFame

Keywords and Identifiers in C

Every language has words with a fixed meaning. In C, these are called keywords, like int, if and return.

The names you choose, for variables and functions, are called identifiers. In this lesson you will learn all 32 keywords in an easy-to-remember way, and the rules for making valid identifiers.

What You Will Learn

  • What keywords are, and the list of 32 keywords in ANSI C.
  • What identifiers are.
  • The rules for naming identifiers.
  • How to spot valid and invalid names.
  • The difference between a keyword and an identifier.

Keywords in C

Exam definition: Keywords are reserved words that have a fixed meaning in C. They cannot be used as identifiers. ANSI C has 32 keywords, and all of them are written in lowercase.

All 32 keywords, in 4 easy groups

GroupKeywordsCount
Data typeschar int float double void short long signed unsigned9
Control flowif else switch case default for while do break continue goto return12
Storage classesauto register static extern4
Othersconst volatile sizeof typedef struct union enum7

Memory trick: 9 + 12 + 4 + 7 = 32. Remember the 4 groups: Types, Control, Storage, Others. Storage is the easiest: "A R S E" = auto, register, static, extern.

Newer standards added a few more keywords (C99 added inline, restrict and _Bool, for example), but exams usually ask for the 32 keywords of ANSI C.

Identifiers in C

Exam definition: An identifier is the name given to a variable, function, array or other user-defined item in a program.

Rules for naming identifiers

  1. It can contain letters (a–z, A–Z), digits (0–9) and the underscore _ only.
  2. It must start with a letter or an underscore, never with a digit.
  3. No spaces and no special characters like @, #, -, $.
  4. It cannot be a keyword.
  5. It is case-sensitive: marks, Marks and MARKS are three different names.
  6. Keep it reasonably short. Compilers are only required to treat the first 31 characters as important (63 in C99 and later).

Memory trick: "L-D-U, start with L or U": only Letters, Digits and Underscore, and the first character must be a Letter or Underscore.

Valid or invalid?

NameValid?Reason
marksValidOnly letters
total_marksValidUnderscore is allowed
_countValidCan start with an underscore
marks2ValidDigits allowed after the first character
2marksInvalidStarts with a digit
total marksInvalidContains a space
total-marksInvalid- is not allowed
intInvalidIt is a keyword
IntValidDifferent from int (case-sensitive), but confusing; avoid it

Keyword vs Identifier

KeywordIdentifier
Fixed meaning, given by CName chosen by the programmer
Cannot be changed or reusedCan be any valid name
Always lowercaseCan use upper and lower case
32 in ANSI CUnlimited
Example: int, whileExample: age, total_marks

Example 1: Good Identifiers in a C Program

#include <stdio.h>

int main(void)
{
    int student_age = 19;
    float cgpa = 8.4f;
    char grade = 'A';

    printf("Age: %d\n", student_age);
    printf("CGPA: %.1f\n", cgpa);
    printf("Grade: %c\n", grade);

    return 0;
}

Output

Age: 19
CGPA: 8.4
Grade: A

Code Explanation

  1. Keywords used here: int, float, char, void, return.
  2. Identifiers used here: main, student_age, cgpa, grade (and printf, a name from the library).
  3. The names describe what they store. student_age is much clearer than x.

How the Compiler Uses Them

  • When the compiler reads your code, it checks each word against its list of keywords.
  • If the word is a keyword, it uses its fixed meaning (for example, int means "integer type").
  • Otherwise, it treats the word as an identifier and checks that it was declared before use.
  • That is why a keyword can never be an identifier: the compiler would not know which meaning you want.

Example 2: What Happens with an Invalid Identifier

#include <stdio.h>

int main(void)
{
    int 2marks = 50;
    printf("%d\n", 2marks);
    return 0;
}

Output (GCC compiler error)

error: invalid suffix "marks" on integer constant

Other compilers word it differently (Clang says expected identifier), but it is the same problem. The compiler reads 2 as a number, then gets confused by marks. Rename it to marks2 or second_marks and it works.

Important Points to Remember

  • ANSI C has 32 keywords, all in lowercase.
  • Identifiers use only letters, digits and underscore.
  • An identifier cannot start with a digit.
  • A keyword cannot be used as an identifier.
  • C is case-sensitive: age and Age are different.

Common Mistakes Beginners Make

MistakeExampleFix
Starting with a digit1st_rankrank_1st or first_rank
Using a keywordint float = 2;int price = 2;
Using a space or hyphenroll-noroll_no
Wrong case when using itDeclared total, used TotalUse exactly the same spelling
Meaningless namesa1, xxmarks, average

Keywords and Identifiers Practice Questions

  1. What is a keyword? How many keywords are there in ANSI C?
  2. Write the four storage class keywords.
  3. Write any four rules for naming an identifier.
  4. Which are valid: roll_no, 9lives, _temp, float, my name?
  5. Write two differences between a keyword and an identifier.
Show answers
  1. A reserved word with a fixed meaning. ANSI C has 32 keywords.
  2. auto, register, static, extern.
  3. Only letters, digits and underscore; must start with a letter or underscore; no keywords; no spaces or special characters (also: case-sensitive).
  4. Valid: roll_no, _temp. Invalid: 9lives (starts with digit), float (keyword), my name (space).
  5. A keyword has a fixed meaning given by C, an identifier is chosen by the programmer. Keywords cannot be reused; identifiers can be any valid name.

Coding Exercise

Write a program that stores a student's roll number (45), percentage (82.5) and section ('B') in three variables with meaningful, valid names, and prints them. Then list which words in your program are keywords.

Show solution
#include <stdio.h>

int main(void)
{
    int roll_number = 45;
    float percentage = 82.5f;
    char section = 'B';

    printf("Roll number: %d\n", roll_number);
    printf("Percentage: %.1f\n", percentage);
    printf("Section: %c\n", section);
    return 0;
}
Roll number: 45
Percentage: 82.5
Section: B

Keywords used: int, void, float, char, return.

Quick Quiz: Keywords and Identifiers (MCQ)

  1. How many keywords are there in ANSI C?

    A) 24   B) 30   C) 32   D) 48

  2. Which is a valid identifier?

    A) 3total   B) total_3   C) total-3   D) total 3

  3. Which of these is NOT a keyword?

    A) static   B) sizeof   C) main   D) volatile

  4. An identifier can start with:

    A) A digit   B) A letter or underscore   C) @   D) #

  5. Are sum and Sum the same identifier in C?

    A) Yes   B) No   C) Only in C99   D) Only for variables

Show answers
  1. C: 32.
  2. B: total_3.
  3. C: main is an identifier (a function name), not a keyword.
  4. B: a letter or underscore.
  5. B: no, C is case-sensitive.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

KeywordReserved word with a fixed meaning
Number of keywords32 (ANSI C), all lowercase
GroupsData types 9 + Control 12 + Storage 4 + Others 7
Storage keywordsauto, register, static, extern
IdentifierName of a variable, function or array
Allowed charactersLetters, digits, underscore
First characterLetter or underscore (never a digit)
Is main a keyword?No, it is an identifier

Frequently asked questions

Can a keyword be used as a variable name?
No. Keywords are reserved and have a fixed meaning.
Is main a keyword in C?
No. main is an identifier: the name of a function.
Why can't an identifier start with a digit?
The compiler would read it as a number, like 2marks → 2 + marks.
Is printf a keyword?
No. It is a library function declared in stdio.h.
Are keywords case-sensitive?
Yes. Keywords are always lowercase: int is a keyword, INT is not.

Remember

  • Keyword count: 9 + 12 + 4 + 7 = 32.
  • Identifier rule: L-D-U, start with L or U.

Next Topic

Next lesson: Tokens in C. You will learn the smallest building blocks of every C program and how to count them.