Thread: Character literals incorrectly interpreted

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Character literals incorrectly interpreted

    On my system, outputting a character constant such as 'ä' or 'ö' to screen will result in an incorrect character being output (or an incorrect decimal value if %d is used in printf). Output of the character constants to disc file will result in a correct character output but an incorrect decimal output.

    Incorrect output also results when such characters are read from a disk file to an integer variable and that variable is then output as a decimal value or as a character. However, if the characters are read in from the keyboard to a variable they are interpreted correctly.


    The problem happens with both Code::Blocks and VC++ 2008 (English-language software on a non-English-language system).

    Any suggestions?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I suspect that this problem can be more likely to be down to the drivers rather than to be your compiler it self. Not really sure, try reinstall or update youre display drivers. See if that any help?

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    But generally the computer behaves fine. And if I run something like
    for (i = 0; i < 256; i++) printf(%d, %c); the output is entirely as expected too. It's only character constants that something somewhere (presumable in the compilers?) seems to be having difficulty understanding correctly.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    That should have been printf("%d, %c\n", i, i), of course.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    OK, can you post your const ascii value that youre trying to print. Perhaps yout whole snip of code which includes your constant value init and printing ??

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Something like this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int i, l;
        int j ='ö';
        FILE *fptr;
    
        /*character constant*/
        printf("integer constant 148\t\t- as decimal: %d\tas character: %c\n", 148, 148);
        printf("int character constant '\x94'\t- as decimal: %d\tas character: %c\n",j, j);
    
        /*from keyboard*/
        printf("Enter '\x94': ");
        l = getchar();
        printf("Character entered\t\t- as decimal: %d\tas character: %c\n\n",l, l);
    
        /*writing to and then reading from file*/
        printf("int character constant '\x94' written to file, then read from same file as: ");
    
        fptr = fopen("file.txt", "w");
        fputc(j, fptr);
        fclose(fptr);
    
        fptr = fopen("file.txt", "r");
        while ((i = getc(fptr)) != EOF)putchar(i);
        fclose(fptr);
        printf("\n");
    
        return 0;
    }

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well, it seems to be working absolutly fine on my machin. I am using Dev-C++ and here is the output of mine.

    Code:
    integer constant 148            - as decimal: 148       as character: ö
    int character constant 'ö'      - as decimal: -10       as character: ÷
    Enter 'ö':
    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Well that is exactly the output I get too. But I don't think it's fine at all: its outputting ö as ÷.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    did you try setting the appropiate locale? Sometimes the console will display an incorrect character even if the character literal is correct

  10. #10
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    No, I didn't. How would you do that?

  11. #11
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Code:
    #include <locale.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       if(!setlocale(LC_CTYPE, ".1257"))
          {
          fputs("Failed to set locale"\n", stderr);
          abort();
          }
       putc('ö', stdout);
    }
    The .1257 represents the Baltic codepage which appears to include the ö character. The above code seems to work with Microsoft's CRT; keep in mind however, that locale names aren't standardized and that they are usually different across platforms.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Thanks, I will look into that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  2. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  4. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 10:31 PM
  5. Replies: 12
    Last Post: 01-12-2002, 09:57 AM