prev
next
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

C++ Programming Part 6

Rajjit May 27th, 2023 5 mins read
post-15

TOPIC: Constants, Manipulators and Operator Precedence in C++

Constants

  • By constant, it means a specific value assigned to a variable that cannot be changed. Once a value is assigned to a constant variable, it cannot be changed as it is fixed.
  • For example: const int a= 82;
    cout << "a = " << a;

    Here, the output will be 82 as the value is fixed when we declare the constant variable a.
copy
Let us look at the code about Constants in C++

C++ Program - Constants

Code written in Visual Studio Code

// Program to illustrate Constants in C++
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
          // CONSTANTS IN C++
          const int a = 3;
          int a = 444; // I will get an error because a is a constant
          cout << "The value of a is: " << a << endl;

          // This is the correct option
          // const int b = 82;
          // cout << "b = " << b;

          getch();
          return 0;
}
Output:
In function 'int main()':
error: conflicting declaration 'int a'
int a = 444; // I will get an error because a is a constant
    ^
note: previous declaration as 'const int a'
const int a = 3;
          ^

Manipulators

  • Manipulators are used to format the values of the variable in a specific way the user wants. If the user wants to view the value from the right, we can use setw() function to make it formatted and show it as required.
  • For accessing the manipulator functions, we use the header file iomanip which means input-output manipulator
copy
Let us look at the code about Manipulators in C++

C++ Program - Manipulators

Code written in Visual Studio Code

// Program to illustrate Manipulators in C++
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
          int a = 77, b = 777, c = 7777;
          cout << "The value of a without setw  is: " << a << endl;
          cout << "The value of b without setw  is: " << b << endl;
          cout << "The value of c without setw  is: " << c << endl;
                    
          cout << "The value of a is: " << setw(4) << a << endl;
          cout << "The value of b is: " << setw(4) << b << endl;
          cout << "The value of c is: " << setw(4) << c << endl;
                    
          getch();
          return 0;
}          
Output:
The value of a without setw  is: 77
The value of b without setw  is: 777
The value of c without setw  is: 7777
The value of a is:   77
The value of b is:  777
The value of c is: 7777

In the above output, the value is formatted to the right when we use setw()


Operator Precedence in C++

  • Like any other mathematical calculations, operations in programming also requires a of precedence of operators in order to perform calculations correctly.
  • We are going to look at the following table for the precedence of operators:
    Operators Precedence
    ++ (post increment), -- (post decrement) Highest
    ++ (pre-increment), -- (pre-decrement),
    sizeof, ! (not), - (unary minus), + (unary plus)
    * (multiply), / (divide), % (modulus)
    + (add), - (subtract)
    < (less than), <=(less than or equal),
    > (greater than), >= (greater than or equal)
    == (equal), != (not equal)
    && (logical AND)
    || (logical OR)
    ?: (conditional expression)
    = (simple assignment operator)
    comma operator lowest

    In the above table, we can see that the operators are given from Highest to lowest precedence of operators.

copy
Let us look at the code about Precedence of Operators in C++

C++ Program - Precedence of Operators

Code written in Visual Studio Code

// Program to illustrate Precedence of Operators in C++
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
          int a = 444, b = 454;
          int c = (a * 100) + b; 
          int d = ((((a * 100) + b) - 44) + 10);
          cout << c << endl;
          cout << d;
                    
          getch();
          return 0;
}          
Output:
44854
44820

You can write more code examples for checking precedence for yourselves. You can also check more about C++ operator precedence on website cppreference.com

More blog posts coming soon!

Coding is the art of turning imagination into innovation.
- Unknown