Thread: swprintf misbehaving (?!?!)

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    82

    swprintf misbehaving (?!?!)

    This is probably something petty or I don't understand how swprintf is supposed to work.

    I have the following code:

    Code:
    #include <stdio.h>
    #include <wchar.h>
    ​
    int main()
    {
            int count = 22;
            wchar_t data[20];
    ​
            swprintf(data, 20, L"x%d", count);
            wprintf(L"string : %s count : %d \n", data, count);
    ​
            return 0;
    }
    I expect that printed result to be 'x22' and the count to be 4? However, I get totally different values. Anyone know where I am going wrong?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Try to use %ls instead.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Why would count magically change to 4? swprintf doesn't modify any parameters except the first. It returns the number of characters written, which would be 3 since it doesn't count the '\0'.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by john.c View Post
    Why would count magically change to 4? swprintf doesn't modify any parameters except the first. It returns the number of characters written, which would be 3 since it doesn't count the '\0'.
    Yeah, sorry.

    I thought the count would include the terminating '\0'.

    I posted some older code. It looks like this

    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <wchar.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[])
    {
            int count = 22;
            wchar_t data[20];
    
    
            count = swprintf(data, 20, L"%1s", count);
            wprintf(L"string : %s count : %d \n", data, count);
    
    
            return 0;
    }
    sprintf does exactly what I want but swprintf doesn't. ^-^

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by ghoul View Post
    sprintf does exactly what I want but swprintf doesn't. ^-^
    Did you try the suggestion in #2? Since data is a wide string, you need to use the %ls format to print it out.

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by christop View Post
    Did you try the suggestion in #2? Since data is a wide string, you need to use the %ls format to print it out.
    Quote Originally Posted by flp1969 View Post
    Try to use %ls instead.
    That didn't work but %S did work. Thanks for pointing me in the right direction :-)

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by ghoul View Post
    That didn't work but %S did work. Thanks for pointing me in the right direction :-)
    Ah, looks like another Microsoft extension:
    Format specification syntax: printf and wprintf functions
    . The standard format to print a wchar_t string is "%ls", which appears to be supported even by Microsoft. Be sure you're using the letter "l", not the digit "1" in the format. As a bonus, your code will be more portable to the rest of the world (outside of Microsoft).

  8. #8
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by christop View Post
    Ah, looks like another Microsoft extension:
    Format specification syntax: printf and wprintf functions
    . The standard format to print a wchar_t string is "%ls", which appears to be supported even by Microsoft. Be sure you're using the letter "l", not the digit "1" in the format. As a bonus, your code will be more portable to the rest of the world (outside of Microsoft).
    Thanks my friend. I was actually used '1s' and hence wasn't understanding how this could be the solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: '::swprintf' has not been declared
    By TriKri in forum Windows Programming
    Replies: 4
    Last Post: 05-07-2010, 05:27 AM
  2. Array misbehaving
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 11:01 PM
  3. string misbehaving for ifstream
    By Opel_Corsa in forum C++ Programming
    Replies: 13
    Last Post: 11-19-2006, 11:37 AM
  4. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  5. Pointers misbehaving
    By Brian in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 12:50 PM

Tags for this Thread