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;
}
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;
}
#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.
#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