Simple unicode example not working

This is a discussion on Simple unicode example not working within the C Programming forums, part of the General Programming Boards category; If i try to execute this code: Code: #define _UNICODE #include <tchar.h> #include <stdlib.h> int main() { TCHAR c[]=_TEXT("something"); system("PAUSE"); ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    11

    Simple unicode example not working

    If i try to execute this code:
    Code:
    #define _UNICODE
    #include <tchar.h>
    #include <stdlib.h>
    
    
    int main() {
    
    TCHAR c[]=_TEXT("something");
    
    system("PAUSE");
    return 0;
    
    }
    I get the following compile errors (i am using Dev-c++4):

    Code:
    In file included from c:\docume~1\deviance\desktop\x\untitl~2.c:2:
    C:\PROGRA~1\DEVC__~1\Include\tchar.h:57: parse error before `TCHAR'
    C:\PROGRA~1\DEVC__~1\Include\tchar.h:57: warning: data definition has no type or storage class
    c:\docume~1\deviance\desktop\x\untitl~2.c: In function `main':
    c:\docume~1\deviance\desktop\x\untitl~2.c:7: `TCHAR' undeclared (first use in this function)
    c:\docume~1\deviance\desktop\x\untitl~2.c:7: (Each undeclared identifier is reported only once
    c:\docume~1\deviance\desktop\x\untitl~2.c:7: for each function it appears in.)
    c:\docume~1\deviance\desktop\x\untitl~2.c:7: parse error before `c'
    What am i missing?
    If i add:
    Code:
     wprintf(L"This is working: %lc",123);
    i get further errors.
    Code:
     wchar_t mystring[] = L"flipcode";
    doesn't work either.
    Can u tell me what i'm doing wrong. And perhaps give a a working sample o code so i can get the idea. Thank you.
    Last edited by Deviance; 07-11-2006 at 10:46 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    #define UNICODE and _UNICODE.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    It still doesn't work. But i've just come across #include <wchar.h>
    in win32sdk helpfile. This code seems to work:
    Code:
    #define _UNICODE
    #include <wchar.h> 
    #include <tchar.h>
    
    int main() {
    TCHAR c[]=_TEXT("ceva");
    wchar_t mystring[] = L"something";
    
    wprintf(L"This is working: %lc",123);
    wprintf("%ls",c);
    wprintf("%ls",mystring);
    
    system("PAUSE");
    return 0;
    Actually only the first wprintf works. The other 2 don't. Why?
    Last edited by Deviance; 07-11-2006 at 11:13 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > TCHAR c[]=_TEXT("ceva");
    I thought these were declared by windows.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    Hmm I seem to have figured it:
    Code:
     wprintf(L"%ls",c);
    Code:
     wprintf(L"%ls",mystring);

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    i've noticed this:
    Code:
     wprintf(L"This is not working: ->%lc<- ",301); 
     wprintf(L"This is working: ->%lc<- ",201);
    The not working wprintf prints: "This is not working: ->", but not the 301 character as well.I guess it has to do with local unicode settings so that the compiler knows what character to print on the screen coresponding to 301. But how do i "tell him" that?

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    301 and 201 are stupid numerical constants that dont match the conversion specifier. Store them in a TCHAR.
    TCHAR letter1 = 0201;
    TCHAR letter2 = 0301;

    edit: You need to prefix octals with a zero. Sorry I didn't notice you were using octals before, but you will probably get something like

    $ ./a
    This is working
    This is not working

    because in octal the ISO-8859 character map doesn't start until 240.
    Last edited by whiteflags; 07-11-2006 at 12:25 PM.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    I doesn't work. What i'm trying to do is print "Ş" (which is 350 in windows character map) on the screen. So what must i write? (i noticed that wprintf(L"%lc",350) doesn't work.)
    Last edited by Deviance; 07-11-2006 at 12:30 PM.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    > wprintf(L"%lc",350) doesn't work.
    Of course not. I'm working under the impression that you are using base eight numbers. 350 in decimal is completely different from 350 in base eight. Stick to one number convention.

    How about
    TCHAR letter = 'Ş';
    wprintf(L"%lc is a letter\n", letter);

    instead of fumbling around the character map? Because unless you are actually making windows use the language you're writing in, number magic isn't going to work.

    Because I'm using Latin-1 for instance, 0350 on the map is and it would print that, not what you want.
    Last edited by whiteflags; 07-11-2006 at 12:38 PM.

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    I can't! I can't type nor paste "Ş" in the editor window (Dev-c++4). It pastes "S," So what now? p.s. i am not using base eight numbers. should i?
    Last edited by Deviance; 07-11-2006 at 12:45 PM.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Switch the language that the keyboard maps to temporarily to the language you're working with, and then type it. If you don't know how to do that, then you probably should just type in English.

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    I've done so. I was able to type it but the program still didn't print anything. I'm telling you there is a different problem. But i don't want to upset anybody. I'll see what i can do myself.
    Last edited by Deviance; 07-11-2006 at 01:16 PM.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    That is because you are still using Latin-1 as the character set on your system. You need to code your program to set it's locale correctly

    #include <locale.h>

    if (!setlocale(LC_ALL, "")
    fprintf(stderr, "Could not set the locale\n");

    where in the code above, the program will load the character set specified in your OS' environment variables. If you do it this way then all you would have to do is boot up your computer in a different language and run the program, then the output should work.

    If you want to specify what language you're working with (and force other people using your program to read a language they might not know), then you would have to type a specially formatted string like they teach you how to in the man page, to use the proper character set. Either way, you're pretty stuck with the locale library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifdef is harmful - but simple workaround is not working
    By amitbern in forum C Programming
    Replies: 15
    Last Post: 12-06-2008, 06:02 AM
  2. Replies: 10
    Last Post: 09-07-2008, 11:38 PM
  3. Simple program not working
    By oobootsy1 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2005, 02:20 AM
  4. Replies: 5
    Last Post: 02-02-2003, 09:56 AM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 10:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21