Tokens in C
What Are Tokens?
Tokens are the smallest elements of a C program that have a meaning to the compiler. They cannot be broken further. All valid C statements are formed using these tokens.
Types of Tokens in C
There are 6 main types of tokens in C:
1. Keywords 2. Identifiers 3. Constants 4. Strings 5. Operators 6. Special SymbolsBelow, each token is explained with simple examples.
1. Keywords
These are reserved words that have a fixed meaning in C. They cannot be used as variable names.
- int
- float
- char
- if
- else
- return
int age;
Here, int is a keyword.
2. Identifiers
Identifiers are names given to variables, functions, arrays, etc.
int marks; float totalScore;
Here, marks and totalScore are identifiers.
- Must begin with letter or underscore
- No spaces allowed
- Case-sensitive
- Cannot be a keyword
3. Constants
Constants are fixed values that do not change during program execution.
- 10 → integer constant
- 3.14 → floating constant
- 'A' → character constant
- 010, 0x1A → octal & hexadecimal constants
const int MAX = 100;
4. String Literals
Strings are sequences of characters written inside double quotes.
"Hello World" "Welcome to C Programming"
They end with a hidden null character ('\0').
5. Operators
Operators perform operations on operands (variables, values).
Common operator groups:
- Arithmetic → +, -, *, /, %
- Relational → >, <, ==, !=
- Logical → &&, ||, !
- Assignment → =, +=, -=
- Increment/Decrement → ++, --
- Bitwise → &, |, ^, <<, >>
a + b
6. Special Symbols
Symbols that have a special meaning in C.
- () → Function call, expression grouping
- {} → Block of code
- [] → Array index
- , → Separator
- ; → Statement terminator
- # → Preprocessor directive
printf("Hello");
Here, () and ; are special symbols.
Diagram: Tokens at a Glance
TOKEN TYPES IN C
-------------------
| Keywords |
| Identifiers |
| Constants |
| Strings |
| Operators |
| Special Symbols |
-------------------
Practice Questions
- What are tokens in C? Explain.
- Differentiate between keywords and identifiers.
- Write examples of all types of constants.
- Explain operators with examples.
- List any five special symbols used in C.
Practice Task
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("Sum = %d", a + b);
return 0;
}