
200 views
Tokens in C
The C programming, a token is the smallest unit in a program. Tokens are the building blocks of a C program and are recognized by the C compiler. There are several types of tokens in C:
- Keywords:
- Keywords are reserved words that have a specific meaning in the C language.
- Examples:
int
,if
,while
,for
,return
,char
,double
,struct
,break
,continue
, etc.
- Identifiers:
- Identifiers are used to name various program elements such as variables, functions, arrays, and more.
- An identifier is a sequence of letters, digits, and underscores that must begin with a letter or underscore.
- Examples:
main
,counter
,my_function
,MAX_SIZE
,totalAmount
, etc.
- Constants:
- Constants represent fixed values that do not change during program execution.
- Types of constants include integer constants (e.g.,
42
), floating-point constants (e.g.,3.14
), character constants (e.g.,'A'
), and string literals (e.g.,"Hello, World!"
).
- String Literals:
- String literals are sequences of characters enclosed in double quotes.
- Example:
"Hello, World!"
- Operators:
- Operators perform operations on operands to produce a result.
- Examples:
+
,-
,*
,/
,%
,=
,==
,!=
,<
,>
,<=
,>=
,&&
,||
,&
,|
,!
,++
,--
, etc.
- Punctuation Symbols:
- Punctuation symbols include characters like parentheses
()
, braces{}
, square brackets[]
, commas,
, semicolons;
, periods.
, arrow->
, etc. - These symbols are used for grouping, separating, and terminating statements and expressions.
- Preprocessor Directives:
- Preprocessor directives are commands to the C preprocessor and start with
#
. - Examples:
#include
,#define
,#ifdef
,#endif
,#ifdef
,#pragma
, etc.
- Comments:
- Comments are not considered tokens by the compiler but are essential for code documentation.
- Single-line comments start with
//
, and multi-line comments are enclosed in/* */
.
Here’s an example illustrating various types of tokens in a simple C program:
C
#include <stdio.h>
// Function prototype
int add(int a, int b);
int main() {
int num1 = 10; // Identifier: num1, Constants: 10
int num2 = 20; // Identifier: num2, Constants: 20
int sum = add(num1, num2); // Identifier: sum, Function: add
printf("The sum is %d\n", sum); // Identifiers: printf, "The sum is %d\n"
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
In this example, you can see various types of tokens, including keywords (int
, return
), identifiers (main
, num1
, add
), constants (10
, 20
), operators (=
, +
), punctuation symbols (;
, {
, }
), preprocessor directives (#include
, #define
), and comments (// Function prototype
, /* Function definition */
). These tokens collectively make up the C program’s syntax and semantics.