Breaking News

Tokens in C/C++


Tokens in C/C++ - Technotoken


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: 

  1. Keywords
  2. Strings  
  3. Identifiers  
  4. Constants  
  5. Operators  
  6. Special Symbols.

Now when we know what types of tokens are used in C. Let's Discuss each of these tokens one by one to understand it in much more depth.

Keywords

Starting with Keywords let's first understand what does keywords mean. 

Keywords are predefined, reserved words in C and each of which is associated with specific features. 
Now as we know that the keywords are pre-defined words and by predefined we mean that they can be used as Variable names but if the keywords are used as the variable names but you may think that it means you can assign a different meaning to the keyword but that is not allowed as the keywords have special meaning to the compilers.
 

However, there are a total of 32 keywords in C from where you can choose which one's you need to choose from which are as follows :


Keywords in C/C++ - Technotoken



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 −


 
Declarations for String - Technotoken



Here, when we declare 
char as “string[20]”
20 bytes of memory space is allocated for holding the string value.

And when we declare 
char as “string[]”
Memory space will be allocated as per the requirement during the execution of the program.


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.


Here is a table that illustrates the valid use of Identifiers :

Valid Use of Identifiers in C - Technotoken


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:


const data_type variable_name;
 (or) const data_type *variable_name;


Types of constants in C

 


Types of Constants in C - Technotoken



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. 

Binary operators are classified into : 

  1. Arithmetic operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Conditional / Increment and Decrement Operators 
  6. Bitwise Operators
  7. Misc Operators

Arithmetic Operators


Arithmetic Operators - Technotoken



Relational Operators

Relational Operators - Technotoken


Logical Operators


Logical Operators - Technotoken



Assignment Operators


Assignment Operators - Technotoken



Conditional / Increment and Decrement Operators

OperatorDescription
++Increment
−−Decrement


Bitwise Operators


Bitwise Operators - Technotoken



Misc Operators


Misc Operators - Technotoken


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.


special symbols used in C - Technotoken


 

  • 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:


Tokens in C ++ 


As C++ is the superset of C and so most constructs of C are legal in C++ with their meaning and usage unchanged. So all the expressions, data types, and Tokens in C++  are similar to that of C but if you want a separate post for that please feel free to comment.

 


So this is it guys if you want us to write more on this topic or have any particular Query regarding this post you can always comment or feel free to mail us at our official mail also follow us on Instagram and Facebook to receive Latest Updates first.


Peace!!!




No comments

If you have any doubts, please let us Know.