'Why can't i use the € symbol with printf?

How can i put this symbol (€) on the terminal using c. I've tried:

printf("%c",0128);

0128 is the code on the ascii table but the compiller gives the error:

invalid digit 8 in octal constant

enter image description here



Solution 1:[1]

Numeric literals that begin with 0 like that, are interpreted as octal (base 8) and therefore comprised solely of digits from 0-7. So 08 is illegal, and so is 0128.

Windows-1252 (CP-1252) encoding of the euro sign is decimal 128.

What you have posted an image of is the Alt code which is a way of entering the character on Windows by typing: Alt0128 on the numeric key pad.

You may be able to fix your problem by removing the leading zero (to produce the decimal literal 128), provided you are using CP-1252.

Solution 2:[2]

You're confusing a few things here.

First, the table of which you posted a fragment does not show character codes. It apparently documents how to enter characters on Windows (I covered this in a reply to another question here: C# Mapping ALT+<XXX> codes to characters). The leading zero here means that the character code you're entering refers to the so-called ANSI codepage instead of OEM one.

Note that the character you get by typing Alt+0128 may vary from computer to computer.

Also note that 128 is not an ASCII code. ASCII only covers character codes 0 to 127.

Second, leading zero in an integer literal in C and its successors has a different meaning. It means that the number is written in the octal numeral system. Octal notation was more popular than hexadecimal back in 70s when C was drafted. By definition, 8 is not a valid octal digit, so 0128 is a lexical error.

These two meanings are unrelated. So, if you want to print a character that is entered by the key combination Alt+0128, you need to do that otherwise.

Also note that in many cases you cannot have expressed as one char unit. If your console is configured to UTF-8, then the Unicode codepoint for it, 0x20AC, is represented by three bytes: 0xe2, 0x82, 0xac (or 226, 130, 172).

Solution 3:[3]

The best way to print stuff like that is... don’t use hex/oct/whatever constants, use a string.

Use your text editor’s ability to save source code as UTF-8 and do that directly:

puts( "Dinner is €12.50." );

Assuming:

  • Compiler takes UTF-8 source input (modern compilers do, older compilers might require a flag)
  • The standard library handles UTF-8 (it better these days)
  • A terminal that can display UTF-8

That last one is the sticking point. Most modern systems work over UTF-8 display, but the old Windows Console is still in use in a lot of places, and that requires a little extra help. Make sure to have it working in UTF-8 output mode (chcp 65001 on the command-line or the equivalent in code using SetConsoleOutputCP).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wyck
Solution 2 ach
Solution 3 Dúthomhas