-1

How can I print for example "¶"?

I need to print some ASCII extended characters but obviously printf("¶"); dosen't work. Some other posts have taken me to this webpage but I don't know yet if there is any way to do it.

I would appreciate some help. Thanks in advance.

  • 1
    Be aware that your editor might save your source code as UTF-8, which means "extended" characters like that won't be what you expect. – Some programmer dude Apr 05 '21 at 11:13
  • 1
    More importantly, what character encodings are you using a) in your source code and b) in the terminal where you run this program? – Gereon Apr 05 '21 at 11:13
  • 1
    Also what terminal or other tool (a text editor, for example) do you want to show the result in? – MikeCAT Apr 05 '21 at 11:13
  • If you're absolutely sure about the encoding of the output terminal, you might be able to do `printf("\xf4")` – Tordek Apr 05 '21 at 11:14
  • 3
    It is better to use unicode instead of extended ASCII, in my opinion – Suyash Krishna Apr 05 '21 at 11:16
  • I use Eclipse and Visual Studio Code. – Iker Morales Apr 05 '21 at 11:31
  • printf("\xf4") actually works. But how did you get that "\ xf4" is "¶"? I can't find it anywhere. – Iker Morales Apr 05 '21 at 11:47
  • 1
    You can find ascii code of "¶" in the webpage you mentioned in your question. It's 244. And `244` is `f4` in hexadecimal. You can print chars with hexadecimal ascii code by prefixing `\x` – Ultim8_Clock Apr 05 '21 at 12:07
  • @Ultim8_Clock ASCII is a 7-bit charset. ["Extended ASCII" or "ANSI" is a misnomer](https://stackoverflow.com/q/701882/995714). The result completely depends on the charset and you won't get ¶ if the charset is set differently – phuclv Apr 05 '21 at 12:12
  • Ah yes, my bad. It also depends on codepage of the terminal so it's best to learn Unicode. Most generic and safe way to deal with non-ascii chars. – Ultim8_Clock Apr 05 '21 at 12:16

1 Answers1

1

If you are using windows, try typing chcp 65001 in cmd before you execute the program.
This will set terminal's codepage to Unicode, allowing programs to print unicode chars.
For example, if your .exe file is in C:\Users\You\Desktop\test.exe:

> cd C:\Users\You\Desktop
> chcp 65001
> test.exe

This will also allow you to print other Unicode chars such as ⑩.

Ultim8_Clock
  • 304
  • 2
  • 10
  • Thanks, this works, there is any way to implement this in the code? – Iker Morales Apr 05 '21 at 11:53
  • Add `system("chcp 65001" >null")` to your code. `system()` function executes given string in cmd, and `>null` suppresses output produced by chcp command. (Active code page: 65001) Beware that this code will now produce error on other OS, since chcp is windows cmd command. – Ultim8_Clock Apr 05 '21 at 12:03
  • Hey hey I made a mistake! It's `nul` not `null`. `nul` discards all output, but using `null` will create new file named 'null' and write all input into it – Ultim8_Clock Apr 05 '21 at 12:18
  • 2
    https://docs.microsoft.com/en-us/windows/console/setconsoleoutputcp is the right way to change the code page in a program. – Retired Ninja Apr 05 '21 at 14:16