Thread: Using stl's vector.size() with printf

  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 01:57 AM. Reason: marking it "solved"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    hmm... if it is feasible, consider using C++ I/O streams instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,268
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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, 10:14 AM

Tags for this Thread