|
Prev: error 'undefined reference'
Next: misra-c++
From: Barry Schwarz on 27 May 2008 21:58 On Tue, 27 May 2008 08:45:29 -0700 (PDT), Andrew Falanga <af300wsm(a)gmail.com> wrote: >On May 27, 9:31 am, Richard Heathfield <r...(a)see.sig.invalid> wrote: >> Andrew Falanga said: >> >> > Hi, >> >> > I know that %u is for unsigned integers, but, if memory serves me, >> > that's only for 32 bit integers. >> >> Not so. It is for unsigned ints. >> >> > What modifier would I use just >> > before the 'u'? For example, (again if memory serves), to display an >> > object of type long long, I would use "%ll", correct? >> >> Nearly. To display the /value/ of an object of type long long, in a decimal >> representation, you would use "%lld". >> >> > So, what's >> > necessary for an unsigned 64 bit integer? >> >> It depends on the type. If it's a short int, use "%hu". If it's an int, use >> "%u". If it's a long int, use "%lu". And if it's a long long int (always >> assuming your platform supports them, of course), use "%llu". C imposes no >> upper limit on the width of the basic integral data types. Even char is >> allowed to be 64 bits wide - or more! >> >> > You know, I don't know if it would make any difference but I'm using a >> > 64 bit compiler. >> >> C is C. It doesn't matter how many bits your compiler has, as long as it >> conforms to the language spec. >> >> > I'm wondering if, in this case, it matters at all. >> > Since the word size of the compiler is 64 bits, would I simply use %u >> > and call it good? >> >> No, your choice should be based on the specific type, not on bit-count. >> >> -- >> Richard Heathfield <http://www.cpax.org.uk> >> Email: -http://www. +rjh@ >> Google users: <http://www.cpax.org.uk/prg/writings/googly.php> >> "Usenet is a strange place" - dmr 29 July 1999 > >Thank you. To be honest, it's been a while since I've had to use >format specifiers. Most of the code I've written lately I've been >able to use the C++ I/O objects. Although this is C++, I'm coming >into the project late and don't quite know the full extent of the >environment. I don't know why, but we have to use format specifiers >for output. Thanks again. I have no idea if C++ adopted the exact width integer types of C99 but if your system supports int64_t and uint64_t (the only types guaranteed to be exactly 64 bits) then it should support the corresponding formats (PRId64 and PRIu64). Remove del for email
From: Keith Thompson on 28 May 2008 12:33 Andrew Falanga <af300wsm(a)gmail.com> writes: [ questions and answers about printf formats deleted ] > Thanks. I learned something interesting. Using %lu produced nothing > but a '?' in the output as did using %u. The only thing that works is > %ld, but this would be for a signed in, no? "%ld" is for signed long int (also known as signed long, long int, or long). "%lu" is for unsigned long int. If "%ld" works, I'd be very surprised if "%lu" produces just a '?'. Can you show us some actual code that exhibits this behavior? Note: By "actual code" I mean a small, self-contained, compilable program that we can try outselves without modification. Copy-and-paste your actual code; don't re-type it. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Keith Thompson on 28 May 2008 18:34 Andrew Falanga <af300wsm(a)gmail.com> writes: > On May 28, 10:33 am, Keith Thompson <ks...(a)mib.org> wrote: >> Andrew Falanga <af300...(a)gmail.com> writes: >> >> [ questions and answers about printf formats deleted ] >> >> > Thanks. I learned something interesting. Using %lu produced nothing >> > but a '?' in the output as did using %u. The only thing that works is >> > %ld, but this would be for a signed in, no? >> >> "%ld" is for signed long int (also known as signed long, long int, or >> long). "%lu" is for unsigned long int. >> >> If "%ld" works, I'd be very surprised if "%lu" produces just a '?'. >> Can you show us some actual code that exhibits this behavior? >> >> Note: By "actual code" I mean a small, self-contained, compilable >> program that we can try outselves without modification. >> Copy-and-paste your actual code; don't re-type it. [...] > > I wished I could give you what you're asking for here but in this case > I can't. Besides, I'm so new to the build environment that, to be > honest if I could copy/paste, it wouldn't do much good. Nowhere do I > see people using printf() or other standard calls. It's all things > like: > > Print() > UINT64 > > and so forth. Since both of these, on the surface anyway, go outside > of standard C or C++, it's beyond the scope anyway. I don't even know > what include files are necessary for all this magic to take place. > Basically, this is what I have (that would be compilable but for my > lack of understanding the environment): > > // some includes here, don't know what > int main() { > UINT64 bigInt = 3; > > Print("bigInt contains %lu\n", bigInt); > > return 0; > } > > That's what I have, but the output looks like: > bigInt contains ? > > The compiler isn't catching that the %ld is for a signed, rather than > unsigned, 64bit int. For this reason, I'm betting that somewhere, > someone's not turning on something like "-Wformat" (that's for GNU > gcc). I dug up that little tid-bit after finding out why I've been > slapped before in the past for using a format specifier that didn't > match the data type. > > Shoot, this environment is so unfamiliar to me that I've never seen a > system where you need to define the entry point in a header file, but > you do with this. Obviously I don't know what Print() does. I'll assume in the following that its format string means the same thing as the format string for printf() (if it doesn't, somebody should find the designer and hurt him). I'm still very surprised that this would print a '?' character. It should attempt to print, in decimal, the value of the argument, which it will assume is of type unsigned long. Barring undefined behavior, you should get nothing but a string of decimal digits. In any case, you need to make sure that the type of the argument matches the format string. In this case, you don't really know that UINT64 and unsigned long are the same type, or even compatible. You can avoid this problem with a cast: printf("bigInt contains %lu\n", (unsigned long)bigInt); or, assuming Print behaves sanely: Print("bigInt contains %lu\n", (unsigned long)bigInt); This is one of the few cases where a cast is a good idea. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
|
Pages: 1 Prev: error 'undefined reference' Next: misra-c++ |