1

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. enter image description here

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.

SRhm
  • 459
  • 1
  • 5
  • 11
magnus250
  • 97
  • 3
  • 12

1 Answers1

3

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
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • @magnus250: Run `sudo localedef -f ISO-8859-2 pl_PL pl_PL.ISO-8859-2` to define the Polish ISO-8859-2 locale, then retry. It will work. Everyone except Microsoft has basically moved to UTF-8 locales, and that's why that ISO-8859-2 variant is not installed in your Linux machine by default, and you need to install it yourself to replicate Windows behaviour. – Nominal Animal Jan 04 '19 at 20:07
  • @NominalAnimal I do this. When I put locale in console i get LANG=pl_PL.ISO-8859-2, but this code still not working :(. – magnus250 Jan 07 '19 at 09:46
  • okay, I do all your tips. I use printf("%d - \%c\n, i,i) to print all extended ascii and it works. Thanks you :) – magnus250 Jan 07 '19 at 14:19