Sohil Grandhi
Sohil Grandhi

Reputation: 49

Not able to understand the working of Pre-increment/ Pre-decrement in C++

Can someone please explain what is happening in the following code? (Taken from GeeksForGeeks)

int main(){
 int a = 10;
 ++a = 20; // works
 printf("a = %d", a);
 getchar();
 return 0;
}

What exactly is happening when the statement ++a = 20 is executed? Also, please clarify why this code fails in execution?

int main(){
    int a = 10;
    a++ = 20; // error 
    printf("a = %d", a);
    getchar();
    return 0;
 }

Code Taken From: http://www.geeksforgeeks.org/g-fact-40/

Upvotes: 2

Views: 755

Answers (4)

haccks
haccks

Reputation: 105992

The left operand of the assignment operator should be an lvalue.
The expression ++a is an lvalue while a++ is not and therefore it can't be the left operand of the assignment operator.

n3797-§ 5.3.3(p1):

[...] The result is the updated operand; it is an lvalue [...]

Upvotes: 1

Lekens
Lekens

Reputation: 1969

int main(){
 int a = 10;
 ++a = 20; // works
 printf("a = %d", a);
 getchar();
 return 0;
}

This is a c Language. Explaining Line by line int main() THIS line define an entry function called main which is expected to return a type integer(int) int a = 10 declares a variable integer whose value is 10; ++a = 20; AT this point your code is incrementing the value of a by 1 before any operation is performed on a. This means that, the value of a is incremented by 1 bfore a is assign the value of 20;.... the statement ++a = 20 is incorect in the sense that, a is 10 initially, and you increment it to 11 . Is like saying 11 = 20; this may not throw an error because the code line is not useful. printf() is a c-method to print file to screen, passing a string "a = %d" tells the compiler to print a to a decimal number(%d). getchar() is use to terminate the running program and return 0 is to forcefully tell the operating system that the code run successfully and return integer value 0.

int main(){
    int a = 10;
    a++ = 20; // error 
    printf("a = %d", a);
    getchar();
    return 0;
 }

This didn't at a++ = 20 because , right value increment perform the operation before it increment unlike ++a who increment before performing operation. So this isn't possible and it never works because a is already 10, and you are saying the value ++ and assign 20, you can't assign a value to it, it should be the one calculating its own value. So the compiler will want to interprete is as variable a++ = 20, and ++ can not be a vaiable name. That's why it never work.

What is the essence of incrementation and decrementation in c....its useful for creating conditional statement e.g for loop, while statement etc. for example:

for(int i = 0; i < 4; i++)
{
printf('THis is c-language');
}

or

int i = 0;
while(i < 4){
printf('THis is c-Language');
i++;
}

so therefore, following the programming rules and regulations, you are not allow to assign value to either a++ or ++a they are for the compilers to manipulate, so never assign value to them. Thank you.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

When you do

++a = 20;

it's roughly equivalent to

a = a + 1;
a = 20;

But when you do

a++ = 20;

it's roughly equivalent to

int temp = a;
a = a + 1;
temp = 20;

But the variable temp doesn't really exist. The result of a++ is something called an rvalue and those can't be assigned to. Rvalues are supposed to be on the right hand side of an assignment, not left hand side. (That's basically what the l and r in lvalue and rvalue comes from.)

See e.g. this values category reference for more information about lvalues and rvalues.

Upvotes: 8

CIsForCookies
CIsForCookies

Reputation: 12807

This is the difference between r and l values. If you would have compiled the 2nd code snippet with gcc you would have seen this:

lvalue required as left operand of assignment

meaning, that a++ is rvalue and not a lvalue as it should be if you want to assign something to it

Upvotes: 1

Related Questions