Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

PLZ EXPLAIN THE answers of underlined snippets,also why the answer is coming 1 more?

+3 votes
asked Feb 17 by Ushnika (250 points)

#include <stdio.h>

int main()
{
    int a=10;
    printf("%d\t",a++ + a++);//10+11=21
    printf("%d\t",a);//a=12
    printf("%d\t",a++ + ++a);//12+14
    printf("%d\t",a);//a=14
    printf("%d\t",++a + a++);  //ERROR(15+15)=30  ANS =31
    printf("%d\t",a);//a=16
    printf("%d\t",++a + ++a);   //ERROR(17+18=35)   ANS=36
    printf("%d\t",a);//a=18
    return 0;
}

1 Answer

0 votes
answered Feb 18 by Peter Minarik (19,180 points)
edited Feb 22 by Peter Minarik

You have asked very similar questions in your previous posts where I explained how pre- and post-increments/decrements work:

http://question.onlinegdb.com/9389/plz-expain-the-underlined-snippet-of-c

Also, you should mark the correct answer with green and the wrong one with red, not the other way around (as it is right now).

Update

I've looked into this more closely and yes, you are right, OnlineGDB says the correct answers are 31 and 36 (correspondingly). Why?! Well...

I've found a similar thread online: https://www.element14.com/community/thread/46302/l/multiple-pre-incrementpost-increment-in-expression-of-c-language

The whole point is that the code in the example has undefined behaviour. That is, it's not a valid C code and should have not been written in the first place. The bahaviour may vary from compiler to compiler.

In C++ you can override the pre- and post-increment operators, and you could actually "Debug" what's going on:

#include <iostream>

class MyInt
{
private:
    int _value;
    
public:
    MyInt(int value) :
        _value(value)
    {}
    
    MyInt(const MyInt & other):
        _value(other._value)
    {}
    
    // Pre-increment
    MyInt & operator++()
    {
        _value++;
        std::cout << "++MyInt --> " << _value << std::endl;
        return *this;
    }

    // Post-increment
    MyInt operator++(int)
    {
        MyInt tmp(*this);
        //operator++(); // prefix-increment this instance
        ++_value;
        std::cout << "MyInt++ --> " << tmp._value << std::endl;
        return tmp;   // return value before increment
    }
    
    operator int() const
    {
        return _value;
    }
    
};

int main()
{
    MyInt i = MyInt(14);
    printf("%d\n", ++i + i++);
    
    int i2 = 14;
    printf("%d\n", ++i2 + i2++);

    return 0;
}

The output is this:

++MyInt --> 15
MyInt++ --> 15
30
31

It's clearly visible that using your own class (MyInt) and the built in value type int has different results

So why 31... The answer is: no. No. That question is pointless, the code has undefined behaviour. Other undefined behaviours would be the memory address of an uninitialised pointer, or trying to modify a string literal, etc. The C standard doesn't specify what should happen. Various compilers do their own logic.

I hope you don't leave more confused than when you asked the questions. :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...