This tutorial is a detailed guide on tokens and their usage in C.
Tokens in C
In C programming, tokens are the important and smallest elements used for creating a C program. We cannot create a program without using the tokens as they are the building components for C programs. Tokens in C have the following six types:
1: Keywords
Keywords are the pre-defined or reserved words in C having their own importance. You cannot assign the reserved keyword as a variable name because they are the pre-defined words in C used by the compiler. There are a total of 32 keywords available in C and each keyword performs a special function, below we have listed them in the table:
auto | int | double | struct |
---|---|---|---|
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
const | float | short | unsigned |
continue | for | signed | void |
default | goto | sizeof | volatile |
do | if | static | while |
2: Strings
In C, strings are the set of characters with the null character at the end. Strings are declared in double quotes and stored as an array of characters. The only key difference between a string and an array is that the array is not terminated with the null character, but the string is.
There are multiple ways to define and initialize the string in C.
i: The following is the first method for initializing the string in C. In the below example code, we have declared and initialized the string z and allocated the memory of 10 bytes:
ii: If you initialized the string as follows, the compiler will allocate memory at run time:
iii: You can also initialize a string in the form of characters:
‘\0’ is used at the end of the array to indicate the end of the string.
3: Operators in C
Operators are the special symbols used to perform different functions, and the data variables on which operations are performed are known as operands. In C, the following are the two types of operators:
- Unary operator
- Binary operator
i. Unary Operator
The unary operator is an operator applied on a single operand in C and is of two different types:
- Increment operator: This operator of C increases the value of the variable by 1.
- Decrement operator: This operator of C decreases the value of the variable by 1.
ii: Binary Operator
The binary operator is applied between the two operands and C provides a wide range of binary operators to perform various tasks. Following are the binary operators in C:
- Arithmetic operators: These operators are for performing basic functions like addition, division, and subtraction.
- Logical operator: The logical operators are used to connect multiple expression statements and the most commonly used logical operators in C are NOT, AND, and OR.
- Relational operator: The relational operators are used to compare the two variables.
- Bitwise operator: The Bitwise operators in C handle the data types at the bit level.
- Conditional operator: The conditional operator in C is also known as the ternary operator because it takes three operands – the condition, statement 1, and statement 2. It verifies the conditions and returns the statement depending on the condition results.
- Assignment operator: This operator is used for assigning the value to the variable in the program and is denoted by the = symbol.
4: Identifiers
In each program of C, the variable is given a name called identifiers. Any name given to a function, variable, or array are identifiers and they are user-defined words. Identifiers cannot be keywords, but they can be composed of uppercase letters, lowercase letters, or digits.
Here sum is given to the integer type variable is the identifier. Following are the rules to name an identifier in C:
- The first character must be alphabet or underscore
- Any special character or punctuation is not allowed except for the underscore
- Identifiers cannot be keywords
- The length should not be more than 31 characters
5: Special Symbols in C
In C, there are some special symbols that serve the meaning and cannot be used for another purpose. Following are the special symbols in C:
Special Symbols | Symbols | Description |
---|---|---|
Hash | # | For denoting the header file |
Period | . | Access member of the structure |
Square bracket | [] | Single and multi-dimensional subscripts |
Curly braces | {} | Opening and closing of code or loop |
Asterisk | * | Multiplication operator, initialize a pointer |
Simple bracket | () | Function declaration and calling |
Tilde | ~ | Bitwise NOT operator in C |
Comma | , | Separating statements, variables, functions parameters |
6: Constants in C
Constants are the values or variables that cannot be changed in the program and remain the same throughout the execution of the program. Following are the types of constants in C with examples:
Constant | Example |
---|---|
Integer constant | 14,20,30 |
Octal constant | 017, 044, 088 |
Character constant | ‘z’, ‘a’, ‘h’ |
Floating point constant | 14.98, 15.748 |
String constant | “Hello”, “Linuxhint” |
Hexadecimal constant | 0x7c, 0x4a, 0x3b |
Conclusion
In C programming, tokens are used for creating and executing the correct code. They are the essential building elements and without them the code is incomplete. There are six categories of tokens including keywords, strings, identifiers, operators, constants, and special symbols. Understanding the use of tokens is important for the proper working of the code.