Thread: Greek Characters in C

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Post Greek Characters in C

    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?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define OMEGA "\u03a9"
    
    int main(void) {
    	printf("Ω = %s\n", OMEGA);
    	return 0;
    }
    Output:

    Ω == Ω

    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

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually... North American extended ASCII has the omega symbol at char 234.


    You should be able to print it as ...
    Code:
    int value =100;
    
    printf("%c = %d",234, value);
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    You should be able to print it as ...
    Code:
    int value =100;
    
    printf("%c = %d",234, value);
    $ ./a.out
    � = 3
    Maybe the terminal does not support the extended ascii ?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Maybe the terminal does not support the extended ascii ?
    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;                                            
    }
    Greek Characters in C-omega-png

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    I'm more interested also in how your compiler turned 100 into 3...
    Sorry..misquoted...I put a single statement, replacing 'value' with 3.

    Codepages ? ... I thought that ASCII is replaced with UFT-8 by the OS when appropriate. (?)

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Sorry..misquoted...I put a single statement, replacing 'value' with 3.

    Codepages ? ... I thought that ASCII is replaced with UFT-8 by the OS when appropriate. (?)
    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.

  9. #9
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. characters
    By Mini in forum C Programming
    Replies: 2
    Last Post: 08-10-2010, 08:27 AM
  2. EOL characters
    By exareth in forum C Programming
    Replies: 2
    Last Post: 03-18-2009, 02:50 PM
  3. Characters
    By Lucid15 in forum C Programming
    Replies: 35
    Last Post: 02-10-2009, 12:20 AM
  4. Characters like á õ ü in DOS
    By Tropicalia in forum C++ Programming
    Replies: 17
    Last Post: 09-29-2006, 06:30 AM
  5. DOS Characters
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2001, 02:08 PM