Pre-increment (or pre-decrement) in C++

2.8

In C++, pre-increment (or pre-decrement) can be used as l-value, but post-increment (or post-decrement) can not be used as l-value.

For example, following program prints a = 20 (++a is used as l-value)

#include<stdio.h>

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

And following program fails in compilation with error “non-lvalue in assignment” (a++ is used as l-value)

#include<stdio.h>

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.8 Average Difficulty : 2.8/5.0
Based on 41 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.