LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-27-2008, 10:44 PM   #1
duyuyang
Member
 
Registered: Mar 2008
Posts: 56

Rep: Reputation: 16
What is type size_t's format in printf?


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
 
Old 12-27-2008, 11:22 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by duyuyang View Post
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.

Last edited by i92guboj; 12-27-2008 at 11:30 PM.
 
Old 12-27-2008, 11:52 PM   #3
mk27
Member
 
Registered: Sep 2008
Distribution: fedora, gentoo, ubuntu
Posts: 148

Rep: Reputation: 23
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.
 
Old 12-28-2008, 02:16 AM   #4
duyuyang
Member
 
Registered: Mar 2008
Posts: 56

Original Poster
Rep: Reputation: 16
Thank you both.

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.

Shawn
 
Old 12-28-2008, 03:38 AM   #5
mk27
Member
 
Registered: Sep 2008
Distribution: fedora, gentoo, ubuntu
Posts: 148

Rep: Reputation: 23
Quote:
Originally Posted by duyuyang View Post
Frankly speaking, use %d instead of an unsigned integer format is bad. The reason is obvious.
In fact, it is not obvious because you are wrong. But go ahead, typecast.

pps. really, go for the fun I mentioned...

Last edited by mk27; 12-28-2008 at 03:40 AM.
 
Old 12-29-2008, 01:06 AM   #6
duyuyang
Member
 
Registered: Mar 2008
Posts: 56

Original Poster
Rep: Reputation: 16
explain it to me why I am so wrong?

Thanks,
Shawn
 
Old 12-29-2008, 01:45 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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.
 
Old 07-20-2011, 02:04 AM   #8
Cha11
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
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.

Last edited by Cha11; 07-20-2011 at 02:07 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Minus sign in printf format string doesn't work. zebela Linux - Newbie 4 06-13-2008 02:32 AM
Request for Information - wrt unique File Format type pscanuck Linux - General 1 02-21-2006 03:49 PM
Is there a currency format with printf() ? LUB997 Programming 1 01-03-2005 09:33 PM
partioning and format type V_LESTAT Linux - Newbie 41 01-14-2004 06:53 PM
printf new_user10 Programming 3 09-10-2003 12:12 AM


All times are GMT -5. The time now is 03:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration