简体   繁体   中英

Print extended ASCII char in C, Linux

Im trying to print all extended ASCII chars. I have found a code on forum:

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <iostream>

int main(void) {
    wchar_t c;
    setlocale(LC_ALL, "C");

    for (c = 128; c < 256; c++) {
        wprintf(L"char nr %d: %lc\n", c, c);
    }
    printf("\n%s\n", setlocale(0, NULL));

    std::cin.get();
    std::cin.get();
return 0;
}

This code is working in windows in VS 2017. 在此处输入图片说明

In the screenshot, you can see the result of this code in windows and Linux. I know that problem is with coding, but I don't know how to fix it.

Ok, all is fine. ASCII characters are limited below 128. What come above depends on the actual character set.

In Linux, you are displaying a subset of the ISO-8859-1 (aka Latin1) character set, while on Windows you are displaying the Windows code page 850. As you declare a UTF8 charset on Linux, you should only display error characters, but your terminal seems to interpret some bytes as latin1.

If you want to display all Latin1 characters, just change the LANG environment variable:

export LANG=pl_PL.ISO-8859-1

Or as you language seems to be Polish, ISO-8859-2 is probably more appropriate:

export LANG=pl_PL.ISO-8859-2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM