Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Tokens In C++ Explained


No replies to this topic

#1 livepcportal

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 221 posts
  • Gender:Male
  • Location:India
  • myCENT:40.55

Posted 31 August 2009 - 10:13 AM

Earlier I was writing this for my website but now I am sharing this with you guys so that who are either new to c++ or learning c++ can get some benefits. Hey mods this is my original work and I haven't copied this from any website.

Tokens
The smallest individual unit in a program is known as a token or a lexical unit. C++ has the following tokens:
  • Keywords
  • Identifiers
  • Literals
  • Punctuators
  • Operators
Keywords

Keywords are the reserve words that convey special meaning to the language compiler. A keyword cannot be used as normal identifier names.
Example of Keywords are given below :
  • asm
  • auto
  • bool
  • break
  • case
  • catch
  • char
  • class
  • Const
  • const_cast
  • continue
  • default
  • delete
  • do
  • double
  • dynamic_cast
  • else
  • enum
  • explicit
  • Export
  • extern
  • false
  • float
  • for
  • friend
  • goto
  • If
  • inline
  • int
  • long
  • mutable
  • namespace
  • new
  • Operator
  • private
  • protected
  • public
  • register
  • reinterpret_cast
  • return
  • Short
  • signed
  • sizeof
  • static
  • static_cast
  • struct
  • switch
  • Template
  • this
  • throw
  • true
  • try
  • typedef
  • typeid
  • typename
  • union
  • unsigned
  • using
  • virtual
  • void
  • volatile
  • wchar_t
  • while
Identifier

An identifier is an arbitrarily long sequence of letters and digits. It is also known as a variable. For example, abc, _123, etc.
Almost any name we can think of can be used as an identifier but there are some restrictions.
The following rules must be followed while naming an identifier:
  • It must not be a keyword.
  • It must be starting with an alphabet or an underscore ( _ ). It cannot be started with a digit.
  • It should not contain special characters.
  • It should be up to 1-10 characters long.
Literals(Constants)
Literals are data items that never change their value during a program run. In C++ following literals are allowed:

Integer Constant
An integer constant is a number without decimal that do not change its value throughout the program. An integer constant must have at least one digit that must not contain any decimal point.

Floating Constant
Floating constants are also called real constants. Real constants are the numbers with decimal point which do not change its value. It must have at least one digit before a decimal point and at least one digit after the decimal point In C++ a floating constant can be written in one of the two forms called fractional form or the exponent form.

Character Constant
A character constant is one character enclosed in single quotes, as in 'z'. A character constant in C++ must contain one character and must be enclosed in single quotation marks.

C++ allows us to have certain nongraphic characters in character constants. Nongraphic characters are those characters that cannot be typed directly from keyboard e.g., return, backspace, tabs etc.These nongraphic characters can be represented by using escape sequences. An escape sequence is preceded by a backslash (\).

String Constant
A string constant is a group of characters enclosed within double quotes. Every string is ended with null character ( \0 )

Operators
Operators is a symbol that triggers an action. In C++ following operators are allowed:

Arithmetic Operators

  • + (Addition)
  • -(Subtraction)
  • * (Multiplication)
  • /(Division)
  • %(Remainder/ Modulus)

Relational Operators

  • <(Less than)
  • >(Greater than)
  • <=(Less than or equal to)
  • >=(Greater than or equal to)
  • = =(Equal to)
  • !=(Not equal to)
All relational operators give answer in either true or false.

Logical Operators

In C++ we have three logical operators:

a). AND(&&) :In AND operator if anyone condition is false answer is also false.
:P. OR(||) :In OR operator if anyone condition is true answer is true.
c). NOT(!) :NOT operator is an unary operator and it negates the value of its operand(condition) e.g., !(0) returns 0 and !(a=10) returns 1.

Unary Operators
The following unary operators are available in C++

a). ++(Increment)

:P. - -(Decrement)

The increment operator adds 1 to its operands whereas the decrement operator subtracts 1 from its operand. The increment and decrement operators can be prefix or postfix. When they are used separately their meaning do not change but when they are used in an expression they follow the following rules:
Prefix: change and use
Post fix: use then change


Ternary Operators(Conditional Operators)
The only ternary operator available in C++ is ? : (conditional operator)
It takes the following format:

expression 1?expression 2: expression 3

Here expression 1 is a logical or a relational condition which is evaluated first. If it is evaluated to true then the value of the complete expression will be expression 2 otherwise it will be expression 3.

Comma Operators

The comma operator has the lowest precedence over other C++ operators. That is why comma operator is always given in brackets. It is evaluated from left to right and the evaluated right most value is the answer of the comma operator.

I hope this post might be useful for you. Please give your feedback and if you like this post don't forget to rate this post.

Thank you! ;)

Edited by livepcportal, 31 August 2009 - 10:20 AM.





Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users