tags:

views:

146

answers:

4

What is the character encoding expected in libc? For example, gethostname(char *name, size_t namelen); takes char* as argument. Is it expected that the name parameter be encoded in utf8(which keeps the ascii intact) or plain ascii or some other format?

Also does C mandates any character encoding scheme?

+2  A: 

All string functions (except widechar ones) support only native charset, e.g. ASCII on Unix/Linux/Windows or EBCDIC on IBM mainframe/midrange computers.

qrdl
How do use these functions in non english environment?
chappar
Also, i think libc hasn't got wchar_t* equivalent of all char * functions.
chappar
You have to convert yourself or get some lib to do the job - see more here: http://stackoverflow.com/questions/313555/light-c-unicode-library.Anyway you cannot name you host in UTF-8, can you?
qrdl
+2  A: 
  • char uses ASCII
  • wchar_t is the standard C datatype for unicode

use and in order to deal with the wide characters.

dfa
what is encoding used for wchar_t? Is it UCS2 or utf16?
chappar
@chappar: it's implementation-defined.
Bastien Léonard
Implementation-defined? I thought it was application-defined? (Two apps compiled with the same compiler on the same OS has the right to use different encodings, which is consistent with the "low-levelness" of C.)
bortzmeyer
+1  A: 

char should be a 7-bit compatible ASCII encoding (I can't find any definite reference on this though). The definition of wchar_t is left to the implementation, but the C standard requires that the characters from the C portable character set be the same. If I understand this correctly, then

char a = 'a';
wchar_t aw = L'a';
if (a == (char)aw) {
    // should be true
}

The standard does not say anything about UTF-8.

JesperE
ASCII isn't mandatory, the implementation may use EBCDIC too.
Bastien Léonard
A: 

You will probably have to use a third-party library, such as GLib. This lib is portable and very useful, it also provides regular expressions, data structures and more.

Bastien Léonard
Why the downvote?
Bastien Léonard
Probably because the answer was not helpful. How would GLib help in this case?
JesperE
I don't know why someone has downvoted this.
chappar