-2
// Pre-increment (or pre-decrement) 
#include <cstdio> 

int main() 
{ 
    int a = 10; 

    ++a = 20; // works 

    //printf("a = %d", ((++a)++)); 
    getchar(); 
    return 0; 
}

It is given in this article that ++a=20 works but when I am running on ideone, this code is giving an error.

https://www.geeksforgeeks.org/g-fact-40/

https://ideone.com/12DmS7

Swordfish
  • 12,971
  • 3
  • 21
  • 43

2 Answers2

4

This is one difference between C and C++.

In C, ++a is not an l-value, so it can't be on the left hand side of an assignment.

In C++ it can be.

Some compilers that purportedly compile C code (e.g. MSVC) emit the construct in error.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

This code works well in ideone too(See the screenshot below). Run it using C++ not C. Pre-increment works in C++ but it gives a compilation error in C.

enter image description here

Community
  • 1
  • 1
Niloy Rashid
  • 687
  • 4
  • 16