Skip to contentSkip to conversation

printk and long long

DISCUSSION sting sting 12 participants 17 messages Feb 11, 2004 – Feb 13, 2004

lore
Share & tools
lore
#0 lore raw

Hello,
I am trying to perfrom printk with a variable of type long long. (loff_t is that type and it is long long , as can be seen in posix+types.h). what is the format string for such a type ?
I had tried %lld" but it gace wrpng results.
regards,
sting

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

#1 lore raw

how about simply using a shift to output two regular longs, i.e.

printk("%ld%ld",loff_t >> (sizeof(long) * 8), loff_t << sizeof(long) * 8 >> sizeof(long) * 8);

perhaps you could even place this ghastly code in a macro if you have to access it often (so that you don't have to look at it :)?

I know it's not pretty, but at least the %ld is considered standard printf functionality. I don't think %lld (even if it is implemented in some printf derivates) can be considered portable.

Willem
Citeren sting sting <[email protected]>:

Quoted 19 lines

Hello,
I am trying to perfrom printk with a variable of type long long. (loff_t is that type and it is long long , as can be seen in posix+types.h).
what is the format string for such a type ?
I had tried %lld" but it gace wrpng results.
regards,
sting

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#2 lore raw
Quoted 4 lines

how about simply using a shift to output two regular longs, i.e.

printk("%ld%ld",loff_t >> (sizeof(long) * 8), loff_t << sizeof(long) * 8 >> sizeof(long) * 8);

I'd venture to guess you'd also have to cast the above to long.

Cheers,
MaZe.

#3 lore raw
Quoted 6 lines · Maciej Zenczykowski
Quoted 4 lines

how about simply using a shift to output two regular longs, i.e.

printk("%ld%ld",loff_t >> (sizeof(long) * 8), loff_t << sizeof(long) * 8

Quoted 1 line
Quoted 1 line

sizeof(long) * 8);

I'd venture to guess you'd also have to cast the above to long.

Hey that will work only for %x, not %d.

BTW, man printf says:

The character L specifying that a following e, E, f, g, or G conversion corresponds to a long double argument, or a following d, i, o, u, x, or X conversion corresponds to a long long argument. Note that long long is not specified in ANSI C and therefore not portable to all architectures.
--
vda

#4 lore raw
Quoted 5 lines · vda

The character L specifying that a following e, E, f, g, or G conversion corresponds to a long double argument, or a following d, i, o, u, x, or X conversion corresponds to a long long argument. Note that long long is not specified in ANSI C and therefore not portable to all architectures.

[ personally I'd say screw the un-portable architectures ;) ] Long long is here to stay.
Besides if a linux architecture utilises long long in the kernel and doesn't support it in printf via %lld then it's horked. printf/libc should be fixed instead.
Maybe that's the problem - the libc support fragment in the kernel tree is not up to date on that architecture - maybe the fixes should applied there instead - instead of trying to work around the problem, fix the cause.

Cheers,
MaZe.

#5 lore raw
Followup to:  <[email protected]>
By author:    Maciej Zenczykowski <[email protected]>
In newsgroup: linux.dev.kernel
Quoted 11 lines · vda
Quoted 6 lines

The character L specifying that a following e, E, f, g, or G conversion corresponds to a long double argument, or a following d, i, o, u, x, or X conversion corresponds to a long long argument. Note that long long is not specified in ANSI C and therefore not portable to all architectures.

[ personally I'd say screw the un-portable architectures ;) ] Long long is here to stay.

long long is C99, so it's *definitely* here to say. The conversion specifier is "ll" not "L", however.

Quoted 6 lines

Besides if a linux architecture utilises long long in the kernel and doesn't support it in printf via %lld then it's horked. printf/libc should be fixed instead.
Maybe that's the problem - the libc support fragment in the kernel tree is not up to date on that architecture - maybe the fixes should applied there instead - instead of trying to work around the problem, fix the cause.

Indeed. Feel free to steal the code from klibc :)

-hpa

Signature · 4 lines
PGP public key available - finger [email protected]
Key fingerprint: 2047/2A960705 BA 03 D3 2C 14 A8 A8 BD  1E DF FE 69 EE 35 BD 74
"The earth is but one country, and mankind its citizens."  --  Bahá'u'lláh
Just Say No to Morden * The Shadows were defeated -- Babylon 5 is renewed!!
#6 lore raw
Quoted 12 lines · [email protected] (H. Peter Anvin) writes:
Followup to:  <[email protected]>
By author:    Maciej Zenczykowski <[email protected]>
In newsgroup: linux.dev.kernel
Quoted 9 lines · vda
Quoted 6 lines

The character L specifying that a following e, E, f, g, or G conversion corresponds to a long double argument, or a following d, i, o, u, x, or X conversion corresponds to a long long argument. Note that long long is not specified in ANSI C and therefore not portable to all architectures.

Quoted 5 lines
Quoted 2 lines

[ personally I'd say screw the un-portable architectures ;) ] Long long is here to stay.

long long is C99, so it's *definitely* here to say. The conversion specifier is "ll" not "L", however.

What is the proper way to deal with printing an int64_t when int64_t can be either long or long long depending on machine?

Signature · 2 lines
Måns Rullgård
[email protected]
#7 lore raw
Quoted 2 lines · Måns Rullgård

What is the proper way to deal with printing an int64_t when int64_t can be either long or long long depending on machine?

Print it as long long, and even if there is an arch where that is 128 bits it'll still work.

Chris

Signature · 4 lines
Chris Friesen                    | MailStop: 043/33/F10
Nortel Networks                  | work: (613) 765-0557
3500 Carling Avenue              | fax:  (613) 765-2986
Nepean, ON K2H 8E9 Canada        | email: [email protected]
#8 lore raw
Quoted 7 lines · Chris Friesen
Quoted 2 lines · Måns Rullgård

What is the proper way to deal with printing an int64_t when int64_t can be either long or long long depending on machine?

Print it as long long, and even if there is an arch where that is 128 bits it'll still work.

In that case you'll want to cast it to (long long) before you pass it so as to be sure that "%lld" and the parameter are the same size. Right?

#9 lore raw
In reply to Måns Rullgård
Quoted 2 lines · [email protected] (Måns Rullgård) writes:

What is the proper way to deal with printing an int64_t when int64_t can be either long or long long depending on machine?

PRId64 from <inttypes.h> (replace d with the desired format character). This is for user space, not sure whether that is acceptable for kernel code (<intttypes.h> is not one of the required headers for freestanding implementations).

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux AG, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#10 lore raw
Quoted 9 lines · Andreas Schwab
Quoted 2 lines · [email protected] (Måns Rullgård) writes:

What is the proper way to deal with printing an int64_t when int64_t can be either long or long long depending on machine?

PRId64 from <inttypes.h> (replace d with the desired format character). This is for user space, not sure whether that is acceptable for kernel code (<intttypes.h> is not one of the required headers for freestanding implementations).

That should work for userspace. What standard specifies those? What about kernel sources?

Signature · 2 lines
Måns Rullgård
[email protected]
#11 lore raw
Followup to:  <[email protected]>
By author:    [email protected] (=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=)
In newsgroup: linux.dev.kernel
Quoted 8 lines
Quoted 5 lines · Andreas Schwab
Quoted 2 lines · [email protected] (Måns Rullgård) writes:

What is the proper way to deal with printing an int64_t when int64_t can be either long or long long depending on machine?

Quoted 8 lines
Quoted 4 lines

PRId64 from <inttypes.h> (replace d with the desired format character). This is for user space, not sure whether that is acceptable for kernel code (<intttypes.h> is not one of the required headers for freestanding implementations).

That should work for userspace. What standard specifies those? What about kernel sources?

C99 defines those.

Another (frequently easier) way is to cast to (intmax_t) and use the %j size modifier:

	printf("foo = %jd\n", (intmax_t)foo);

	-hpa
Signature · 4 lines
PGP public key available - finger [email protected]
Key fingerprint: 2047/2A960705 BA 03 D3 2C 14 A8 A8 BD  1E DF FE 69 EE 35 BD 74
"The earth is but one country, and mankind its citizens."  --  Bahá'u'lláh
Just Say No to Morden * The Shadows were defeated -- Babylon 5 is renewed!!
#12 lore raw
In reply to vda

vda <[email protected]> wrote:
| Note that long long is not specified in ANSI C and therefore | not portable to all architectures.

ANSI C corresponds to ISO-C90, which is 14 years old now. The current C standard is ISO-C99, which specifies "ll" as the length modifier for "long long" integer arguments.

Signature · 3 lines
Dick Streefland                      ////                      Altium BV
[email protected]           (@ @)          http://www.altium.com
--------------------------------oOO--(_)--OOo---------------------------
#14 lore raw

Quoting Måns Rullgård <[email protected]>:

Quoted 3 lines

And how do you plan to make sense of the printed value?

perhaps I was a bit quick with replying. As others have pointed out, you should specify the precision to be able to glue the two parts together. Ideally, this should be done using a "%.8lx%.8lx", since the length of the variable in hex is static. Unfortunately, it seems %ld is requested, in which case "%ld*(2^32)+%ld" works (for humans), but is obviously not great.

I guess you'll need a number of extra tests (if lower 32bits > 1 billion ..) and specific printk statements if the output should be shown as a correct 10-base number.

I don't know, it depends on the specific use-case, really. If at all possible, I would stick to the hex-representation.

#15 lore raw
In reply to [email protected]
Quoted 8 lines · On Wed, 11 Feb 2004 [email protected] wrote:

how about simply using a shift to output two regular longs, i.e.

printk("%ld%ld",loff_t >> (sizeof(long) * 8), loff_t << sizeof(long) * 8 >> sizeof(long) * 8);

perhaps you could even place this ghastly code in a macro if you have to access it often (so that you don't have to look at it :)?

Quoted 15 lines

I know it's not pretty, but at least the %ld is considered standard printf functionality. I don't think %lld (even if it is implemented in some printf derivates) can be considered portable.

Willem
Citeren sting sting <[email protected]>:

Quoted 8 lines

Hello,
I am trying to perfrom printk with a variable of type long long. (loff_t is that type and it is long long , as can be seen in posix+types.h).
what is the format string for such a type ?
I had tried %lld" but it gace wrpng results.
regards,
sting

This is probably portable. I could do it much simpler in assembly but it wouldn't be portable.

Printing both unsigned long long and signed long long.

#include <stdio.h>

unsigned long long tester0 = 12345678901234567890ULL; unsigned long long tester1 = 0xffffffffffffffff;

char *ulltoa(char *buf, unsigned long long val)

{
    char tmp[0x20];
    char *cp = tmp;
    char *rp = buf;
    do {
            *cp++ = (char) (val%10ULL) + '0';
            val /= 10ULL;
       } while (val);
    do {
           *buf++ = *(--cp);
       } while (cp != tmp);
    *buf = 0x00;
    return rp;
}

char *lltoa(char *buf, long long val)
{
char *cp = buf;

   if(val < 0) {
        *cp++ = (char) '-';
        val = ~val + 1LL;

}
(void) ulltoa(cp, (unsigned long)val);
return buf;
}

int main()

{
    char buf[0x100];

    printf("%llu\n", 0ULL);
    printf("%llu\n", tester0);
    printf("%llu\n", tester1);
    printf("%s\n", ulltoa(buf, 0ULL));
    printf("%s\n", ulltoa(buf, tester0));
    printf("%s\n", ulltoa(buf, tester1));
    printf("%s\n", lltoa(buf, tester1));

    return 0;
}

Cheers,
Dick Johnson
Penguin : Linux version 2.4.24 on an i686 machine (797.90 BogoMips).
Note 96.31% of all statistics are fiction.

#16 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Richard" == Richard B Johnson <[email protected]> writes:

Quoted 4 lines · Richard> On Wed, 11 Feb 2004 [email protected] wrote:
Quoted 4 lines

how about simply using a shift to output two regular longs, i.e.

printk("%ld%ld",loff_t >> (sizeof(long) * 8), loff_t << sizeof(long) * 8 >> sizeof(long) * 8);

Why bother? printk already handles 64-bit types just fine.

Do

	loff_t x;
	printk("%lld\n" (long long)x)

You need the cast, because on 64-bit architectures, loff_t is long not long long.

Peter c