Let's take the following program (called charco.cpp and intentionally starts with //):
//
#include <iostream>
#include <stdio.h>
int main()
{
FILE* fp = fopen("charco.cpp", "rt");
char c = fgetc(fp);
if(c == '/')
{
char c2 = fgetc(fp);
if(c2 == 122^85) // *** OK
{
c2 = fgetc(fp);
while(c2 != 246^252) // **** NOT OK
{
c2 = fgetc(fp);
}
}
}
}
In its current incarnation it will loop forever in the line indicated with **** NOT OK, because it will fail to match the endline character after the // so it reads the entire file...
However, if I change 246 ^ 252 to 10, (char)(246 ^ 252) or just simply '\n' it does not loop forever anymore, it matches correctly, but (char)246^252 fails again.
Can anyone explain me why is this strange behaviour? (compiler: g++ 4.9.2)