Keywords and Identifiers in C

C Keywords

📌 Definition

Keywords are predefined and reserved words in C programming that have special meanings to the compiler.

  • Keywords are part of the language syntax

  • They cannot be used as identifiers (variable names, function names, etc.)

  • Since C is case-sensitive, all keywords must be written in lowercase


📌 Example

int money;
  • int → keyword indicating the data type integer

  • money → variable name (identifier)

Here, int tells the compiler that money is an integer variable.


📌 Number of Keywords in ANSI C

ANSI C contains 32 keywords.

✔ List of ANSI C Keywords



Additional Keywords in C

Along with the 32 ANSI C keywords, C may support additional keywords depending on the compiler implementation.


C Identifiers

📌 Definition

An identifier is the name given to program elements such as:

  • Variables

  • Functions

  • Structures

  • Arrays

  • Objects

Identifiers must be unique so that each entity can be identified during program execution.


📌 Example

int money; double accountBalance;

Here:

  • money → identifier (variable name)

  • accountBalance → identifier

  • int, double → keywords (data types)

⚠ Identifier names must not be keywords.
Example: int cannot be used as an identifier.


Rules for Writing Identifiers

1️⃣ A valid identifier may contain:

  • Letters (A–Z, a–z)

  • Digits (0–9)

  • Underscore _


2️⃣ The first character must be:

  • A letter, OR

  • An underscore _

(Starting with _ is allowed but discouraged)


3️⃣ Length of identifier:

  • No strict limit in theory

  • Most compilers distinguish at least first 31 characters


4️⃣ Identifiers are case-sensitive

Xx

5️⃣ Keywords cannot be used as identifiers.


Valid Identifier Examples

x y12 sum_1 _temperature names area tax_rate TABLE

Invalid Identifier Examples

IdentifierReason
2csbmec        Cannot start with digit
mec#cs2b        Illegal character #
order-no        Illegal character -
mec cs2b        Space not allowed

Identifier Length Note

  • Identifiers can be very long

  • Some older implementations recognized only first 8 characters

  • Modern compilers typically recognize 31 or more

Example

file-manager file-management

Both are valid, but older compilers may treat them as identical because the first 8 characters are the same.
Therefore, avoid using such similar names in the same program.


Good Programming Practice

  • You may choose any valid identifier name (except keywords)

  • Always use meaningful and descriptive names

✔ Makes programs easier to:

  • Read

  • Understand

  • Maintain

  • Debug



Example: University Question
State proper reasons to identify whether the given identifiers are valid or not.

i) 9marks
ii) Mark 2020
iii) v4vote
iv) _csdept
v) @cse
vi) AxB



Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays