AnkitSablok
AnkitSablok

Reputation: 3159

Inconsistent arithmetic with characters in C++?

I was just playing with characters using a very simple C++ program, let me explain the situation -:

#include<iostream>

int main(){

    char c;
    std :: cin >> c;
    std :: cout << "The integer value of character entered is : " <<  int(c) << '\n';

    int m = 12 + 'á';

    std :: cout << m << '\n';

    return 0;
}

now when I execute the above program I enter the value of c as 'á' which is in the spanish character set and is typed as "Alt + 160" in windows and because my computer implements the plain old char as a signed char the above program outputs the integer value of 'á' as -96, but a strange thing happens when I output the value of m it returns the output as -19 instead of -84, while if I execute the following program -:

#include<iostream>

int main(){

    signed char c;
    std :: cin >> c;
    std :: cout << "The integer value of character entered is : " <<  int(c) << '\n';

    int m = 12 + c;

    std :: cout << m << "\n";

    return 0;
}

I get the correct output value, now I am confused as to why is this happening, if every character is backed by some number in the computer then why is not the expression m = 12 + 'á' evaluated as m = 12 + (-96). Kindly enlighten me regarding this issue. I am using Windows 7 and Dev C++

Upvotes: 1

Views: 161

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120079

I have just said that 160 is not a code for á... well I was wrong, it is, in code page CP437 aka DOS. In the Windows (CP1252) code page, á is 225, but apparently Windows does not use Windows code page in the console.

Your editor may or may not use CP437, CP1252, UTF8, or anything else. Look at your program in a hex editor to be certain. Better yet, never use anything but plain 7-bit ASCII in your program text, especially on Windows but in general everywhere else too. These things are not portable even between different computers running the same version of the same OS, and are not allowed by the standard. They will come and bite you. If you need non-ASCII character strings in your program, read them from a data file, never embed them in the source.

When you work with text in your program, always make sure a correct encoding is used. This is NOT simple, especially under Windows when using Visual Studio and standard C and/or C++ I/O libraries. I was not able to make this combination work with UTF-8.

Upvotes: 2

Related Questions