Thread: I give up - TCHAR, char - initializing/printing

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    44

    I give up - TCHAR, char - initializing/printing

    Google simply is not definitive for figuring out any of this C syntax.

    Can someone provide the syntax for every variation of initializing TCHAR or char single-byte (non-array) and array strings?

    Initialize to a null string.
    Initialize to a valid string w/ len of zero.
    Initialize to a hex value(s).
    Initialize to decimal byte values.

    For the TCHAR/char statement itself as well as later, in the BODY of the program (e.g. on-the-fly). I'm looking for simple assignment statements (not for loops).

    It seems the compiler treats a NON-array char/tchar differently from one with a [] or [someintegernumber] at definition time.

    Also, I've gotten confused. If have gotten rid of all the char in my program and replaced with TCHAR. Everything works.

    I am using _tprintf.

    Should I use:

    _tprintf(L"string");

    or

    _tprintf(_T"string"); - what header is this _T _TEXT thing in anyway?


    While we're here:

    #define FRUNREC 0x01 // recurse all directories
    #define FRUNDIR 0x02 // return directories
    #define FRUNFILE 0x04 // return files


    int Frun (TCHAR* Fruni, TCHAR Fruniopb, void (*FrunCB)(TCHAR* FrunCBt)) {


    Invoking this function has a peculiarity:


    char
    Frunopb = FRUNDIR | FRUNREC;
    TCHAR Frunopbt = { FRUNDIR | FRUNREC };
    TCHAR TCHdir[300];

    z = Frun( TCHdir, Frunopbt, print1 );

    The above has no & before TCHdir and Frunopbt yet one is passed as TCHAR* and the other as TCHAR. Why the difference? My hunch is it has something to do with the TCHAR Frunopbt not being an "array"?

    Last edited by jlewand; 02-07-2012 at 08:37 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why use TCHAR? Just use the standard/accurate name, char. And unless you absolutely have to, use standard portable functions like printf().

    Once you've gotten rid of the practically useless and annoying MS typedefs, you can now google with many more results.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    Over on the Windows forum, it was recommended to make one's code UNI/ASCII-neutral. Hence, TCHAR for everything.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by jlewand View Post
    Over on the Windows forum, it was recommended to make one's code UNI/ASCII-neutral. Hence, TCHAR for everything.
    That sounds fine, except for the fact that

    a) You aren't sure of what your machine is using, memory-wise. If you consciously make the decision to use a char, or a larger unicode type, you can more carefully monitor the memory usage of your program.
    b) You can actually display unicode characters in an "ASCII" environment (one-byte type), they take the form of multiple escape codes in a row to form a single "special" character.
    c) TCHAR is not only non-standard, but it hauls along more non-standard functions that go with it. Printf can display anything ASCII, and does surprisingly well with special characters represented with a cheap ASCII trick (b).

    Could you show me where this was recommended? I'd like to see why they thought using this was a good solution.. (I mean, maybe I'm wrong, maybe not)

  5. #5

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    I saw you post that before, but I thought the concensus was use TCHAR. Jeez. I just changed everything (but it did go quite smoothly).

    Could someone at least address the int function call peculiarity between 2 TCHAR types whereby one is TCHAR* and other TCHAR but no & needed on function call?

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    My hunch is it has something to do with the TCHAR Frunopbt not being an "array"?
    That's exactly right. The name of an array is essentially a pointer.

    For questions on this forum, you're best to get rid of the whole TCHAR thing. You're still learning char, char*, char[] etc. No need to complicate things. You can learn TCHAR (a Microsoft specific thing) later.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    I already understand TCHAR. I just don't know how to initialize TCHAR strings. I have to keep trying every possible combination of syntax.
    Last edited by jlewand; 02-08-2012 at 07:18 AM.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I believe it's something like:
    Code:
    TCHAR szOne[64]; // array of 64 TCHAR
    _sntprintf(szOne, sizeof(szOne), _T("First %s"), _T("string"));
    LPTSTR pszTwo = malloc(64 * sizeof(*pszTwo)); // Pointer to TCHAR
    _tcscpy(pszTwo, _T("Second string"));
    LPCTSTR pcszThree = _T("Third string"); // const pointer to TCHAR

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I saw you post that before, but I thought the concensus was use TCHAR.
    Perhaps that isn't the best thread to link to, since the other person doesn't know how to carry on an argument/debate. The important information is here:
    CodeGuru Forums - View Single Post - To _T() or not to _T()?
    CodeGuru Forums - View Single Post - To _T() or not to _T()?

    >> _sntprintf(szOne, sizeof(szOne) ...
    Second parameter is "element count", not byte count. So "sizeof(szOne) / sizeof(*szOne)".

    gg

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    the reason to use TCHAR boils down to being able to compile using ASCII or Unicode in a windows program. makes sense if you only target Windows AND you need both versions.
    a reason not to use TCHAR is that porting your program to a non windows platform will be more difficult.
    take your pick based on what you need to do.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    >> _sntprintf(szOne, sizeof(szOne) ...
    Second parameter is "element count", not byte count. So "sizeof(szOne) / sizeof(*szOne)".
    Oops, sorry...should've known better then to go off the top of my head.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I read Codeplug's post on the other site and yes it's all true.
    However, I still say go ahead and use TCHAR. You'll personally get more out of having gone there and seen how it all works for yourself, and you'll be in a much better position to understand others' programs.
    You just put _T() around every string literal or char literal.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    iMalc, I'm inclined to use TCHAR. I have, mistakenly, put L" in every _tprintf statement. I will change to _T. My conversion from char to TCHAR went quite quickly and with no hitches.

    Can someone help me with my original post/request. I had particular troubles trying to assign a hex value to a TCHAR. Took me about 15 minutes of playing around. I don't have my source handy to say what I finally did to get the 0xnnn assignment working.

    Code:
    TCHAR test = { } syntax
    TCHAR test[] = { } syntax
    TCHAR test = _T"this is easy to understand"; (add szero on the end)
    TCHAR test = { 10 }; will that put a \n double-wide character in test?
    I have trouble with why you can't seem to say things like:
    Code:
    test[0] = anothertchar[0];
    It seems you have to _tcscpy one to the other.
    Last edited by jlewand; 02-08-2012 at 12:57 PM.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It works just fine.
    Code:
    #include "stdafx.h"
    
    int _tmain(int argc, TCHAR* argv[]) {
        _tprintf(_UNICODE ? _T("UNICODE mode\n") : _T("ASCII mode\n"));
        TCHAR str1[10] = _T("hello");
        TCHAR str2[10] = _T("goodbye");
        str1[0] = str2[0];
        _tprintf(_T("str1 is now [%s]\n"), str1);  // This prints: str1 is now [gello]
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with initializing a char array
    By csepraveenkumar in forum C Programming
    Replies: 10
    Last Post: 03-22-2011, 01:25 AM
  2. TCHAR * to unsinged char []
    By simly01 in forum C Programming
    Replies: 5
    Last Post: 11-14-2004, 03:05 PM
  3. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 05:19 PM
  4. tchar or just char
    By stallion in forum Windows Programming
    Replies: 5
    Last Post: 01-23-2003, 01:44 PM
  5. initializing char arrays to null
    By swiss powder in forum C++ Programming
    Replies: 6
    Last Post: 02-28-2002, 02:56 PM