Tokens in C? with example | Types of Tokens in C

 

Tokens in C? with example

Tokens in C programming are fundamental building blocks that make up the source code of a program. They are the smallest units of a C program and include keywords, identifiers, constants, operators, and punctuation marks.

Tokens in C


Types of Tokens in C

1. Keywords:

Keywords are reserved words in C that have predefined meanings and cannot be used as identifiers. Examples of C keywords include int, if, while, for, return, and switch.C language supports 32 keywords given below:
autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile

Example:

c
int main() { if (x > 0) { return 1; } return 0;
}In the above code, int, if, and return are keywords.

2. Identifiers:

Identifiers are names given to variables, functions, arrays, and other user-defined entities. They must start with a letter or underscore and can be followed by letters, digits, or underscores.

Example:

c
int age; float salary; void calculateTotalAmount();
In the code above, age, salary, and calculateTotalAmount are identifiers.

3. Constants:

Constants are values that do not change during program execution. There are various types of constants in C:
ConstantExample
Integer constant10, 11, 34, etc.
Floating-point constant45.6, 67.8, 11.2, etc.
Octal constant011, 088, 022, etc.
Hexadecimal constant0x1a, 0x4b, 0x6b, etc.
Character constant'a', 'b', 'c', etc.
String constant"java", "c++", ".net", etc.

      1.Integer constants:

c
42 0xFF // Hexadecimal 077 // Octal

           2.Floating-point constants:

c
3.14
2.0e-5

   3.Character constants:

c
'A' '\n' // Newline character

    4.String literals:

c
"Hello, World!"

        In the code examples above, 42, 3.14, 'A', and "Hello, World!" are constants.

4. Operators:

C provides various operators for performing operations on data. These include arithmetic, relational, logical, and bitwise operators.Depending on the number of operands, operators are classified as follows:

Unary Operator

A unary operator is an operator applied to the single operand. For example: increment operator (++), decrement operator (--), sizeof, (type)*.

Binary Operator:

The binary operator is an operator applied between two operands. The following is the list of the binary operators:

1. Arithmetic Operators

2. Relational Operators

3. Shift Operators

4. Logical Operators

5. Bitwise Operators

6. Conditional Operators

7. Assignment Operator

8. Misc Operator

Examples:
c
int sum = a + b; // Addition operator if (x == y) // Equality operator { result = 1; }
In the code above, +, ==, and = are operators.

5. Punctuation:

Punctuation symbols such as parentheses, braces, semicolons, and commas are used to structure C code and separate different elements.

Examples:

c
for (int i = 0; i < 10; i++) { printf("Hello, World!\n"); }

In this code, (){};, and , are punctuation marks.

6. Preprocessor Directives:

Preprocessor directives begin with a # symbol and are used to instruct the C preprocessor to perform actions like including header files and defining macros.

Examples:

c
#include <stdio.h> #define MAX_VALUE 100
In this code, #include and #define are preprocessor directives.

7. Comments:

Comments are not considered tokens but are essential for adding human-readable explanations within the code. There are two types of comments in C: single-line comments and multi-line comments.

Example:
c
// This is a single-line comment /* This is a multi-line comment */


Comments are used for documentation and do not affect the program's execution.

In summary, tokens in C programming are the smallest units of a program and include keywords, identifiers, constants, operators, punctuation marks, preprocessor directives, and comments. Understanding these tokens is crucial for writing and comprehending C code effectively.

Post a Comment

0 Comments