2.8 TOKENS
Programming in C
TOKENS IN C
Tokens are the basic lexical building blocks of a C program.
They are the smallest individual units of a program that are recognized and understood by the compiler.
Characters written in a source program are grouped into tokens according to the rules of the C language. The compiler performs lexical analysis, during which it checks whether the tokens form legal strings as per the syntax rules of C.
C language has five classes of tokens:
-
Identifiers
-
Keywords (Reserved words)
-
Operators
-
Separators
-
Constants
Example Showing Tokens
Tokens Generated
-
Keywords :
if,else -
Identifiers :
x -
Constants :
5,2,10 -
Operators :
<,=,+ -
Separators :
;,(,)
1 Identifier
An identifier is a sequence of characters created by the programmer to name or identify program elements such as:
-
Variables
-
Arrays
-
Functions
-
Labels
Rules for Naming Identifiers
-
The first character must be an alphabet or an underscore (_).
-
Subsequent characters may be alphabets, digits, or underscores.
-
Only the first 31 characters are significant.
-
Keywords cannot be used as identifiers.
-
Identifiers are case-sensitive.
Valid Identifiers
-
employee_number -
monthly_pay -
interest_per_annum -
tool_4
Invalid Identifiers
-
230_item(starts with digit) -
#pulse_rate(special symbol) -
total~amount -
/profit margin
2 Keywords
Keywords are reserved words that have a special meaning to the compiler.
They cannot be redefined or used as variable names.
Important Characteristics
-
Predefined by the C language
-
Always written in lowercase
-
Used for program structure and control
Keywords in C (C89 – 32 keywords)
Some important ones include:
int, float, char, if, else, while, for, do, return, switch, case, break, continue, struct, union, typedef, static, extern, sizeof, void
Additional Keywords
-
C89 added:
const,enum,signed,volatile -
C99 added:
inline,restrict,_Bool,_Complex,_Imaginary
⚠️ Note: Some compilers provide extended keywords like near, far, asm, which are compiler-specific.
3 Constants
A constant is a fixed value written directly in the program.
Its value does not change during program execution.
Types of Constants in C
-
Integer Constants
-
Floating-point Constants
-
Character Constants
-
String Constants
3.1 Integer Constants
-
Sequence of digits
-
Can be decimal, octal, or hexadecimal
| Type | Example |
|---|---|
| Decimal | 92 |
| Octal (starts with 0) | 0134 |
| Hexadecimal (0x / 0X) | 0x5C |
Suffixes:
-
U / u→ unsigned -
L / l→ long -
LL→ long long (C99)
Examples:
3.2 Floating-point Constants
-
Have decimal point and optional exponent
-
Default type: double
Examples:
3.3 Character Constants
-
Single character enclosed in single quotes
-
Stored as numeric ASCII values
Escape Sequences
Used for non-printable characters:
Hex and Octal representation:
⚠️ Portability Tip: Use escape characters like '\b' instead of numeric ASCII codes.
3.4 String Constants
-
Sequence of characters enclosed in double quotes
-
Stored as array of characters
-
Ends with a null character '\0'
Example:
⚠️ Important Difference
-
'A'→ single character -
"A"→ string containingAand\0
4 Assignment
The assignment operator (=) assigns the value of an expression to a variable.
General Form
Examples:
Important Points
-
Left operand must be an lvalue
-
Right operand can be any expression
-
Assignment itself is an expression
Example:
5 Initialization
When a variable is declared without a value, it contains garbage data.
Initialization
Assigning a value at the time of declaration:
Advantages
-
Prevents undefined behavior
-
Improves program reliability
Conclusion
Tokens form the foundation of C programming.
Understanding identifiers, keywords, constants, operators, separators, assignment, and initialization is essential for:
-
Writing correct syntax
-
Avoiding compiler errors
-
Developing efficient and portable programs
📘 Reference
The content is prepared by referring to the standard textbook:
“Computer Fundamentals and Programming in C”
Pradip Dey & Manas Ghosh
Oxford University Press, Second Edition (2018)
Aivette-COI
Helping college students learn smartly, revise quickly, and approach exams with confidence.
With love and care 🤍
— Aivette
Comments
Post a Comment