Thread: StringCchLength & StringCbLength wrong output

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    StringCchLength & StringCbLength wrong output

    Hey guys,

    I find it hard to understand what exactly with the below code.
    even copy pasted from the book which im using to learn, same wrong output.

    Code:
    #include <windows.h>
    #include <strsafe.h>
    #include <wchar.h>
    
    int wmain(void) { 
    
        wchar_t str[] = L"ZetCode";
        size_t target_size = 0;
    
        size_t size = sizeof(str);
    
        HRESULT r = StringCbLengthW(str, size, &target_size);
        
        if (SUCCEEDED(r)) {
    
            wprintf(L"The string has %lld bytes\n", target_size);
        } else {
    
            wprintf(L"StringCbLengthW() failed\n"); 
            return 1;
        }
    
        size = sizeof(str)/sizeof(wchar_t);
    
        r = StringCchLengthW(str, size, &target_size);
        
        if (SUCCEEDED(r)) {
    
            wprintf(L"The string has %lld characters\n", target_size);
        } else {
    
            wprintf(L"StringCchLengthW() failed\n"); 
            return 1;
        }
    
        return 0;
    }
    my output when i run is

    The string has 28429359194832910 bytes
    The string has 28429359194832903 characters
    Press any key to continue...
    whereas the output according to book and what i read in the MSDN documentation should be

    The string has 14 bytes
    The string has 7 characters
    Note that i even copy pasted the code from book,, still got a wrong output. where did i go wrong?

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    You need to pay attenstion to the compiler warnings:
    main.cpp(16): warning C4477: 'wprintf' : format string '%lld' requires an argument of type '__int64', but variadic argument 1 has type 'std::size_t'
    main.cpp(16): note: consider using '%zd' in the format string
    main.cpp(30): warning C4477: 'wprintf' : format string '%lld' requires an argument of type '__int64', but variadic argument 1 has type 'std::size_t'
    main.cpp(30): note: consider using '%zd' in the format string

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    size_t is an unsigned type. It requires a "%zu" as the format specifier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting wrong output
    By mistu4u in forum C Programming
    Replies: 10
    Last Post: 06-24-2011, 12:01 AM
  2. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  3. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 01:58 PM
  4. Wrong output.
    By doampofo in forum C Programming
    Replies: 2
    Last Post: 03-13-2003, 12:23 PM
  5. Help! I am getting wrong output
    By mastamoo in forum C Programming
    Replies: 0
    Last Post: 10-08-2001, 10:12 AM

Tags for this Thread