Navratri Sale: Get Upto 30% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
What is Expressions in C++ | Types of Expressions in C++ ( With Examples )

What is Expressions in C++ | Types of Expressions in C++ ( With Examples )

21 May 2024
Beginner
9.01K Views
16 min read
Learn via Video Course & by Doing Hands-on Labs

Free C++ Course Online with Certificate [Full Course]

Expressions in C++ Programming: An Overview

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.

What is Expression in C++ Programming

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)

Read More - C++ Interview Interview Questions and Answers

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

Different types of expressions in C++

1.) 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 in C++ Editor


#include <iostream> 
using namespace 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

2.) 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 namespace std;

int main() {
 int a; // variable declaration.
 int b; // variable declaration
 int c; // variable declaration
 a=8; 
 b=9;
 c = a + b;
 cout << "\nValue of c is: " << c; // displaying the value of c.
 return 0;
}

Output

Value of c is:17 

3.) 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 namespace std;

int main() {
 float a = 8.9; // variable initialization
 float b = 5.6; // variable initialization
 float c; // variable declaration

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

Output

The value of c is:14.5

4.) 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 namespace std;

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

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

Output

value of the second element of an array: 2

5.) 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 namespace std;

int main() {
 int X = 45; // variable declaration
 int Y = 78; // variable declaration
 bool Z = X > Y; // relational expression
 cout << "The value of Z is: " << Z << endl; // displaying the value of Z.
 return 0;
}

Output

The value of Z is : 0

6.) Logical Expression in C++

Logical expression in C++ combines two or more relational expressions to produce a Boolean type of value.

Example of logical expression in C++ Online Compiler


#include <iostream> 
using namespace std; 

int main() 
{ 
 int x = 2; 
 int y = 7; 
 int z = 4; 

 cout << ((x > y) || (x > z)); 

 return 0; 
} 

Output

0

7.) 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 namespace std; 

int main() 
{ 
 int A = 5; // variable declaration 
 cout << (A >> 1) << endl; 
 return 0; 
} 

Output

2 

8.) 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-1


#include <iostream> 
using namespace std; 

int main() 
{
 int x; // variable declaration 
 int y; // variable declaration 
 x = y = 80; // chained assignment 
 cout << "Values of 'x' and 'y' are: " << x << "," << y << 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-2


#include <iostream> 
using namespace std; 

int main() 
{
 int x; // variable declaration 
 int y; // variable declaration 
 x = 10 + (y = 90); // embedded assignment expression 
 cout << "Value of 'x' is " << x << endl; 
 return 0; 
} 

Output

Values of 'x' is 100

9.) Compound assignment expressions-

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

Example of compound assignment expressions in C++ Online Compiler


#include <iostream> 
#include 
using namespace std; 

int main() 
{ 
 int x = 10; // variable declaration 
 x += 10; // compound assignment 
 cout << "The value of x is: " << x << 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. 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 Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Advanced Full-Stack .NET Developer Certification TrainingOct 05SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification TrainingOct 05SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Angular Certification TrainingOct 06SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Full-Stack .NET Developer Certification TrainingOct 06SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core ProjectOct 19SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
.NET Solution Architect Certification TrainingOct 20SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Microservices Certification TrainingOct 20SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification TrainingOct 20SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Certification TrainingOct 20SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Azure Developer Certification TrainingOct 27SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by 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 9th time in a row (2016-2024). 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.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this