2

I tried the following

printf ("%c", 236);   //236 is the ASCII value for infinity

But I am just getting garbage output on the screen.

printf was working correctly for ASCII values less than 128. So I tried the following

printf ("%c", 236u);  //unsigned int 236

Still I am just getting garbage only. So, what should I do to make printf display ASCII values from 128 to 255.

Hashken
  • 4,396
  • 7
  • 35
  • 51
  • 5
    236 is most certainly not the "ASCII value for infinity". ASCII is only defined up to 127. The rest are "code pages". – Daniel Kamil Kozar Apr 11 '13 at 07:21
  • Oh. I was referring this link http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm. In that case how do I print the infinity symbol? – Hashken Apr 11 '13 at 07:25
  • printf("\xe2\x88\x9e\n"); – Aki Suihkonen Apr 11 '13 at 07:27
  • on `MinGW`, I was able to get infinity symbol with `printf("Infinity is %c\n", 236);` – Ganesh Apr 11 '13 at 07:27
  • I think you are going to need to work out what code page you are using. It's no use if you are using ASCII since that doesn't contain infinity. – David Heffernan Apr 11 '13 at 07:29
  • @Ganesh : That means that the symbol ∞ is actually `236` in the code page that your OS currently uses. – Daniel Kamil Kozar Apr 11 '13 at 07:31
  • @Karthik It is possible that your console can't display that character. Check the [link](http://stackoverflow.com/questions/5781447/showing-characters-in-extended-ascii-code-ubuntu)k by DhruvPathak – Suvarna Pattayil Apr 11 '13 at 07:32
  • The output you see depends on the locale configured in your terminal. Still, ASCII is only defined upto 127. You might probably look into `UTF-8` and use `wprintf` to print instead, after setting the correct locale in your program using `setlocale`. – Tuxdude Apr 11 '13 at 07:32
  • 1
    @DanielKamilKozar .. Yes, I ran this on `Windows 7` machine with `MinGW` support. I feel that link in DhruvPathak's comment is very apt as the display of special characters is dependent on the terminal. – Ganesh Apr 11 '13 at 07:33
  • 1
    Karthik, the link you gave is outdated. The codepage shown is OEM 437 or better known as IBM-PC character set. You would need to use MS-DOS or the command line under Windows to display your character. http://en.wikipedia.org/wiki/Code_page_437 – Patrick Schlüter Apr 11 '13 at 07:33

2 Answers2

2

Like everyone else in the comments already mentioned, you would not be able to reliably print characters after 127 (and assuming it as ASCII) since ASCII is only defined upto 127. Also the output you see very much depends on the terminal settings (i.e. which locale it is configured to).

If you're fine using UTF-8 to print, you could give wprintf a try as shown below:

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main()
{
    setlocale( LC_ALL, "en_US.UTF-8" );
    wprintf (L"%lc\n", 8734);
    return 0;
}

It would produce the following output:

8734 (or 0x221E) is the equivalent of the UTF-8 UNICODE character for the symbol .

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • 1
    note that this will not work with MSVC since it does not support [`setlocale`](http://msdn.microsoft.com/en-us/library/x99tb11d(v=vs.110).aspx) with UTF8 – msam Apr 11 '13 at 08:01
  • @msam - I hardly use Windows environment for compiling and do not have one handy right now. Do you know the equivalent English US UTF-8 locale which needs to be set for MSVC ? I can update the answer with that info. :) – Tuxdude Apr 11 '13 at 08:04
  • 1
    On windows `setlocale( LC_ALL, "C" ); wprintf (L"%lc\n", 236);` (or just `printf ("%c", 236);` since the C locale is set anyway on program startup) does it for the infinity (∞) symbol - but this is , of course, specific to this case. – msam Apr 11 '13 at 08:28
0

Standard C does not have a symbol for infinite. That's for your implementation (eg. your compiler, your operating system, your terminal and your hardware) to define. Consider that C was designed with portability for systems that use non-ASCII character sets in mind (eg. EBCDIC).

autistic
  • 1
  • 3
  • 35
  • 80