Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I printk <type> correctly?

138 views
Skip to first unread message

Alexey Dobriyan

unread,
Oct 23, 2008, 7:40:05 AM10/23/08
to
If variable is of Type use printk format specifier.
---------------------------------------------------------
int %d or %x
unsigned int %u or %x
long %ld ot %lx
unsigned long %lu or %lx
long long %lld or %llx
unsigned long long %llu or %llx
size_t %zu or %zx
ssize_t %zd or %zx

Raw pointer value SHOULD be printed with %p.

u64 SHOULD be printed with %llu/%llx, (unsigned long long):

printk("%llu", (unsigned long long)u64_var);

s64 SHOULD be printed with %lld/%llx, (long long):

printk("%lld", (long long)s64_var);

If type is dependent on config option (sector_t), use format specifier
of biggest type and explicitly cast to it.

Reminder: sizeof() result is of type size_t.

Thank you for your cooperation.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

David John

unread,
Oct 23, 2008, 8:00:19 AM10/23/08
to

Useful list, but I think there are more format specifiers available with
printk though.

David.

Alexey Dobriyan

unread,
Oct 23, 2008, 8:10:12 AM10/23/08
to
On Thu, Oct 23, 2008 at 05:21:47PM +0530, David John wrote:
> Useful list, but I think there are more format specifiers available with
> printk though.

This list for not generating compile-time warnings, nothing more.

Johannes Berg

unread,
Oct 23, 2008, 11:20:09 AM10/23/08
to

> u64 SHOULD be printed with %llu/%llx, (unsigned long long):
>
> printk("%llu", (unsigned long long)u64_var);
>
> s64 SHOULD be printed with %lld/%llx, (long long):
>
> printk("%lld", (long long)s64_var);

out of curiosity, why are there platforms that use an "unsigned long"
for u64? An "unsigned long long" is 64-bits too even on 64-bit
platforms, so why the difference? Are there any differences like
alignment on some platforms?

johannes

signature.asc

David Miller

unread,
Oct 23, 2008, 4:50:12 PM10/23/08
to
From: Johannes Berg <joha...@sipsolutions.net>
Date: Thu, 23 Oct 2008 17:13:26 +0200

> > u64 SHOULD be printed with %llu/%llx, (unsigned long long):
> >
> > printk("%llu", (unsigned long long)u64_var);
> >
> > s64 SHOULD be printed with %lld/%llx, (long long):
> >
> > printk("%lld", (long long)s64_var);
>
> out of curiosity, why are there platforms that use an "unsigned long"
> for u64?

Because that type is large enough.

> An "unsigned long long" is 64-bits too even on 64-bit platforms, so
> why the difference? Are there any differences like alignment on some
> platforms?

I invite you to go make that change and then try to fixup all the
resulting build warnings. Some of us already made an attempt at
some point in the past. :-/

But it's just papering over the core problem, that we can't extend
gcc's automatic printf format type checking easily.

Even if we make u64 the same type everywhere, things like sector_t
and other types which do have to vary by architecture will need
to be casted.

Johannes Berg

unread,
Oct 24, 2008, 6:00:18 AM10/24/08
to
On Thu, 2008-10-23 at 13:40 -0700, David Miller wrote:

> > An "unsigned long long" is 64-bits too even on 64-bit platforms, so
> > why the difference? Are there any differences like alignment on some
> > platforms?
>
> I invite you to go make that change and then try to fixup all the
> resulting build warnings. Some of us already made an attempt at
> some point in the past. :-/

Heh, ok. I was just curious really. I think that answers it well
enough :)

> But it's just papering over the core problem, that we can't extend
> gcc's automatic printf format type checking easily.
>
> Even if we make u64 the same type everywhere, things like sector_t
> and other types which do have to vary by architecture will need
> to be casted.

Indeed, it doesn't really help that much.

johannes

signature.asc

Randy Dunlap

unread,
Oct 28, 2008, 7:20:08 PM10/28/08
to
On Thu, 23 Oct 2008 15:41:33 +0400 Alexey Dobriyan wrote:

> If variable is of Type use printk format specifier.
> ---------------------------------------------------------
> int %d or %x
> unsigned int %u or %x
> long %ld ot %lx
> unsigned long %lu or %lx
> long long %lld or %llx
> unsigned long long %llu or %llx
> size_t %zu or %zx
> ssize_t %zd or %zx
>
> Raw pointer value SHOULD be printed with %p.
>
> u64 SHOULD be printed with %llu/%llx, (unsigned long long):
>
> printk("%llu", (unsigned long long)u64_var);
>
> s64 SHOULD be printed with %lld/%llx, (long long):
>
> printk("%lld", (long long)s64_var);
>
> If type is dependent on config option (sector_t), use format specifier
> of biggest type and explicitly cast to it.
>
> Reminder: sizeof() result is of type size_t.
>
> Thank you for your cooperation.

and attention.


Building for Alpha (cross-build) finds over 125 printk format warnings:

net/9p/client.c:820: warning: format '%llx' expects type 'long long unsigned int', but argument 4 has type 'u64'
net/9p/client.c:820: warning: format '%llx' expects type 'long long unsigned int', but argument 5 has type 'u64'
net/9p/client.c:867: warning: format '%llx' expects type 'long long unsigned int', but argument 4 has type 'u64'
net/9p/client.c:867: warning: format '%llx' expects type 'long long unsigned int', but argument 5 has type 'u64'
net/9p/client.c:932: warning: format '%llx' expects type 'long long unsigned int', but argument 5 has type 'u64'
net/9p/client.c:932: warning: format '%llx' expects type 'long long unsigned int', but argument 6 has type 'u64'
net/9p/client.c:982: warning: format '%llx' expects type 'long long unsigned int', but argument 4 has type 'u64'
net/9p/client.c:982: warning: format '%llx' expects type 'long long unsigned int', but argument 5 has type 'u64'
net/9p/client.c:1025: warning: format '%llx' expects type 'long long unsigned int', but argument 4 has type 'u64'
net/9p/client.c:1025: warning: format '%llx' expects type 'long long unsigned int', but argument 5 has type 'u64'
net/9p/client.c:1227: warning: format '%llx' expects type 'long long unsigned int', but argument 7 has type 'u64'
net/9p/client.c:1227: warning: format '%llx' expects type 'long long unsigned int', but argument 12 has type 'u64'
net/9p/client.c:1227: warning: format '%llx' expects type 'long long unsigned int', but argument 8 has type 'u64'
net/9p/client.c:1227: warning: format '%llx' expects type 'long long unsigned int', but argument 13 has type 'u64'
net/9p/client.c:1252: warning: format '%llx' expects type 'long long unsigned int', but argument 7 has type 'u64'
net/9p/client.c:1252: warning: format '%llx' expects type 'long long unsigned int', but argument 12 has type 'u64'
net/9p/client.c:1252: warning: format '%llx' expects type 'long long unsigned int', but argument 8 has type 'u64'
net/9p/client.c:1252: warning: format '%llx' expects type 'long long unsigned int', but argument 13 has type 'u64'

fs/ext4/balloc.c:615: warning: format '%lld' expects type 'long long int', but argument 2 has type 's64'
fs/ext4/inode.c:1833: warning: format '%lld' expects type 'long long int', but argument 2 has type 's64'
fs/ext4/inode.c:1835: warning: format '%lld' expects type 'long long int', but argument 2 has type 's64'

net/mac80211/rc80211_minstrel_debugfs.c:98: warning: format '%8llu' expects type 'long long unsigned int', but argument 11 has type 'u64'
net/mac80211/rc80211_minstrel_debugfs.c:98: warning: format '%8llu' expects type 'long long unsigned int', but argument 12 has type 'u64'
net/mac80211/rc80211_minstrel_debugfs.c:98: warning: format '%8llu' expects type 'long long unsigned int', but argument 11 has type 'u64'
net/mac80211/rc80211_minstrel_debugfs.c:98: warning: format '%8llu' expects type 'long long unsigned int', but argument 12 has type 'u64'

fs/ocfs2/xattr.c:2400: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:2400: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:2400: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:2424: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:2424: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:2424: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:2443: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:2443: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:2443: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:2779: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:2779: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:2779: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:2942: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:2942: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:2942: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3060: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3060: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3060: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3060: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3060: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3060: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3189: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3189: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3189: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3189: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3189: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3189: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3360: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3360: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3360: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3360: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3360: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3360: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3431: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3431: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3431: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3431: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3431: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3431: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3561: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3561: warning: format '%llu' expects type 'long long unsigned int', but argument 8 has type 'u64'
fs/ocfs2/xattr.c:3561: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3561: warning: format '%llu' expects type 'long long unsigned int', but argument 8 has type 'u64'
fs/ocfs2/xattr.c:3561: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3561: warning: format '%llu' expects type 'long long unsigned int', but argument 8 has type 'u64'
fs/ocfs2/xattr.c:3629: warning: format '%llu' expects type 'long long unsigned int', but argument 8 has type 'u64'
fs/ocfs2/xattr.c:3629: warning: format '%llu' expects type 'long long unsigned int', but argument 8 has type 'u64'
fs/ocfs2/xattr.c:3629: warning: format '%llu' expects type 'long long unsigned int', but argument 8 has type 'u64'
fs/ocfs2/xattr.c:3718: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3718: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3718: warning: format '%llu' expects type 'long long unsigned int', but argument 7 has type 'u64'
fs/ocfs2/xattr.c:3763: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3763: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'
fs/ocfs2/xattr.c:3763: warning: format '%llu' expects type 'long long unsigned int', but argument 6 has type 'u64'

fs/ubifs/journal.c:693: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/journal.c:1131: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/dir.c:163: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/tnc.c:2680: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/tnc.c:2700: warning: format '%lu' expects type 'long unsigned int', but argument 5 has type 'ino_t'
fs/ubifs/replay.c:1066: warning: format '%lu' expects type 'long unsigned int', but argument 7 has type 'ino_t'
fs/ubifs/orphan.c:108: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:135: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:142: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:154: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:159: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:451: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:539: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:612: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:843: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/orphan.c:856: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/recovery.c:1438: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/recovery.c:1443: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/recovery.c:1475: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/recovery.c:1495: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:105: warning: format '%lu' expects type 'long unsigned int', but argument 3 has type 'ino_t'
fs/ubifs/debug.c:105: warning: format '%lu' expects type 'long unsigned int', but argument 3 has type 'ino_t'
fs/ubifs/debug.c:110: warning: format '%lu' expects type 'long unsigned int', but argument 3 has type 'ino_t'
fs/ubifs/debug.c:110: warning: format '%lu' expects type 'long unsigned int', but argument 3 has type 'ino_t'
fs/ubifs/debug.c:114: warning: format '%lu' expects type 'long unsigned int', but argument 3 has type 'ino_t'
fs/ubifs/debug.c:114: warning: format '%lu' expects type 'long unsigned int', but argument 3 has type 'ino_t'
fs/ubifs/debug.c:118: warning: format '%lu' expects type 'long unsigned int', but argument 3 has type 'ino_t'
fs/ubifs/debug.c:118: warning: format '%lu' expects type 'long unsigned int', but argument 3 has type 'ino_t'
fs/ubifs/debug.c:1591: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1671: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1674: warning: format '%lu' expects type 'long unsigned int', but argument 5 has type 'ino_t'
fs/ubifs/debug.c:1680: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1699: warning: format '%lu' expects type 'long unsigned int', but argument 5 has type 'ino_t'
fs/ubifs/debug.c:1788: warning: format '%lu' expects type 'long unsigned int', but argument 5 has type 'ino_t'
fs/ubifs/debug.c:1821: warning: format '%lu' expects type 'long unsigned int', but argument 5 has type 'ino_t'
fs/ubifs/debug.c:1833: warning: format '%lu' expects type 'long unsigned int', but argument 5 has type 'ino_t'
fs/ubifs/debug.c:1924: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1932: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1938: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1945: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1953: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1960: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1967: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1973: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1988: warning: format '%lu' expects type 'long unsigned int', but argument 4 has type 'ino_t'
fs/ubifs/debug.c:1991: warning: format '%lu' expects type 'long unsigned int', but argument 5 has type 'ino_t'
fs/ubifs/debug.c:2009: warning: format '%lu' expects type 'long unsigned int', but argument 2 has type 'ino_t'

drivers/isdn/sc/shmem.c:57: warning: format '%d' expects type 'int', but argument 3 has type 'size_t'
drivers/net/mlx4/en_rx.c:289: warning: format '%llx' expects type 'long long unsigned int', but argument 4 has type 'dma_addr_t'
drivers/net/mlx4/en_netdev.c:1002: warning: format '%llx' expects type 'long long unsigned int', but argument 5 has type 'u64'


---
~Randy

Artem Bityutskiy

unread,
Oct 29, 2008, 2:20:07 AM10/29/08
to

I'll look at these and fix, thank you!

--
Best regards,
Artem Bityutskiy (Битюцкий Артём)

Artem Bityutskiy

unread,
Oct 29, 2008, 4:10:13 AM10/29/08
to
On Tue, 2008-10-28 at 16:11 -0700, Randy Dunlap wrote:
> On Thu, 23 Oct 2008 15:41:33 +0400 Alexey Dobriyan wrote:
>
> > If variable is of Type use printk format specifier.
> > ---------------------------------------------------------
> > int %d or %x
> > unsigned int %u or %x
> > long %ld ot %lx
> > unsigned long %lu or %lx
> > long long %lld or %llx
> > unsigned long long %llu or %llx
> > size_t %zu or %zx
> > ssize_t %zd or %zx
> >
> > Raw pointer value SHOULD be printed with %p.
> >
> > u64 SHOULD be printed with %llu/%llx, (unsigned long long):
> >
> > printk("%llu", (unsigned long long)u64_var);
> >
> > s64 SHOULD be printed with %lld/%llx, (long long):
> >
> > printk("%lld", (long long)s64_var);
> >
> > If type is dependent on config option (sector_t), use format specifier
> > of biggest type and explicitly cast to it.
> >
> > Reminder: sizeof() result is of type size_t.

Do we have this somewhere in Documentation? Would be nice to have it.

--
Best regards,
Artem Bityutskiy (Битюцкий Артём)

--

Artem Bityutskiy

unread,
Oct 29, 2008, 11:40:11 AM10/29/08
to
On Wed, 2008-10-29 at 08:34 -0700, Randy Dunlap wrote:
> It's in the C standard, which we don't try to put into Documentation/ AFAIK.
>
> section 6.5.3.4, The sizeof operator:
>
> 4 The value of the result is implementation-defined, and its type (an unsigned integer type)
> is size_t, defined in <stddef.h> (and other headers).

Well, OK. I was more thinking about printing stuff like ino_t.
UBIFS used %lu for this which was the source of warnings.

Randy Dunlap

unread,
Oct 29, 2008, 11:40:14 AM10/29/08
to
On Wed, 29 Oct 2008 10:00:04 +0200 Artem Bityutskiy wrote:

> On Tue, 2008-10-28 at 16:11 -0700, Randy Dunlap wrote:
> > On Thu, 23 Oct 2008 15:41:33 +0400 Alexey Dobriyan wrote:
> >
> > > If variable is of Type use printk format specifier.
> > > ---------------------------------------------------------
> > > int %d or %x
> > > unsigned int %u or %x
> > > long %ld ot %lx
> > > unsigned long %lu or %lx
> > > long long %lld or %llx
> > > unsigned long long %llu or %llx
> > > size_t %zu or %zx
> > > ssize_t %zd or %zx
> > >
> > > Raw pointer value SHOULD be printed with %p.
> > >
> > > u64 SHOULD be printed with %llu/%llx, (unsigned long long):
> > >
> > > printk("%llu", (unsigned long long)u64_var);
> > >
> > > s64 SHOULD be printed with %lld/%llx, (long long):
> > >
> > > printk("%lld", (long long)s64_var);
> > >
> > > If type is dependent on config option (sector_t), use format specifier
> > > of biggest type and explicitly cast to it.
> > >
> > > Reminder: sizeof() result is of type size_t.
>
> Do we have this somewhere in Documentation? Would be nice to have it.

It's in the C standard, which we don't try to put into Documentation/ AFAIK.

section 6.5.3.4, The sizeof operator:

4 The value of the result is implementation-defined, and its type (an unsigned integer type)
is size_t, defined in <stddef.h> (and other headers).


---
~Randy

Randy Dunlap

unread,
Oct 29, 2008, 11:50:10 AM10/29/08
to
Artem Bityutskiy wrote:
> On Wed, 2008-10-29 at 08:34 -0700, Randy Dunlap wrote:
>> It's in the C standard, which we don't try to put into Documentation/ AFAIK.
>>
>> section 6.5.3.4, The sizeof operator:
>>
>> 4 The value of the result is implementation-defined, and its type (an unsigned integer type)
>> is size_t, defined in <stddef.h> (and other headers).
>
> Well, OK. I was more thinking about printing stuff like ino_t.
> UBIFS used %lu for this which was the source of warnings.

I've been thinking of adding (trying to add) Alexey's hints in the
Doc/ tree in some new file.

--
~Randy

Randy Dunlap

unread,
Nov 5, 2008, 1:30:23 PM11/5/08
to
On Thu, 23 Oct 2008 15:41:33 +0400 Alexey Dobriyan wrote:

> If variable is of Type use printk format specifier.
> ---------------------------------------------------------
> int %d or %x
> unsigned int %u or %x
> long %ld ot %lx
> unsigned long %lu or %lx
> long long %lld or %llx
> unsigned long long %llu or %llx
> size_t %zu or %zx
> ssize_t %zd or %zx

Add, or is this one too infrequent to be listed here?

tcflag_t %lu or %lx or %lo

Since tcflag_t is unsigned int (on most $ARCH-es) or unsigned long (on sparc32),
use a cast to (unsigned long)flag when printing it.

[Yes, a sparc32 build gets lots of printk format warnings when printing tcflag_t
values.]


> Raw pointer value SHOULD be printed with %p.
>
> u64 SHOULD be printed with %llu/%llx, (unsigned long long):
>
> printk("%llu", (unsigned long long)u64_var);
>
> s64 SHOULD be printed with %lld/%llx, (long long):
>
> printk("%lld", (long long)s64_var);
>
> If type is dependent on config option (sector_t), use format specifier
> of biggest type and explicitly cast to it.
>
> Reminder: sizeof() result is of type size_t.
>
> Thank you for your cooperation.
> --

---
~Randy

0 new messages