C++ Basic Elements.

Basic Elements in C++ 

  • Programming language is a set of rules, symbols, and special words used to construct programs. 
  • There are certain elements that are common to all programming languages. Now, we will discuss these elements in brief :

www.mskuthar.blogspot.com
C++ Basic Elements

C++ Character Set

  • Character set is a set of valid characters that a language can recognize.
LettersA-Z, a-z
Digits0-9
Special CharactersSpace  +  -  *  /  ^  \  ()  []  {}  =  !=  <>  ‘  “  $  ,  ;  :  %  !  & ? _  #  <=  >=  @ 
Formatting charactersbackspace, horizontal tab, vertical tab, form feed, and carriage return

 

Tokens

  • A token is a group of characters that logically belong together. The programmer can write a program by using tokens. C++ uses the following types of tokens.
  • Keywords, Identifiers, Literals, Punctuators, Operators.

1. Keywords

  • These are some reserved words in C++ which have predefined meaning to compiler called keywords. It is discussed in previous section.

2. Identifiers

  • Symbolic names can be used in C++ for various data items used by a programmer in his program. A symbolic name is generally  known as an identifier. 
  • The identifier is a sequence of characters taken from C++ character set. The rule for the formation of an identifier are:
  • An identifier can consist of alphabets, digits and/or underscores.
  • It must not start with a digit
  • C++ is case sensitive that is upper case and lower case letters are considered different from each other.
  • It should not be a reserved word.

3. Literals

  • Literals (often referred to as constants) are data items that never change their value during the execution of the program. The following types of literals are available in C++.
  • Integer-Constants
  • Character-constants
  • Floating-constants
  • Strings-constants

Integer Constants

  • Integer constants are whole number without any fractional part. C++ allows three types of integer constants.
  • Decimal integer constants : It consists of sequence of digits and should not begin with 0 (zero). For example 124, - 179, +108.
  • Octal integer constants: It consists of sequence of digits starting with 0 (zero). For example. 014, 012.
  • Hexadecimal integer constant: It consists of sequence of digits preceded by ox or OX.

Character constants

  • A constant character in C++ must contain one or more characters and be included in single quotation marks. For examples,' A',' 9' etc. C++ requires non-graphical characters that can not be accessed directly from the keyboard, such as backspace, line, carriage return etc. 
  • Using an escape sequence will reflect certain characters. Single character depicts an escape sequence.

Floating constants

  • Also, they are called real constants. These are numbers which have fractional sections. They may be written in an exponent or fractional form. 
  • A real fractional constant consists of signed or unsigned digits with one decimal point between digits. For instance the 3.0, -17.0, -0.627 etc.

String Literals

  • A character series enclosed inside double quotes is called a literal string. By default, string literal is inserted (automatically) with a special character' \0' that denotes the end of the string. So one character increases the size of a string. 
  • For instance, "COMPUTER" will re-represent in the memory as "COMPUTER\0" and its size is 9 characters.

4. Punctuators

  • The following characters are used as punctuators in C++.
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

  • Operators are special symbols used for specific purposes. C++ provides six types of operators. 
  • Arithmetical operators, Relational operators,  Logical operators, Unary operators, Assignment operators, Conditional operators, Comma operator

Arithmetical operators

  • Arithmetical operators +, -, *, /, and % are used to performs an arithmetic (numeric) operation. 
  • You can use the operators +, -, *, and / with both integral and floating-point data types. 
  • Modulus or remainder % operator is used only with the integral data type.
  • Operators that have two operands are called binary operators.

Relational operators

  • The relational operators are used to test the relation between two values. All relational operators are binary operators and therefore require two operands. 
  • A relational expression returns zero when the relation is false and a non-zero when it is true. The following table shows the relational operators.
Relational OperatorsMeaning
Less than
<=Less than or equal to
==Equal to
Greater than
>=Greater than or equal to
! =Not equal to

Logical operators

  • The logical operators are used to combine one or more relational expression. The logical operators are
OperatorsMeaning
||OR
&&AND
!NOT

Unary operators

  • C++ provides two unary operators for which only one variable is required.

For Example
a = - 50;
a = + 50;
  • Here plus sign (+) and minus sign (-) are unary because they are not used between two variables.

Assignment operator

  • The assignment operator '=' is used for assigning a variable to a value. This operator takes the expression on its right-hand-side and places it into the variable on its left-hand-side. For example:
          m = 5;
  • The operator takes the expression on the right, 5, and stores it in the variable on the left, m.

          x = y = z = 32;
  • This code stores the value 32 in each of the three variables x, y, and z.
in addition to standard assignment operator shown above, C++ also support compound assignment operators.

Compound Assignment Operators

OperatorExampleEquivalent to
+ =A + = 2A = A + 2
- =A - = 2A = A - 2
% =A % = 2A = A % 2
/=A/ = 2A = A / 2
*=A * = 2A = A * 2

Increment and Decrement Operators

  • C++ includes two special operators viz ' + + ' and ' — ' to increase and decrease the variable value by 1. 
  • The increment / decrement operator can be used with any sort of variable but with no constant can be used. 
  • The operators of increments and decrements each have two forms, pre and post.
The syntax of the increment operator is:
  • Pre-increment: ++variable
  • Post-increment: variable++

The syntax of the decrement operator is:
  • Pre-decrement: ––variable
  • Post-decrement: variable––
  • In Prefix form first variable is first incremented/decremented, then evaluated
  • In Postfix form first variable is first evaluated, then incremented/decremented
int x, y;
int i = 10, j = 10;
x = ++i; //add one to i, store the result back in x
y = j++; //store the value of j to y then add one to j
cout << x; //11
cout << y; //10 

Conditional operator

  • The conditional operator ?: is called ternary operator as it requires three operands. The format of the conditional operator is:

Conditional_ expression ? expression1 : expression2;
  • If the value of conditional expression is true then the expression1 is evaluated, otherwise expression2 is evaluated.
int a = 5, b = 6;
big = (a > b) ? a : b;
  • The condition evaluates to false, therefore biggets the value from b and it becomes 6.

The comma operator

  • The comma operator gives left to right evaluation of expressions. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.

int a = 1, b = 2, c = 3, i; // comma acts as separator, not as an operator
i = (a, b); // stores b into i
  • Would first assign the value of a to i, and then assign value of b to variable i. So, at the end,  variable i would contain the value 2.

The sizeof operator

  • As we know that different types of Variables, constant, etc. require different amounts of memory to store them The sizeof operator can be used to find how many bytes are required for an object to store in memory. 
For example
sizeof (char) returns 1
sizeof (float) returns 4
the sizeof operator determines the amount of memory required for an object at compile time rather than at run time.

The order of Precedence

  • The order in which the Arithmetic operators (+,-,*,/,%) are used in a. given expression is called the order of precedence. The following table shows the order of precedence.
OrderOperators
First
Second
Third
()
*, /, %
+, -
  • The following table shows the precedence of operators.
++, --(post increment/decrement)

Highest
To

Lowest
++ (Pre increment) -- (Pre decrement), sizeof ( ), !(not), -(unary), +(unary)
*,/, %
+, -
<, <=, >, >=
==,!=
&&
? :
=
Comma operator

Post a Comment

Previous Post Next Post