Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 2 Nov 2014 07:48:11 -0500 (EST)
From:      Rick Macklem <rmacklem@uoguelph.ca>
To:        Ian Lepore <ian@FreeBSD.org>
Cc:        Freebsd hackers list <freebsd-hackers@freebsd.org>
Subject:   Re: how to kernel printf a int64_t?
Message-ID:  <1123726553.4004621.1414932491616.JavaMail.root@uoguelph.ca>
In-Reply-To: <1414900709.17308.243.camel@revolution.hippie.lan>

next in thread | previous in thread | raw e-mail | index | archive | help
Ian Lepore wrote:
> On Sun, 2014-11-02 at 11:20 +0800, Julian Elischer wrote:
> > On 11/2/14, 10:14 AM, Rick Macklem wrote:
> > > Julian Elischer wrote:
> > >> On 10/31/14, 1:09 PM, Tim Kientzle wrote:
> > >>
> > >>
> > >> On Oct 30, 2014, at 2:01 PM, Rick Macklem <rmacklem@uoguelph.ca>
> > >> wrote:
> > >>
> > >> Hi,
> > >>
> > >> I feel kinda dumb asking this, but...
> > >>        int64_t i;
> > >>
> > >>        printf("%qd\n", (u_quad_t)i);
> > >>
> > >> works but looks dorky, to put it technically;-).
> > >> Is there a better way to printf() a int64_t in the kernel? I
> > >> often
> > >> use the following to print large integers:
> > >>
> > >>      printf(=E2=80=9C%jd\n=E2=80=9D, (intmax_t)i); the "cannonical' =
way is to
> > >>      use
> > >>      PRIu64 and friends, but some people seem to have a problem
> > >>      with
> > >>      doing that.
> > >>
> > > Ok, so now I need to ask another dumb question.
> > > How do you do this in the kernel?
> > > (I can see them defines in <machine/_inttypes.h>, but including
> > > that
> > >   doesn't help, which isn't surprising since PRIu64 is in a
> > >   string
> > >   and won't be recognized as a macro.)
> >=20
> > you use it with string concatenation.
> > like:
> >=20
> >    printf (" this is a 64 it unsigned value:  %" PRIu64 " and I
> >    just
> > printed it\n", thingy64);
> >=20
> > After substitution the compiler sees
> >   " this is a 64 it unsigned value: %" "llu" " and I just printed
> >   it\n"
> > which simplifies to:
> > " this is a 64 it unsigned value: %llu and I just printed it\n"
> >=20
> > due to concatenation. (note I didn't actually look what PRIu64
> > evaluates to)
> >=20
> >=20
>=20
> Which is exactly the explanation for why "some people seem to have a
> problem with doing that."  "Some people" would be "anyone who thinks
> it
> should be possible to read code as well as write it."  This may be
> more
> correct in some pedantic sense, but %j and a cast is more readable.
>=20
Yes, thanks. I'll admit to thinking exactly the same thing.
I guess I'll use %j.

Thanks everyone for your help, rick

> -- Ian
>=20
> > >
> > > Oh, and is intmax_t going to be int64_t on all arches?
> > >
> > > Thanks, rick
> > >
> > >>
> > >> Tim
> > >>
> > >> _______________________________________________
> > >> freebsd-hackers@freebsd.org mailing list
> > >> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To
> > >> unsubscribe, send any mail to
> > >> "freebsd-hackers-unsubscribe@freebsd.org"
> > >>
> > >
> > >
> >=20
> > _______________________________________________
> > freebsd-hackers@freebsd.org mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> > To unsubscribe, send any mail to
> > "freebsd-hackers-unsubscribe@freebsd.org"
> >=20
>=20
>=20
> _______________________________________________
> freebsd-hackers@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to
> "freebsd-hackers-unsubscribe@freebsd.org"
>=20



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1123726553.4004621.1414932491616.JavaMail.root>