I am writting a program about resistances and i want to include the greek letter "Ω" Ι even copy it from word and still doesnt work. how do I do it?
I am writting a program about resistances and i want to include the greek letter "Ω" Ι even copy it from word and still doesnt work. how do I do it?
You'll need wide characters...
wchar_t if you want it to be portable...or some OS specific Unicode type ..which will (possibly) give you a much mature interface.
If your interface uses utf-8, you can just do it the same way you did it in your post. You can also do this manually by using the unicode values:
Unicode/UTF-8-character table
The values you want are down the right hand side, and notice there's a pull down menu up top (it says "Basic Latin" by default) because there are thousands of such characters. So, eg:
Output:Code:#include <stdio.h> #include <string.h> #define OMEGA "\u03a9" int main(void) { printf("Ω = %s\n", OMEGA); return 0; }
Ω == Ω
If you examined the source code byte by byte, you'd find the literal Ω is two bytes, 0xce 0xa9. This probably reduces the portability because utf-8 in code is not standard, some compilers may puke, but unicode (the "\u03a9") is part of the C99 and C++ standard. wchar_t is C90 and more generalized, I believe.
Last edited by MK27; 12-10-2011 at 03:09 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Actually... North American extended ASCII has the omega symbol at char 234.
You should be able to print it as ...
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversionCode:int value =100; printf("%c = %d",234, value);
It will depend on what code page (location specific) you are using.
I'm more interested also in how your compiler turned 100 into 3...
Works just fine on Pelles C...
Code:#include <stdio.h> int main( void ) { int value = 100; printf("%c = %d\n\n",234, value); return 0; }
Maybe so on Linux... but certainly not on Windows.
Windows CHAR (C char) type is pure ascii
Windows WCHAR (C wchar_t) is utf16le unicode.
If you want utf8 you need to convert...
Code:// wchar to utf8 for(x = 0; x <= LastLine; x++) { ws = WideCharToMultiByte(CP_UTF8,0,Lines[x],-1,utf,MAX_PATH,NULL,NULL); // utf8 to wchar { if (MultiByteToWideChar(CP_UTF8,0,(PCHAR)Buf,Bytes,ut,Bytes * sizeof(WCHAR)) < 1)
Last edited by CommonTater; 12-10-2011 at 03:42 PM.
Be warned that some extended ASCII characters in your source (i.e. directly inside the strings you write) can make compilers and other tools choke on your code quite easily. Even Eclipse/gcc can have problems with such things and tools like ViewVC can actually stop processing your source while it's trying to display it if you use some tools.
If you have to hard-code something in extended ASCII, I would do so using octal/hex escaped notation inside the C string (or in a string constant). If you are planning to use Unicode (I've only ever played with UTF-8), make sure your libraries support it (things like printf may only ever support ASCII in pre-C99, for example, and then if you go to print using graphical routines your graphics library will also need to support it, and maybe the font you use, etc.).
Personally, I hard-code any "non-ASCII" characters in my strings using octal/hex escape codes via string constants / defines. I use the BabelMap tool to do so (the author kindly added an option so you can see the complete octal/hex escape code for any selected character, ASCII or Unicode, into the program for me). If you can't type it on a keyboard directly without having to do SHIFT-ALT-CTRL-SCROLLOCK-F7 or something, encode it with an escape sequence rather than try to paste it into the source code. It's a rule that'll help you work with everything from C source to *nix regular expressions.