C++ Basic Elements

Any programming language is a set of rules applied to symbols and special words which are used to construct a program. There are some elements that are similar in most of the programming languages including C++. Therefore before moving to the next sections, we define those basic elements. These basic elements include a set of characters and tokens. The tokens are categorized into five types which are discussed below.

C++ Character Set

There are certain characters which any programming language can recognize while we write the code. The set of those character constitutes the character set. In C++ following character set is recognizes. Using this set of characters the C++ code is written. Any other character is not allowed.

Letters a-z, A-Z
Digits 0-9
Special Characters Space  +  –  *  /  ^  \  ()  []  {}  =  !=  <>  ‘  “  $  ,  ;  :  %  !  & ? _  #  <=  >=  @
Formatting characters  horizontal tab, vertical tab, form feed, backspace and carriage return

Tokens

A token in programming language is group of characters. Any program consists of tokens. The C++ tokens are consists of keywords, identifiers, literals, punctuators, operators.

  1. Identifiers

    A symbolic name use to identity various data items are known as identifiers. These are composed by sequence of characters which are taken from the set of the characters. There are some rules for the formation of identifiers which are

    The identifier can only consists of alphabets, digits and/or underscores. the identifier must not start with a digit. The identifiers must not be a reserved word. As C++ is case sensitive language therefore identifiers are also.

  2. Keywords

    There are some reserved words which have specific meanings and are known to compiler. The details of the C++ keywords are given in the list of C++ keywords.

  3. Literals

    Literals are commonly known as constants. These are data items which whose values never change in the execution of the pr0gram. There are four types of literals that are used in C++ language. which are follwing

     Integer Constants: Those numbers whose fractional part is not defined are called integers. There are three types of integers constants which are allowed in C++. These are

    Hexamdecimal Integer Constant: In this manner hexadecimal numbers are written which are perdeded by 0x or OX.

    Decimal Integer Constants: These integers consists of sequesnce of decimal digits. These digits should not start with zero for exam 555, -24, +987 etc.
     

    Character constants

    The character constants are defined by enclosing the characters in single quotation marks e.g. ‘A’,’ABc’,’A94f’. In C++, non-graphic characters such backspace, tab and carriage return are not allowed directly instead they are represented using escape sequence. An escape sequence represents a single character. For example if we need to define tab sequence then we put \t within single quotation marks.

     Floating constants

    The numbers having fractional part can be defined as constants as well and these are called floating constants. These are also known as real constant. The real constants are defined in the fraction from and are signed or unsigned digits. For example -44.04, 3.000 etc.

     String Literals

    The sequence of characters if enclosed within double quotation marks are called string literals.  A special character called null character i.e.’\0′ is added by default. It denotes the end of the string. It causes to to increase the size by one character. For example if we define a string literals as “tutorialsart.com” then it will be represented in the memory as “tutorialsart.com\0”.  The total character count will be seventeen in this case.

  4. Punctuators

    The punctuation marks used in C++ have some meaning. Not all of the punctuation marks are used. The allowed punctuators and their meanings are given as

    Brackets [   ] Opening and closing brackets indicate single and multidimensional array subscript.
    Parentheses (   ) Opening and closing brackets indicate functions calls,; function parameters for grouping expressions etc.
    Braces {   } Opening and closing braces indicate the start and end of a compound statement.
    Comma , It is used as a separator in a function argument list.
    Semicolon ; It is used as a statement terminator.
    Colon : It indicates a labeled statement or conditional operator symbol.
    Asterisk * It is used in pointer declaration or as multiplication operator.
    Equal sign = It is used as an assignment operator.
    Pound sign # It is used as pre-processor directive.
  5. Operators

    There are special symbols used for special purpose in C++.  There are six types of operators in C++ which are Arithmetic Operators, Relational Operators, Logical Operators, Unary Operators, Assignment Operators, Conditional Operators and Comma operator. For details of the operators please look at this tutorial.