Expression in C++ programming

Shailendra Chauhan  13 min read
21 Jul 2023
Beginner
1.51K Views

Introduction

Do you want to learn a programming language that can help build user-friendly projects? C++ is an excellent choice because it's a powerful, flexible language - and with the right instruction through C++ online training, it can be easier to learn than you think! Expression in programming is a value that executes in a way that ends up being another value. A value is one of the unique features of programming. It is basically a syntactic entity that determines the value of that program.

What is Expression in C++ programming

C++ Expression is a particular entity that contains constants, operators, and variables and arranges them according to the rules of the C++ language. If the question is what is an expression in C++ then it can be answered that the c++ expression contains function calls that return values. Every expression generates some values that are assigned to that particular variable with the help of the assignment operator.

Example of what is an expression in c++

  1. (a-b) + c
  2. (x*y) -z
  3. 4a2 - 5b +c
  4. (a+b) / (x+y)

Different types of expressions in C++

There are different types of expression in c++ programming language is generated, which are:

  • Constant expressions
  • Integral expressions
  • Float expressions
  • Pointer expressions
  • Relational expressions
  • Logical expressions
  • Bitwise expressions
  • Special assignment expressions

Constant Expression in C++

What is constant in c++ programming language can be explained as this is an expression that contains only constant values. The value of this expression is determined in compile time but it is evaluated in run time. Constant definition in c++ can be composed of integer, floating point, character, and enumeration constants.

  • A constant can be used in the following situations,
  • It can be used in the declarator of the subscript for describing the array bound.
  • It can be used after the keyword, case in the switch statement.
  • It can be used as a numeric value in an “enum”
  • It helps to specify a bit-field width.
  • It is used as the pre-processor in the if statement
Expression containing constantConstant value
x = (2/5) * 4(2/5) * 4
extern int y = 8989
int z = 2121
static int a = 6060

Example

#include <iostream> 
using class std; 
int main() 
{ 
    int a; // variable declaration. 
    a=(4/3) + 2; // constant expression 
    cout<<"The value of a is: "<<a; // displaying the value of a. 
    return 0; 
} 

Output

The value of a is: 3

Integral Expression in C++

An integral expression is a type of expression in C++ that helps to produce the integer as output after doing all the implicit and explicit conversions.

Example of integral expression in c++

#include <iostream> 
using class std; 
int main() 
{ 
    int a; // variable declaration. 
    int b; // variable declaration 
    int c; // variable declaration 

    cout<<"Enter the values of a and b"; 
    cin>>a>>b; 
    c=a+b; 
    cout<<"\n"<<"Value of c is:"<<c; // displaying the value of c. 
    return 0; 
} 

Output

Enter the values of a and b 
8 
9 
Value of c is:17 

Float Expression in C++

Float expressions produce floating point values as an output of a program after doing all the explicit and implicit conversions.

Example of float expression in c++

#include <iostream> 
using class std; 
int main() 
{ 
   float a=8.9; // variable initialization 
   float b=5.6; // variable initialization 
   float c; // variable declaration 

   c=a+b; 
   std::cout <<"The value of c is:" << c<<std::endl; // displaying the value of c. 
     return 0; 
} 

Output

The value of c is:14.5

Pointer Expression in C++

This particular pointer expression in C++ produces an address value as an output of the program.

Example of pointer expression in c++

#include <iostream> 
using class std; 
int main() 
{ 
   int X[]={1,2,3,4,5}; // array initialization 
   int *ptr; // pointer declaration 
   ptr=X; // assigning base address of array to the pointer ptr 
   ptr=ptr+1; // incrementing the value of pointer 

   std::cout <<"value of the second element of an array: " << *ptr<<std::endl; 
   return 0; 
} 

Output

value of the second element of an array: 2

Relational Expression in C++

This Relational expression produces a particular type of value that can be either true or false which is also known as a boolean expression. If any arithmetic expressions are used on both sides of the Relational operator then the arithmetical expression will evaluate first then it will compare the results.

Example of relational expression in c++

#include <iostream> 
using class std; 
int main() 
{ 
    int X=45; // variable declaration 
    int Y=78; // variable declaration 
    bool Z= a>b; // relational expression 
    cout<<" The value of Z is :"<<Z; // displaying the value of Z. 
    return 0; 
} 

Output

The value of Z is : 0

Logical Expression in C++

Logical expression in c++ combines two or more relational expressions for producing a Boolean type of value.

Example of logical expression in c++

#include <iostream> 
using class std; 
int main() 
{ 
 int x=2; 
 int y=7; 
 int z=4; 
cout<<((x>y)||(x>z)); 
return 0; 
} 

Output

0

Bitwise Expression in C++

Bitwise expressions are used to manipulate data at a bit level for shifting it to bits.

Example of bitwise expression in c++

#include <iostream> 
using class std; 
int main() 
{ 
 int A=5; // variable declaration 
std::cout << (A>>1) << std::endl; 
return 0; 
} 

Output

2 

Special assignment Expression in C++

Special assignments in c++ can be classified depending on the value of the variable that is assigned

  • Chained assignment expressions- in chained assignment expression assigns the same values more than once by using only one statement

Example-

#include <iostream> 
using class std; 
int main() 
{
 int x; // variable declaration 
 int y; // variable declaration 
 x=y=80; // chained assignment 
 std::cout <<"Values of 'x' and 'y' are : " <<x<<","<<y<< std::endl; 
 return 0; 
} 

Output

Values of 'x' and 'y' are : 80,80
  • Embedded assignment expressions- In this embedded assignment expression one expression is enclosed within another assignment expressions

Example 

#include <iostream> 
using class std; 
int main() 
{
 int x; // variable declaration 
 int y; // variable declaration 
 x=10+(y=90); // embedded assignment expression 
 std::cout <<"Values of 'x' is " <<x<< std::endl; 
 return 0; 
} 

Output

Values of 'x' is 100

Compound assignment expressions-

This particular assignment operator combines the assignment operator and binary operator.

Example

#include <iostream> 
using class std; 
int main() 
{ 
  int x=10; // variable declaration 
  x+=10; // compound assignment 
  std::cout << " The value of x is :" <<a<< std::endl; // displaying the value of x. 
  return 0; 
} 

Output

The value of x is: 20

Summary

This article contains what an expression is in the C++ programming language, including different types of expressions in the C++ language with examples. With these expressions, you can make your C++ programming more efficient and concise, which can be beneficial when obtaining a C++ certification. As always, experiment with different ways of using them to see what works best for you and your coding style. We hope you have enjoyed learning about expressions in C++ programming. If you would like to learn more about this topic, please check out our other blog posts or contact us for more information.

Share
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this