0

Whats the problem with the code below, and why I'm getting this error?

#include <iostream>
#define A 1
using namespace std;

int main()
{
   cout <<A++; 
   return 0;
}
  • 7
  • Make `A` an actual int variable and then you will be able to increment it. The value 1 is an [rvalue](https://stackoverflow.com/questions/9406121/what-exactly-is-an-r-value-in-c) and cannot be changed. Also [this](https://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c/) may be of interest. – Paul Rooney Jan 06 '20 at 01:53

2 Answers2

3

#define A 1 doesn't make a variable called A.

It tells your computer to replace all utterances of A with 1 before compiling.

So, your program is actually:

#include <iostream>
using namespace std;

int main()
{
   cout <<1++; 
   return 0;
}

And you cannot increment the literal 1.

You may read more about preprocessor directives in your C++ book.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0
#define A 1

A is not valid c++ left value, so gave us "lvalue required as increment operand"

int A; 

is valid c++ left value and will work, also of other simple number type float, unsigned char etc