1

I just started to learn C and then want to proceed to learn C++. I am currently using a textbook and just write the examples in order to get a bit more familiar with the programming language and procedure.

Since the example that is given in the book didn't work, I tried to find other similar codes. The problem is that after compiling the code, the program itself does not show and of the symbols represented by %c. I get symbols for the numbers 33-126 but everything else is either nothing at all or just a white block...

Also, on some previous example I wanted to write °C for temperature and it couldn't display the symbol °

The example I found on the web that does not display the %c symbols is

#include <stdio.h>
#include <ctype.h>

int main()
{
    int i;
    i=0;
    do
    {
        printf("%i %c \n",i,i);
        i++;
    }
    while(i<=255);
}

Is anyone familiar with this? Why can I not get an output for %c or e.g. ° as well???

phuclv
  • 27,258
  • 11
  • 104
  • 360
  • to better undestanding what is ascii Code see http://www.theasciicode.com.ar/extended-ascii-code/non-breaking-space-no-break-space-ascii-code-255.html – EsmaeelE May 29 '17 at 15:13

2 Answers2

2

ASCII is a 7-bit character set, which means it consists of only codepoints in the range [0, 127]. For 8-bit code pages there are still 128 available codepoints with values from 128 to 255 (i.e. the high bit is set). These are sometimes called extended ASCII (although they're not related to ASCII at all) and the characters that they map to depend on the character set. An 8-bit charset is sometimes also called ANSI although it's actually a misnomer

US English Windows uses Windows-1252 code page by default, with the character ° at codepoint 0xB0. Other OSes/languages may use different character sets which have different codepoint for ° or possibly no ° symbol at all.

You have many solutions to this:

  • If your PC uses an 8-bit charset
    • Lookup the value of ° in the charset your computer is using and print it normally. For example if you're using CP437 then printf("\xF8") will work because ° is at the code point 0xF8. printf("°") also works if you save the source file in the same code page (CP437)
    • Or just change charset to Windows-1252/ISO 8859-1 and print '°' or '\xB0'. This can be done programmatically (using SetConsoleOutputCP on Windows and similar APIs on other OSes) or manually (by some console settings, or by running chcp 1252 in Windows cmd). The source code file still needs to be saved in the same code page
  • Print Unicode. This is the recommended way to do
    • Linux/Unix and most other modern OSes use UTF-8, so just output the correct UTF-8 string and you don't need to care about anything. However because ° is a multibyte sequence in UTF-8, you must print it as a string. That means you need to use %s instead of %c. A single char can't represent ° in UTF-8. Newer Windows 10 also supports UTF-8 as a locale so you can print the UTF-8 string directly
    • On older Windows you need to print the string out as UTF-16. It's a little bit tricky but not impossible

If you use "\u00B0" and it prints out successfully then it means your terminal is already using UTF-8. \u is the escape sequence for arbitrary Unicode code points

See also

phuclv
  • 27,258
  • 11
  • 104
  • 360
0

Anything outside the range 33-126 isn't a visible ASCII character. 0-32 is stuff like backspace (8), "device control 2" (18), and space (32). 127 is DEL, and anything past that isn't even ASCII; who knows how your terminal will handle that.

user2357112 supports Monica
  • 215,440
  • 22
  • 321
  • 400
  • Thank you very much for replying so fast! :) So I cannot use the numbers 1-32 and everything above 126 with "%c" at all? May I ask why then in the textbook as well as in the example above they use 255 as upper limit? And do you maybe also know what happens to the " ° " for the Celsius symbol? Because for that I also only receive a white block... – user3526475 Apr 12 '14 at 10:28
  • @user3526475: You may be misunderstanding what the `%c` format specifier does. Why are you passing arbitrary numeric values for that slot? And what Celcius symbol? Does a degree symbol appear in your output somewhere? There isn't one in your code. – user2357112 supports Monica Apr 12 '14 at 10:32
  • C `char`s and strings don't support Unicode values, and most of the standard functions you'll learn about don't do anything with Unicode. There are ways to do Unicode in C, but if you're still learning the basics, stick with ASCII. – user2357112 supports Monica Apr 12 '14 at 10:41
  • The example I have from the textbook is a small piece of code that allows me to choose among the numeral systems octal, hexal and also Ascii and then display a given/random number in the respective system or Ascii. I am just asking because I would like to understand why it doesn't work. The Celsius example converts Kelvin to Celsius where I have sth like printf("\n%.2f °C are %.2f K",temperature,temperature+273.15); Everything works fine except for " ° " I get only this white block. – user3526475 Apr 12 '14 at 10:42
  • @user3526475: It's entirely possible the textbook's example actually works and you misunderstood or mistyped it somehow. As for the Celcius thing, ° isn't an ASCII character. Stick with characters you can see on your keyboard (or if you're not a native English speaker, stick with characters you can see in this picture: http://www.asciitable.com/index/asciifull.gif ). – user2357112 supports Monica Apr 12 '14 at 10:47
  • look up the degree character on your charset – phuclv Apr 13 '14 at 04:16
  • I checked the code an I am sure that I didn't mistype it. I also used a simple example with printf only and it doesn't work as well. The above example also doesn't work. Yes, I am not a native English speaker and I use a German keyboard. The ° is on my keyboard... How could I print the list that you linked in your comment? How can I find out about the charset? By now it seems as if I couldn't use the %c command at all, since I don't get any output... – user3526475 Apr 13 '14 at 12:24
  • @user3526475: I don't know much about dealing with terminal charsets. The simplest way to get the list of ASCII characters is to just Google something like "ASCII table"; the information is probably available on your computer somewhere, but I don't know how you'd get at it in C. You should probably avoid the `%c` format specifier for now; it doesn't really make sense to use it the way your example does. When you need it, it should be clearer what to do. – user2357112 supports Monica Apr 13 '14 at 12:29
  • I used \u00B0 now and it works for °. Still I don't really understand why it cannot print °. The textbook is German as well, so I assume that it should work... However, as I understood only right now, the textbook is more than 10 years old, so there might be some things that have changed. Thank you very much! – user3526475 Apr 13 '14 at 12:37