[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question regarding sscanf() vs. off_t and similar

From: Philipp Marek
Subject: Re: Question regarding sscanf() vs. off_t and similar
Date: Thu, 14 Sep 2006 16:39:41 +0200
User-agent: KMail/1.9.3

On Thursday 14 September 2006 15:01 Andreas Schwab wrote:
> Philipp Marek <address@hidden> writes:
> > If I'm doing
> >     ino_t ino;
> >     char *name;
> >     sprintf("%Lu %s", ino, name);
> Note that "L" is not a valid length modifier for integer formats.
For gnu libc it is:

> > that works fine for 64bit systems, but writes trash for sizeof(ino_t) ==
> > 4.
> >
> > Is there an easy way to find the format strings (sprintf, printf, sscanf)
> > for various system types, or something like that?
> Assuming you meant %llu instead of %Lu you can always add a cast to
> unsigned long long to match the format.
Yes. But that's a bit unclean, and doing that unnecessarily in a loop for 
~300000 iterations is something I'd like to avoid.

> > Or has somebody some already defined preprocessor magic to get this for
> > some variable?
> > I'm thinking of something along the lines
> >     #define FORMAT(x)
> >       #if sizeof(x) == 4
> >         "%l"
> >       #else
> >         #if sizeof(x) == 8
> >           "%L"
> >         #else
> >           #error "Don't know size"
> >         #endif
> >       #endif
> You can't use sizeof in preprocessor directives. 
Yes, that was meant as some kind of pseudo-code.

> Besides that, the size 
> of an integer says nothing about its type.  For example, a 4 byte integer
> can be either int or long on an ILP32 system, and an 8 byte integer can be
> long or long long on an LP64 system.
But that main problem arises if the printf() format doesn't match the size 
pushed on the stack - then the remaining arguments are read in the wrong 

If I know that some variable is 32bit, and %lu takes a 32bit integer from the 
stack, I'm saved.

Thank you for your answer - I think I'll have to go with casting everything.



Versioning your /etc, /home or even your whole installation?
             Try fsvs (!

reply via email to

[Prev in Thread] Current Thread [Next in Thread]