< Prev
Next >



Decrement Operator in C++










Pre-decrement operation


This operation is named pre-decrement because the decrement operator(--) comes before the operand.

Sequence of operation in pre-decrement operation is -

//Decrement Operator Example in C++

#include<iostream>

using namespace std; 

int main()
{
char ch= 'b';;
short sh= 99;
int i=9;
long l=9999;
float f=19.5f;
double d= 10.5;


cout<< "Original char : " << ch <<"\n";
cout<< "Decremented char : " << --ch << "\n";


cout<< "Original short : " << sh << "\n";
cout<< "Decremented short : " << --sh <<"\n";


cout<< "Original int : " << i << "\n";
cout<< "Decremented int : " << --i <<"\n";

cout<< "Original float : " << f << "\n";
cout<< "Decremented float : " << --f <<"\n";

cout<< "Original double : " << d << "\n";
cout<< "Decremented double : " << --d << "\n";

return 0;
}


Output is


Original char : b
Decremented char : a
Original short : 99
Decremented short : 98
Original int : 9
Decremented int : 8
Original float : 19.5
Decremented float : 18.5
Original double : 10.5
Decremented double : 9.5





Post-Decrement operator




// Post-decrement operator example in C++

#include<iostream>

using namespace std; 

int main()
{
char ch= 'd';;
short sh= 101;
int i=1;
long l=999999;
float f=2.5f;
double d= 19.555;


cout<< "Original char : " << ch <<"\n";
cout<< "Decremented char : " << ch-- << "\n";


cout<< "Original short : " << sh << "\n";
cout<< "Decremented short : " << sh-- <<"\n";


cout<< "Original int : " << i << "\n";
cout<< "Decremented int : " << i-- <<"\n";

cout<< "Original float : " << f << "\n";
cout<< "Decremented float : " << f-- <<"\n";

cout<< "Original double : " << d << "\n";
cout<< "Decremented double : " << d-- << "\n";

return 0;
}


Output -


Original char : d
Decremented char : d
Original short : 101
Decremented short : 101
Original int : 1
Decremented int : 1
Original float : 2.5
Decremented float : 2.5
Original double : 19.555
Decremented double : 19.555
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2