I am currently working on a more complex program, and I came across a very weird syntax error best demonstrated with the following minimal example:
#include <iostream>
int main(int argc, char *argv[]){
char c = 1 + '0';
std::cout << 1 + '0' << std::endl;
std::cout << c << std::endl;
std::cout << '0' + 1 << std::endl;
return 1;
}
This example produces the following output:
$ ./program
49
1
49
What appears to be happening here is that when the conversion from a single digit integer expression to a character happens outside a stream statement, it succeeds, but when it happens inside such a statement, it produces a garbage answer.
I tried to find someone else asking something similar on Google, but I can't find anything relevant.
I am using g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
on Ubuntu 16.04 LTS x64, but the issue occurs in clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
as well, which rules out a compiler bug.