Tokens in C/C++
Hy guys welcome to another tutorial of C/C++ Programming Language. In this tutorial, you will learn about Tokens which are used both in C and c++
What is a Token?
So let's start by understanding the term " Token " used here.
So what is a Token?
The Smallest individual part of a program is called a token.
As we know that all the programs written in any language whether it is C or C++ or any other Programming Language all are comprised of small individual parts which can be a variable, keyword, constant, operator, special symbols, strings when combined together creating that particular program.
Tokens In C
Tokens in C are the most important element used for creating a program in C. However we know that the tokens are the smallest individual part (element) of a Program but there are various types of tokens used in C Programming Language which we will be discussing now.
The following are the types of tokens used in C:
- Keywords
- Strings
- Identifiers
- Constants
- Operators
- Special Symbols.
Keywords
Keywords are predefined, reserved words in C and each of which is associated with specific features.
Strings
Now moving on to Strings as Keywords are predefined, reserved words in C on the other hand
Strings are always represented as an array of characters having null character '\0' at the end where the null characters denote the end of that string.
In C Language the Strings are always enclosed within double quotes (“ “), while on the other hand the characters are enclosed within single characters (' '). However, the size of a string is the number of characters that the string contains.
There are mainly three ways to declare a String in C language which is shown in the given figure below −
char as “string[20]”
char as “string[]”
Identifiers Operators
Moving on to the Identifiers.
Each program element in C programming is known as an identifier.
The main uses of Identifiers are :
The naming of variables, functions, arrays, etc.
while the identifiers are user-defined names that consist of alphabets, numbers, underscore ‘_’. However, the name of the Identifiers should not be the same or the same as the keywords also the Keywords are not used as identifiers.
However, there are a set of rules for the naming C identifiers which are listed below as follows :
Rules −
- It must begin with alphabets or underscore.
- Only alphabets, numbers, underscore can be used, no other special characters, punctuations are allowed.
- It must not contain white space.
- It should not be a keyword.
- It should be up to 31 characters long.
Moving on to the Identifiers.
Each program element in C programming is known as an identifier.
The main uses of Identifiers are :
The naming of variables, functions, arrays, etc.
while the identifiers are user-defined names that consist of alphabets, numbers, underscore ‘_’. However, the name of the Identifiers should not be the same or the same as the keywords also the Keywords are not used as identifiers.
However, there are a set of rules for the naming C identifiers which are listed below as follows :
Rules −
- It must begin with alphabets or underscore.
- Only alphabets, numbers, underscore can be used, no other special characters, punctuations are allowed.
- It must not contain white space.
- It should not be a keyword.
- It should be up to 31 characters long.
Constants / literals
Moving on Constants.
Constants are also like normal variables. But, the only difference is that their values can not be modified by the program once they are defined. Constants refer to fixed values.
They are also called literals. Constants may belong to any of the data types
Syntax:
(or) const data_type *variable_name;const data_type variable_name;
Types of constants in C
Operators
Moving on the Operators
Operators are symbols that trigger an action when applied to C variables and other objects.
The data items on which operators act upon are called operands which depends upon the number of operands that an operator can act upon.
Operators can be classified as follows:
- Unary Operators: Those operators that require only a single operand to act upon are known as unary operators.For Example increment and decrement operators
- Binary Operators: Those operators that require two operands to act upon are called binary operators.
- Arithmetic operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Conditional / Increment and Decrement Operators
- Bitwise Operators
- Misc Operators
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Conditional / Increment and Decrement Operators
Operator Description ++ Increment −− Decrement
Bitwise Operators
Misc Operators
Special Symbols
Special Symbols in C are the primitive data type whose instances have a unique human-readable form. Symbols can be used as identifiers. Also known as " Atoms " in some programming languages, their uniqueness is enforced by holding them in a symbol table.
Howsoever there are various types of Special Symbols that are used in C having some special meaning and thus, cannot be used for some other purpose.
These various special symbols are shown below with their respected Symbols, Trivial names, and their functions.
- Brackets[]: Opening and closing brackets are used as array element references. These indicate single and multidimensional subscripts.
- Parentheses(): These special symbols are used to indicate function calls and function parameters.
- Braces{}: These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.
- Comma (, ): It is used to separate more than one statement like for separating parameters in function calls.
- Colon(:): It is an operator that essentially invokes something called an initialization list.
- Semicolon(;): It is known as a statement terminator. It indicates the end of one logical entity. That’s why each individual statement must be ended with a semicolon.
- Asterisk (*): It is used to create a pointer variable.
- Assignment operator(=): It is used to assign values.
- Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before the actual compilation.
Here is a table that illustrates some of the special characters in C:
No comments
If you have any doubts, please let us Know.