-2

I tried using wchar_t but it gave a warning and print weird characters in the terminal. or printing directly gave the same result

cout<<"Ø"<<"  "<<"";

gives output:

Ø  𐌟
rustyx
  • 80,671
  • 25
  • 200
  • 267
Riaun
  • 31
  • 3
  • 1
    There is no list of characters that C++ can or cannot print. Every C++ implementation is different, every output device is different, every font is different. To get a useful answer you need to provide much more detail, compiler, terminal software, operating system etc. etc. (OK, just noticed the tags, compiler seems to be gcc, but still really need to know the operating system). – john Jun 07 '23 at 16:58
  • 1
    Show the code you're using for the output. The source code encoding could be important too as well as any encoding-related compiler options? Did you try `std::wcout << L"Ø\n";`? – fabian Jun 07 '23 at 17:06
  • In C++ you can use `wchar`. C has no idea about those characters though. – tadman Jun 07 '23 at 17:10
  • C or C++, pick one and remove the other tag. – Barmar Jun 07 '23 at 17:11
  • Don't waste your time with ChatGPT. It doesn't know how to program (it doesn't *know* anything, except how to generate realistic-seeming word patterns). – Barmar Jun 07 '23 at 17:12
  • Is your console set up to receive and display special characters? – Thomas Matthews Jun 07 '23 at 17:12
  • @ThomasMatthews how to setup the console to do so? – Riaun Jun 07 '23 at 17:33
  • @fabian Yes I did it was of no avail, same output – Riaun Jun 07 '23 at 17:35
  • 1
    @Questor windows using vs code – Riaun Jun 07 '23 at 17:36
  • @DivyanshNirman Depends on the Operating system. Linux, Mac, Windows? What version (Ubuntu, Arch....) (SNow Leapord etc...) (7, 8, 10 etc...) – Questor Jun 07 '23 at 17:36
  • @john os: windows – Riaun Jun 07 '23 at 17:38
  • Consoles are different. You'll need to see which console your operating system uses, then research the options for that console or terminal. For example, Windows and Linux have different consoles. – Thomas Matthews Jun 07 '23 at 17:39
  • Why did I get downvoted this was a genuine question, It was for my first cpp project, and I was replacing x with and o with Ø, this was my first question in the platform as well – Riaun Jun 07 '23 at 17:41
  • C++ isn't required to parse UTF8 source code to begin with, so a standards compliant compiler wouldn't even compile your code as is. – Blindy Jun 07 '23 at 17:43
  • @DivyanshNirman *Yes I did it was of no avail, same output* -- Did you try outputting those characters to a text file? If you did that, and then tried to view that file in an editor, you may have the same issues that the console is having. – PaulMcKenzie Jun 07 '23 at 17:43
  • @DivyanshNirman Probably mostly for not showing the code you have written and also not providing much backgrouhd detail. – john Jun 07 '23 at 17:44
  • @DivyanshNirman Writing non-ASCII characters on Windows consoles is quite difficult. The main issues are 1) choosing a encoding to represent those characters (UTF-8 or UTF-16 are popular choices) 2) getting strings that use the chosen encoding 3) setting the console to handle that encoding. Quite a lot to explain here, but if you search the site this question has been asked and answered many times. – john Jun 07 '23 at 17:47
  • @DivyanshNirman It's also quite tricky for a beginner (which you seem to be). My advice would be to stick to ASCII, if you can. – john Jun 07 '23 at 17:51
  • @DivyanshNirman Now I've seen the code. The issue you seem completely unaware of is *character encoding*, you have little chance of writing working code unless you understand that concept. Just because you can write a particular character in your source code, does not mean that you are going to see the same character in the output, because the source code and the output console may be using different character encodings. – john Jun 07 '23 at 17:55
  • It's hard to tell from the code snippet — since it is not a complete program — but did you `setlocale(LC_ALL, ".utf8");`? – Eljay Jun 07 '23 at 17:59
  • @Blindy standard allows implementations to use encoding as they see fit, even unicode entity names are permitted. Compliance suggests meeting requrements, not disabling everything left outside of the document. – Swift - Friday Pie Jun 07 '23 at 18:09
  • @Eljay I don't know about that syntax, it wasn't used, the code I provided was the all – Riaun Jun 07 '23 at 18:10
  • Could anyone suggest which book to refer to learn cpp extensively? – Riaun Jun 07 '23 at 18:12
  • @Swift-FridayPie, what are you trying to say, that his code is valid because the standard didn't explicitly declare it otherwise? Because that's not how it works, just because you find a compiler that goes beyond what the standards require of it and compile it as expected, it doesn't mean that *every* compiler will. Or do you have any other point you're trying to make that I'm somehow missing? – Blindy Jun 07 '23 at 18:14
  • 1
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Blastfurnace Jun 07 '23 at 18:15
  • @Blindy ANY real compiler is beyond standard. They adhere to pltform s specs too. it's not governed by standard. And you said that it forbidden by standard, which is not true. Being not compliant equals to allowing forbidden behavior, allowing ill-formed code to compile, allowing behavior different from explicitly stated by standard). – Swift - Friday Pie Jun 09 '23 at 09:54
  • @Blindy See code in answer. It also could be shorter, setconsole calls not always required with Microsoft platform\compiler, if build tool is set up properly. – Swift - Friday Pie Jun 09 '23 at 10:04
  • You're confusing the runtime (`setconsole`) with source code. C++ compilers pre-C++20 are not required to compile Unicode source code in any encoding. In other words, I could write a C++17 compliant compiler right now that just throws an error on OP's source code, and all anyone can say is "yes, that's standard compliant". C++20 changes this so that the C++ source code character set is Unicode, but it does *not* require it to be UTF8, so again OP's source code is not standard compliant. – Blindy Jun 09 '23 at 15:56
  • @Blinder older versions were allowing unicode. Identifiers in code could be in Unicode (albeit I saw how that can cause issues, e.g. Russian 'с' and latin 'c' look same). **If there is no requirement, there cannot be compliance.** It IS compliant, the support of UTF8 is an implementation-specific detail. Now if you want it portable you have to push code through some string literal\source code conversion before compiling (which, with some toolchains can be do seamlessly during the build) – Swift - Friday Pie Jun 10 '23 at 08:21
  • @Blindy A C++ compiler is not required to recognize UTF-8 by the standard, but this is a problem with the standard, not with the OPs code. The de facto state of the art is that all relevant C++ compilers can do UTF-8. There is nothing wrong whatsoever in relying on that. No one is obliged to write code that is portable to every imaginable compiler. Being reasonably portable in practice is very good (better than most programs can ever dream to be). – n. m. could be an AI Jun 10 '23 at 19:58

1 Answers1

2
#include <string>
#include <iostream>
#include <Windows.h>
#include <cstdio>


int main(){
// Set console code page to UTF-8 so console known how to interpret string data
    SetConsoleOutputCP(CP_UTF8);

// Enable buffering to prevent VS from chopping up UTF-8 byte sequences
    setvbuf(stdout, nullptr, _IOFBF, 1000);

    std::cout<<"Ø"<<"  "<<"";
}

I got the answer, It prints the value as expected

Riaun
  • 31
  • 3
  • That's good but you have to be aware of one thing. For the above code you work you have to save your source file in UTF-8. Instead of `"Ø"` you could try `"\xC3\x98"`. C3 98 is the UTF-8 encoding of Ø, so this version works whatever encoding you save your source file in. – john Jun 07 '23 at 18:15
  • @john At the price of being an unreadable pile of hex codes, yes. Having to save your source file in UTF-8 is an absolute no-brainer. Why on God's green earth would you want to do anything else? – n. m. could be an AI Jun 11 '23 at 06:38
  • @n.m. Just pointing out that this is an potential pitfall. Saving as UTF-8 does not happen by default using Visual Studio for example. – john Jun 11 '23 at 07:21
  • You don't compromise readability of your code because some editor has a default designed for year 1998. You change the default instead. – n. m. could be an AI Jun 11 '23 at 07:28
  • @n.m. Eh, you could say that about whole OS and platform. Windows platform stays wchar with 8-bit character console by default for observable future. They opt for supporting outdated development tecgniques and legacy code. – Swift - Friday Pie Jun 11 '23 at 15:36