Home > Blockchain >  I can't get the infinity symbol to print in Visual Studio 2019 using C
I can't get the infinity symbol to print in Visual Studio 2019 using C

Time:10-22

I'm trying to get the infinity symbol (∞) to print but I keeping getting garbage. I've tried everything mentioned here but nothing is working.

What I'm trying to accomplish is this

modifies strength by 9

I've tried

printf ("%c", 236);
printf ("%c", 236u);

and I get

modifies strength by 9 ì

I've tried

printf("∞");

and I get

modifies strength by 9 ?

I tried this

if ( paf->duration == -1 ){
    setlocale(LC_ALL, "en_US.UTF-8");
    wprintf(L"%lc\n", 8734);
    ch->printf("∞");

Just to see if I could get wprintf to print it but it completely ignores setlocale and wprintf and still gives me

modifies strength by 9 ?

I tried

if ( paf->duration == -1 ){
    std::cout << "\u221E";
    ch->printf("∞");

But got the this warning and error

Error   C2664   'int _CrtDbgReportW(int,const wchar_t *,int,const wchar_t *,const wchar_t *,...)': cannot convert argument 5 from 'int' to 'const wchar_t *'    testROS1a   C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\malloc.h   164 
Warning C4566   character represented by universal-character-name '\u221E' cannot be represented in the current code page (1252)    testROS1a   C:\_Reign of Shadow\TEST\src\CPP\act_info.cpp   3724    

which I can't make heads or tails of. I've exhausted the scope of my knowledge so does anyone know how to make this happen?

CodePudding user response:

This does the trick on my machine, windows code page (1252), not sure how universal this is though. I never really got to work a lot with unicode/localization stuff. And there always seems to be one more gotcha.

#include <iostream>
#include <io.h>
#include <fcntl.h>

const wchar_t infinity_symbol = 0x221E;

int main()
{
    // enable windows console to unicode
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << infinity_symbol;
}

CodePudding user response:

To use the Windows command prompt with wide strings, change the mode as follows, printf/cout won't work until the mode is switched back. Make sure to flush between mode changes:

#include <iostream>
#include <io.h>
#include <fcntl.h>

using namespace std;

int main()
{
    // To use wprintf/wcout and output any BMP (<= U FFFF) code point
    int org = _setmode(_fileno(stdout), _O_U16TEXT);
    wcout << L'\u221e' << endl;
    wprintf(L"\u221e\n");

    fflush(stdout);
    _setmode(_fileno(stdout), org); // to switch back to cout/printf and default code page
    cout << "hello, world!" << endl;
    printf("hello, world!\n");
}

Output:

∞
∞
hello, world!
hello, world!

If you use UTF-8 source and your compiler accepts it, you can also change the code page of the terminal to 65001 (UTF-8) and it could work with printf as is:

test.c

#include <stdio.h>

int main() {
    printf("∞\n");
}

Console output:

C:\demo>cl /W4 /utf-8 /nologo test.c
test.c

C:\demo>chcp
Active code page: 437

C:\demo>test          ## NOTE: Wrong code page prints mojibake.
∞                   ##       These are UTF-8 bytes interpreted incorrectly.

C:\demo>chcp 65001
Active code page: 65001

C:\demo>test
∞
  • Related