C: Display special characters with printf() - c

I wanted to know how to display special characters with printf().
I'm doing a string conversion program from Text to Code128 (barcode encoding).
For this type of encoding I need to display characters such as Î, Ç, È, Ì.
Example:
string to convert: EPE196000100000002260500004N
expected result: ÌEPEÇ3\ *R 6\ R $ÈNZÎ
printf result typed: ╠EPEÇ3\ *R 6\ R $ÇNZ╬
printf result image: []
EDIT: I only can use C in this program no C++ at all. All the awnsers I've find so far are in C++ not C so I'm asking how to do it with C ^^

I've find it,
#include <locale.h>
int main()
{
setlocale(LC_ALL,"");
printf("%c%c%c%c\n", 'Î', 'Ç', ' È','Ì');
}
Thank you all for your awnsers it helps me a lot!!! :)

If your console is in UTF-8 it is possible just to print UTF-8 hex representation for your symbols. See similar answer for C++ Special Characters on Console
The following line prints heart:
printf("%c%c%c\n", '\xE2', '\x99', '\xA5');
However, since you print '\xCC', '\xC8', '\xCE','\xC7' and you have 4 different symbols it means that the console encoding is some kind of ASCII extension. Probably you have such encoding http://asciiset.com/. In that case you need characters '\x8c', 'x8d'. Unfortunately there are no capital version of those symbols in that encoding. So, you need some other encoding for your console, for example Latin-1, ISO/IEC 8859-1.
For Windows console:
UINT oldcp = GetConsoleOutputCP(); // save current console encoding
SetConsoleOutputCP(1252);
// print in cp1252 (Latin 1) encoding: each byte => one symbol
printf("%c%c%c%c\n", '\xCC', '\xC8', '\xCE','\xC7');
SetConsoleOutputCP(CP_UTF8);
// 3 hex bytes in UTF-8 => one 'heart' symbol
printf("%c%c%c\n", '\xE2', '\x99', '\xA5');
SetConsoleOutputCP(oldcp);
The console font should support Unicode (for example 'Lucida Console'). It can be changed manually in the console properties, since the default font may be 'Raster Fonts'.

Related

Use the letter ñ in C

I have to save in a char[] the letter ñ and I'm not being able to do it. I tried doing this:
char example[1];
example[0] = 'ñ';
When compiling I get this:
$ gcc example.c
error: character too large for enclosing
character literal type
example[0] = 'ñ';
Does anyone know how to do this?
If you're using High Sierra, you are presumably using a Mac running macOS 10.13.3 (High Sierra), the same as me.
This comes down to code sets and locales — and can get tricky. Mac terminals use UTF-8 by default and ñ is Unicode character U+00F1, which requires two bytes, 0xC3 and 0xB1, to represent it in UTF-8. And the compiler is letting you know that one byte isn't big enough to hold two bytes of data. (In the single-byte code sets such as ISO 8859-1 or 8859-15, ñ has character code 0xF1 — 0xF1 and U+00F1 are similar, and this is not a coincidence; Unicode code points U+0000 to U+00FF are the same as in ISO 8859-1. ISO 8859-15 is a more modern variant of 8859-1, with the Euro symbol € and 7 other variations from 8859-1.)
Another option is to change the character set that your terminal works with; you need to adapt your code to suit the code set that the terminal uses.
You can work around this by using wchar_t:
#include <wchar.h>
void function(void);
void function(void)
{
wchar_t example[1];
example[0] = L'ñ';
putwchar(example[0]);
putwchar(L'\n');
}
#include <locale.h>
int main(void)
{
setlocale(LC_ALL, "");
function();
return 0;
}
This compiles; if you omit the call to setlocale(LC_ALL, "");, it doesn't work as I want (it generates just octal byte \361 (aka 0xF1) and a newline, which generates a ? on the terminal), whereas with setlocale(), it generates two bytes (\303\261 in octal, aka 0xC3 and 0xB1) and you see ñ on the console output.
You can use "extended ascii". This chart shows that 'ñ' can be represented in extended ascii as 164.
example[0] = (char)164;
You can print this character just like any other character
putchar(example[0]);
As noted in the comments above, this will depend on your environment. It might work on your machine but not another one.
The better answer is to use unicode, for example:
wchar_t example = '\u00F1';
This really depends on which character set / locale you will be using. If you want to hardcode this as a latin1 character, this example program does that:
#include <cstdio>
int main() {
char example[2] = {'\xF1'};
printf("%s", example);
return 0;
}
This, however, results in this output on my system that uses UTF-8:
$ ./a.out
�
So if you want to use non-ascii strings, I'd recommend not representing them as char arrays directly. If you really need to use char directly, the UTF-8 sequence for ñ is two chars wide, and can be written as such (again with a terminating '\0' for good measure):
char s[3] = {"\xC3\xB1"};

Outputting √ symbol in C

Everytime I copy a (√) over from a word document my compiler (DEV C++) turns it into a v
Anyone know how to display a square root symbol in c?
It's for aesthetic purposes (I'm trying to display the quadratic formula and ()^1/2) isn't going to work.
Using windows 7
It is extended ASCII code. Please refer to Ascii & extended ascii chart
http://www.asciitable.com/index/asciifull.gif
http://www.asciitable.com/index/extend.gif
and you can try something like:
char chr = 251; //stores the extended ASCII of a symbol
printf("Character with an ascii code of 251: %c \n", chr);
//tries to print an ASCII symbol...
Your editor must support unicode source files, your source file must be in unicode because √ is not an ASCII symbol. And your output files or streams must be in the same unicode encoding to be able to display that symbol correctly.
On Linux you should be able to achieve that using UTF-8 encoded source files.
Try the following code:
#include <stdio.h>
int main()
{
printf("\xFB");
return 0;
}

How to use symbols of extended ASCII table in C?

I've been tried to print Extended ASCII characters:
http://www.theasciicode.com.ar/
But all those symbols were printed as question-character on the white background ?.
I use the following cycle to print that symbols:
for (i = 0; i <= 30; i++)
printf("%c", 201);
Question: Is there any way to print those Extended ASCII characters or not? Or maybe there is special library for these characters?
OS Linux Ubuntu 13.04, Code::Blocks 12.11 IDE.
It's better to use unicode than extended ASCII, which is non-standard. A thread about printing unicode characters in C :
printing-utf-8-strings-with-printf-wide-vs-multibyte-string-literals
But indeed you need to copy paste unicode characters..
A better way to start:
#include <stdio.h>
int main() {
printf("\u2500\u2501\n");
}
See https://en.wikipedia.org/wiki/Box-drawing_character#Unicode for unicode characters for this extended ASCII style box art..

printf UTF8 characters with printf from Hexadecimal ints

Kind of trivial thing but ...
I want to print japanese characters using plain C from Hexadecimals
From this table, I know that, the first char in the table, あ's Entity is &# 12353 and its Hex Entity is x3041, etc.
But how do I use this two numbers in order to get printed all characters in the command line?
If your terminal is set to UTF-8 and locale is set correctly, you may write:
char s[]="あ";
you can also try
char s[]={0xe3,0x81,0x82,0x0}
(the last is the Unicode UTF-8 encoding for "あ"), and then just printf("%s",s);
If __STDC_ISO_10646__ is defined, wchar_t is in Unicode, and you can do something like:
printf("%lc", (wchar_t)0x3041);

Handling special characters in C (UTF-8 encoding)

I'm writing a small application in C that reads a simple text file and then outputs the lines one by one. The problem is that the text file contains special characters like Æ, Ø and Å among others. When I run the program in terminal the output for those characters are represented with a "?".
Is there an easy fix?
First things first:
Read in the buffer
Use libiconv or similar to obtain wchar_t type from UTF-8 and use the wide character handling functions such as wprintf()
Use the wide character functions in C! Most file/output handling functions have a wide-character variant
Ensure that your terminal can handle UTF-8 output. Having the correct locale setup and manipulating the locale data can automate alot of the file opening and conversion for you ... depending on what you are doing.
Remember that the width of a code-point or character in UTF-8 is variable. This means you can't just seek to a byte and begin reading like with ASCII ... because you might land in the middle of a code point. Good libraries can do this in some cases.
Here is some code (not mine) that demonstrates some usage of UTF-8 file reading and wide character handling in C.
#include <stdio.h>
#include <wchar.h>
int main()
{
FILE *f = fopen("data.txt", "r, ccs=UTF-8");
if (!f)
return 1;
for (wint_t c; (c = fgetwc(f)) != WEOF;)
printf("%04X\n", c);
fclose(f);
return 0;
}
Links
libiconv
Locale data in C/GNU libc
Some handy info
Another good Unicode/UTF-8 in C resource
Make sure you're not accidentally dropping any bytes; some UTF-8 characters are more than one byte in length (that's sort of the point), and you need to keep them all.
It can be useful to print the contents of the buffer as hex, so you can inspect which bytes are actually read:
static void print_buffer(const char *buffer, size_t length)
{
size_t i;
for(i = 0; i < length; i++)
printf("%02x ", (unsigned int) buffer[i]);
putchar('\n');
}
You can do this after loading a very short file, containing just a few characters.
Also make sure the terminal is set to the proper encoding, so it interprets your characters as UTF-8.
Probably your text file is ISO-8559-1 encoded but your terminal is UTF-8. This kind of mismatch is a standard problem when dealing with byte-oriented text handling; other C programs (such as the standard ‘cat’ and ‘more’ commands) will do the same thing and it isn't generally considered an error or something that needs to be fixed.
If you want to operate on a Unicode character level instead of bytes that's fine, but you'll need to use wchar as your character type instead of char throughout your program, and provide switches for the user to specify what the incoming file encoding actually is. (Whilst it is sometimes possible to guess, it's not very reliable.)
I don't know if it could help but if you're sure that the encodings of terminal and input file are the same, you can try to setlocale():
#include <locale.h>
…
setlocale(LC_CTYPE, "");

Resources