knowledgediarybd.com

Your Virtual Knowledge Campus







Token in C ++

c++ token

In every human language, there are some grammatical rules to write something. Like human language, C++ has some rules to write code properly. These rules are known as token.

Dear reader, Here you can know details about various c++ token that are essential  when you write a c++ program.

There are six types of token in C++:

 

  1. Keywords

  2. Identifiers

  3. Constants

  4. String constants

  5. Operators and expressions

  6. Punctuators

Let a brief discuss about them.

Keywords

C++ has some reserved words to do some specific work. These words are known as keywords. For example, “int” is used to declare integer type variable.keywords in C++:

auto

do

goto

return

typedef

break

double

if

short

union

case

else

inline

signed

unsigned

char

enum

int

sizeof

void

const

extern

long

static

volatile

continue

float

register

struct

while

default

for

restrict

switch

A keyword can be used for a predefined work does not for other work. For example if we declare an int type variable named “char”, an error will be shown in display.

For Example:

#include <iostream>
using namespace std;

int main(){

int char;

return 0;

}

error

Identifiers

An identifier is to give a name of a variable, function, array or pointer so that compiler can identify these. For example,

int salary;                       //variable identifier

int sum_calculation();    //function identifier

Constants

Constants are user defined values which cannot be changed in the program. Constants can be declared in two ways:

  1.  Using “#define”

Format:       #define  <constant name><constant value>

Example:    #define  pi 3.1416

#define  TRUE 1

  1. Using “const” keyword

Format:       const <constant name> = <constant value> ;

Example:    const  pi = 3.1416;

String Constant

String is the collection of characters written in double quotation (“ ”). For example,

“This is a sample string”;

Keep mind that ‘a’ is a character but “a” is a string. A null string is just an empty string (“”). To insert a double quotation in a string we have to use a backslash (\). For example:

“He said, \“I am new in c++.\” ”;

Operators & Expression

Operator is used to operate something in C++. There are two types of operator in C++:

  1.  Mathematical operator

a + b           // “+” is an operator, to add two variables

a – b           //to subtract between two variables

  1.  Logical operator

a > b           //check if a is greater than b

a = b           //assign value of b to a

Punctuators

Semicolon (;), single quotation (‘), double quotation (“) and various braces ( [ ], (), {} ) are considered as token in C++. These are known as punctuators. For example, a semicolon must be used at the end of every statement of C++.

c = a+b;

int money, sum;

return 0;

Related post or you may like:

  1. Introduction & Fundamental of C++
  2. Code Blocks Compiler Download
  3. Variable In C++
  4. Data Types In C++
  5. C++ Operators,Operands and expression
  6. If, Else If..Else & Switch Statement
  7. For loop while loop&Do while loop in C++
  8. Continue and Break Statement In C++

 

knowledgediarybd.com © 2017
shares