Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
When printing type size_t in printk, I use %lu, but a warning message is delivered to me. See below:
warning: format '%lu' expects type 'long unsigned int', but argument 2 has type 'size_t'
Though it is no harm, but what if I want to avoid this message?
Shawn
You can use explicit type casting by ante posing the desired type in between parentheses. If "arg" is of type "size_t", then "(int) arg" is of type "int". You get the idea
EDIT. Of course, how accurate the conversion is will depend on the involved types.
Since a size_t is actually an UNSIGNED INT and not an UNSIGNED LONG INT, you should not be using %lu to start with, and since there could be no reason to so, don't claim there is. The best way to avoid your warning is to just use %d. Type casting will get around it, but consider: why do want to use %ul when your variable is not an unsigned long? You could complain that you wanted to use %s as well...or even non existant types like %suv or %lol...
ps. you will have a lot more fun at cprogramming.com with these kinds of questions.
Type casting is an idea. I will use that. But I still don't get why there is no unsigned int output conversion in printf(k).
Frankly speaking, use %d instead of an unsigned integer format is bad. The reason is obvious.
I think he means you should probably be using "%u" (for unsigned int).
It's true that, at least for the C99 standard, size_t is equivalent to unsigned int, and not to unsigned long int, long int or anything else. So, just using the correct format specifier should sort it out without a need for type castings.
Well, the thread is dormant without answer so thought of closing it for anyone who stumbles on this warning. size_t is platform dependent and it's size depends on whether the code is running on 32-bit or 64-bit hardware. For this warning to go away use %zd or %zu as a format specifier, though it's definitely unsigned.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.