[OracleOSS] [TitleIndex] [WordIndex]

OCFS2/CodingStyle/Style Printks

Printing u64s

Hi Folks,

Please read the below post - I keep having to fix up printk-related build warnings, usually because I miss them on x86_64, then someone builds on ia64 or ppc, etc and e-mails me. This is trivial to avoid though - we keep making the same mistake:

{
        u64 block;
        printk("block: %llu\n", block);
}

That code above will generate a warning similar to:
fs/ocfs2/xxx.c:lineo: warning: long long unsigned int format, u64 arg (arg X)

Instead, you must always cast 'u64' and 's64' to whatever you're asking printk to print. For example, the correct form of the above example would be:

{
        u64 block;
        printk("block: %llu\n", (unsigned long long)block);
}

There's more regarding size_t, s64, etc below.
        --Mark

----- Forwarded message from Alexey Dobriyan <adobriyan@gmail.com> -----

Date:   Thu, 23 Oct 2008 15:41:33 +0400
From: Alexey Dobriyan <adobriyan@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: How do I printk <type> correctly?
User-Agent: Mutt/1.5.18 (2008-05-17)

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.

2011-12-23 01:01