Using stl's vector.size() with printf

This is a discussion on Using stl's vector.size() with printf within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I'm using this code: Code: vector<int> v; v.push_back(123); printf("size: %u\n", v.size()); And it retuns the result you would ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    Thumbs up Using stl's vector.size() with printf [solved]

    Hi guys,

    I'm using this code:
    Code:
    vector<int> v;
    v.push_back(123);
    printf("size: %u\n", v.size());
    And it retuns the result you would expect.
    But if you compile it you get this warning:
    Code:
    warning #181: argument is incompatible with corresponding format string conversion
                            printf("size: %u\n", v.size());
                                                 ^
    Is there any better solution than just writing that
    Code:
    (int) v.size()
    Can I use a different format string?
    Last edited by manuels; 08-18-2009 at 02:57 AM. Reason: marking it "solved"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    That does not make sense: v.size() returns a std::vector<int>::size_type, which is an unsigned integer type. %u should be an acceptable format specifier.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    I know. But I just saw that the error only appears if I use Intel's icc.
    With g++ it works fine.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    hmm... if it is feasible, consider using C++ I/O streams instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    yeah, would be a workaround. But my output line is quite complex so I would prefer a printf version.
    This warning annoys me since years and I want to know why it doesn't work like that.. in principle.
    Is it just an error by Intel's Compiler crew?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by manuels
    But my output line is quite complex so I would prefer a printf version.
    You could consider using Boost.Format.

    Quote Originally Posted by manuels
    Is it just an error by Intel's Compiler crew?
    You could ask them, but they may be right: after all %u is meant to work with an unsigned int, but std::vector<int>::size_type is only specified as an unsigned integer type.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,255
    I guess you could see if the compiler supports:
    Code:
    printf("size: %zu\n", v.size());
    But I don't think most C++ compilers support the %z modifier, and I'm not sure if vector<int>::size_type can be treated the same as a size_t.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by laserlight View Post
    That does not make sense: v.size() returns a std::vector<int>::size_type, which is an unsigned integer type. %u should be an acceptable format specifier.
    unsigned integ[ral|er] type doesn't mean unsigned int type. so %u might be the wrong format specification.

    edit: hmm you said the correct way... not really sure what you think now

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by robwhit
    unsigned integ[ral|er] type doesn't mean unsigned int type. so %u might be the wrong format specification.
    Read post #6.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What if you try %ul instead? Maybe Intel thinks it's an unsigned long.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    Quote Originally Posted by bithub View Post
    I guess you could see if the compiler supports:
    Code:
    printf("size: %zu\n", v.size());
    But I don't think most C++ compilers support the %z modifier, and I'm not sure if vector<int>::size_type can be treated the same as a size_t.
    Cool, %zu seems to work with Intel and GNU compiler.

    Thank you, folks!

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Size is actually a size_t type which can be easily cast to an unsigned int to remove the warning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 11:14 AM

Tags for this Thread


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