Prefix and Postfix Operators

the prefix increment, decrement operators and their postfix counterparts.

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.

Postfix Increment and Decrement Operators

The postfix increment or decrement operators act on a single operand, and appear to the right side. Operators that act on only one operand are called unary operators.

The postfix increment operator is used to increment the value of its operand by 1 unit, and the postfix decrement operator is the decrement counterpart that decreases the value by 1 unit. You might have noticed, that I have written "1 unit" and not "1". There is a slight difference of behaviour when the operator operates on a value, and when it operates on an address. The primitive types like an int, char are value types, and in their case the value increases or decreases by 1. Here's the code-

int main()
{
    int x = 0;
 
    char c = 'A';
 
    x++;
 
    c++;
 
    // displays 1
    cout << x << endl;
 
    // displays B
    cout << c << endl;
 
    return 0;
}

If the same operator is applied to an address, the increase or decrease is equal to the number of bytes, and not 1. For example, if the operand is an address of a "short" type, then the increment is of 2 because a short occupies two bytes. If a type such as an int takes 4 bytes, then the increment will cause an increment of 4 units. Have a look at this code.


int main()
{
    int x = 0;
 
    int *p = &x;
 
    // displays the address
    cout << (int)p << endl;
 
    p++;
 
    // shows an increment of
    // 4 on my machine
    cout << (int)p << endl;
 
    return 0;
}

If the postfix increment is applied to a boolean type, the result is true. The postfix decrement cannot be applied to a boolean type; it is a compilation error.


int main()
{
    bool b1 = false;
 
    b1++;
 
    // true or 1
    cout << b1 << endl;
 
    // ERROR,
    // b1--;
 
    return 0;
}

It is not possible to apply postfix or prefix operators to an enum type.


int main()
{
    enum eWeekDays { MON, TUE, WED, THU, FRI };
 
    // ERROR
    eWeekDays++;
 
    return 0;
}

Prefix Increment and Decrement Operators

Prefix operators appear to the left side of their operand. They also increment or decrement the value by 1 unit.

Larger Expressions

When an expression is large, the postfix and prefix operators behave differently. In the code below, the postfix operator appears in a larger expression. In such cases the value is evaluated before the increment occurs. So y gets 8 + 0 = 8. x++ gives 0, and the increment of x in x++ occurs after.


int main()
{
    int x = 0;
 
    int y = (8 + x++);
 
    // displays 8
    cout << y << endl;
 
    return 0;
}

The behaviour of prefix increment in a larger expression can be seen in the code below. Here the value of x is increment before returning the value of x. So y gets 8 + 1 = 9.


int main()
{
    int x = 0;
 
    int y = (8 + ++x);
 
    // displays 9
    cout << y << endl;
 
    return 0;
}

Classroom Training

Classroom training in C/C++ is available at our training institute. Click here for details. You can also register yourself here.

Video Tutorial

We have created a video tutorial that can be downloaded and watched offline on your windows based computer. It is in the form of an exe file. No installation is required, it runs by double clicking the file. Since the exe file is hosted on the Google Drive, it should be very safe because Google itself checks for any virus or malware. The video can be watched offline, no internet is required.

Pricing

This single video will cost you USD $5. When you download the video, you will be able to watch the first few minutes for free, as a sample. The video app will prompt you for payment through PayPal and other payment options. If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials The entire set is priced at USD $50.

Screenshots

This is a screenshot of the software that should give you a good idea of the available functionality. Click on the image to see a larger view.screenshot of the video being played in the software

Installation

This software doesn't require any installation. Just download and double-click to run it. It doesn't require administrative priviliges to run. It runs in limited access mode, so should be very safe.

supports windows xp and later Supported Operating Systems

These videos can be run on 32-bit as well as 64-bit versions of the Microsoft Windows Operating Systems. Windows XP and all later OS are supported. If you want to run it on Windows XP and Windows Vista, then you must also have the .NET Framework Version 3.5 installed on your machines. Those users who have Windows 7 and later donot have to worry because these operating systems already have the .NET Framework installed on them.

Download Links

IMPORTANT NOTE: This download contains ONLY ONE video. The video is about the topic of this tutorial.

Want ALL Videos? If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials. TIP: If you plan to buy these videos, then it would be more economical to buy the entire set.

DISCLAIMER: Even though every care has been taken in making this software bug-free, but still please take a proper backup of your PC data before you use it. We shall not be responsible for any damages or consequential damages arising out of the use of this software. Please use it solely at your own risk.

Please download the software here.
download the video Prefix and Postfix Operators in C



Creative Commons License
This Blog Post/Article "Prefix and Postfix Operators" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-10-05