473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

printf and size_t

Should the following work *if* I have a c99 compiler please?

#include <stdio.h>

int main(void)
{
size_t t = 42;

# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99, so it should support %zu?
*/
printf("%zu", t);

# else

/* Not a C99 compiler - use lu.
*/
printf("%lu", (unsigned long)t);

# endif

return 0;
}

I'm using gcc 3.4.2 and the output is zu, this, I believe, shows that
the printf supplied doesn't recognize %zu as a valid format specifier?

BTW, where would you use plain ol' %z, vs. %zu, i.e, for what data
type?

Ta,

rayw

Aug 8 '06 #1
6 17344
ra*********@gmail.com said:
Should the following work *if* I have a c99 compiler please?
<snip>
>
/* C99, so it should support %zu?
*/
printf("%zu", t);
Yes, that's correct, although you might want to stick a \n in there.
# else

/* Not a C99 compiler - use lu.
*/
printf("%lu", (unsigned long)t);

# endif

return 0;
}

I'm using gcc 3.4.2 and the output is zu, this, I believe, shows that
the printf supplied doesn't recognize %zu as a valid format specifier?
Yes, it shows that the libc you are using is not C99-conforming.
BTW, where would you use plain ol' %z, vs. %zu, i.e, for what data
type?
You wouldn't. In C99's *printf, z is a length modifier like h or l, not a
conversion specifier like d or u. And in pre-C99, it's a bug. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 8 '06 #2

Richard Heathfield wrote:
ra*********@gmail.com said:
Should the following work *if* I have a c99 compiler please?
<snip>

/* C99, so it should support %zu?
*/
printf("%zu", t);

Yes, that's correct, although you might want to stick a \n in there.
Yup.
# else

/* Not a C99 compiler - use lu.
*/
printf("%lu", (unsigned long)t);

# endif

return 0;
}

I'm using gcc 3.4.2 and the output is zu, this, I believe, shows that
the printf supplied doesn't recognize %zu as a valid format specifier?

Yes, it shows that the libc you are using is not C99-conforming.
Ah, so the gcc I'm using hasn't come complete with a libc that's also
compliant.
BTW, where would you use plain ol' %z, vs. %zu, i.e, for what data
type?

You wouldn't. In C99's *printf, z is a length modifier like h or l, not a
conversion specifier like d or u. And in pre-C99, it's a bug. :-)
Duh - should have seen that! Thanks.

So, if %zu isn't supported, and I can't get a libc that does support
it, is %lu ok - or - should I use unsigned long long?

Even then, as far as I know, there's no way to know whether even that
has enough bits to hold a size_t (right?) - should I check that with an
assertion say - that sizeof(size_t) <= sizeof(unsigned long) etc?

Many thanks,

rayw

Aug 8 '06 #3
ra*********@gmail.com said:
Richard Heathfield wrote:
>ra*********@gmail.com said:
>
I'm using gcc 3.4.2 and the output is zu, this, I believe, shows that
the printf supplied doesn't recognize %zu as a valid format specifier?

Yes, it shows that the libc you are using is not C99-conforming.

Ah, so the gcc I'm using hasn't come complete with a libc that's also
compliant.
Hold on there - what makes you think the compiler is C99-conforming?!?

<snip>
So, if %zu isn't supported, and I can't get a libc that does support
it, is %lu ok - or - should I use unsigned long long?
Use: printf("%lu\n", (unsigned long)sizeof object);

unless you are likely to be dealing with objects greater than 4294967295
bytes in size, in which case make sure first that unsigned long is big
enough. If you are and it isn't, you may be forced to use unsigned long
long if your compiler supports it.
Even then, as far as I know, there's no way to know whether even that
has enough bits to hold a size_t (right?) - should I check that with an
assertion say - that sizeof(size_t) <= sizeof(unsigned long) etc?
Well, that's why you need to cast - because printf is variadic, the
arguments you supply to it won't automagically be converted, so you have to
be very careful with types. But provided the /value/ fits inside an
unsigned long, the cast is necessary and sufficient.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 8 '06 #4

Richard Heathfield wrote:
ra*********@gmail.com said:
Richard Heathfield wrote:
ra*********@gmail.com said:

I'm using gcc 3.4.2 and the output is zu, this, I believe, shows that
the printf supplied doesn't recognize %zu as a valid format specifier?

Yes, it shows that the libc you are using is not C99-conforming.
Ah, so the gcc I'm using hasn't come complete with a libc that's also
compliant.

Hold on there - what makes you think the compiler is C99-conforming?!?
Because this ...

# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99, so it should support %zu?
*/
printf("%zu", t);

...

outputs 'zu' - so it looks as though my gcc says that it's c99
compliant (__STDC_VERSION__ isn't defined in stdio.h (which is all I
have included) so I'm assuming it's defined 'internally' by the
compiler - which certainly sounds like it ought to work that way, e.g.,
that you 'ask' the compiler rather that 'trust' an include file)?

<snip>
So, if %zu isn't supported, and I can't get a libc that does support
it, is %lu ok - or - should I use unsigned long long?

Use: printf("%lu\n", (unsigned long)sizeof object);

unless you are likely to be dealing with objects greater than 4294967295
bytes in size, in which case make sure first that unsigned long is big
enough. If you are and it isn't, you may be forced to use unsigned long
long if your compiler supports it.
Even then, as far as I know, there's no way to know whether even that
has enough bits to hold a size_t (right?) - should I check that with an
assertion say - that sizeof(size_t) <= sizeof(unsigned long) etc?

Well, that's why you need to cast - because printf is variadic, the
arguments you supply to it won't automagically be converted, so you have to
be very careful with types. But provided the /value/ fits inside an
unsigned long, the cast is necessary and sufficient.
Does this look ok?

int main(void)
{
size_t t = 42;

# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99, so it should support %zu?
*/
printf("%zu", t);

# else

/* Not a C99.
**
** Use unsigned long OR unsigned long long instead.
*/

# if sizeof(size_t) <= sizeof(unsigned long)

printf("%lu", (unsigned long)t);

# else

printf("%llu", (unsigned long long)t);

# endif

# endif

return 0;
}

Aug 8 '06 #5
rayw said:
Richard Heathfield wrote:
>>
Hold on there - what makes you think the compiler is C99-conforming?!?

Because [...] it looks as though my gcc says that it's c99
compliant
Consider the difference between "says that it is" and "is". :-)

<snip>
Does this look ok?

int main(void)
{
size_t t = 42;

# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99, so it should support %zu?
*/
printf("%zu", t);
We already know this doesn't work if the libc is not conforming (see
upthread).
>
# else

/* Not a C99.
**
** Use unsigned long OR unsigned long long instead.
*/

# if sizeof(size_t) <= sizeof(unsigned long)
Alas, sizeof is a C operator, not a preprocessor directive. This won't
compile (or rather, it won't preprocess!).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 8 '06 #6
"rayw" <ra*********@gmail.comwrites:
Richard Heathfield wrote:
>ra*********@gmail.com said:
Richard Heathfield wrote:
ra*********@gmail.com said:

I'm using gcc 3.4.2 and the output is zu, this, I believe, shows that
the printf supplied doesn't recognize %zu as a valid format specifier?

Yes, it shows that the libc you are using is not C99-conforming.

Ah, so the gcc I'm using hasn't come complete with a libc that's also
compliant.

Hold on there - what makes you think the compiler is C99-conforming?!?

Because this ...

# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99, so it should support %zu?
*/
printf("%zu", t);

...

outputs 'zu' - so it looks as though my gcc says that it's c99
compliant (__STDC_VERSION__ isn't defined in stdio.h (which is all I
have included) so I'm assuming it's defined 'internally' by the
compiler - which certainly sounds like it ought to work that way, e.g.,
that you 'ask' the compiler rather that 'trust' an include file)?
Yes, __STDC_VERSION__ is defined by the compiler, not by the runtime
library. The standard doesn't actually say so; the compiler and the
runtime library are both part of the implementation.

gcc itself is just a compiler; it uses whatever runtime library is
available on the system. __STDC_VERSION__ *should* reflect the
conformance of the implementation as a whole. Theoretically, gcc
could detect what runtime library it's using, and set __STDC_VERSION__
accordingly, but as far as I know it doesn't.

So if __STDC_VERSION__ is 199901L, but the implementation doesn't
support "%zu", that's a bug in the implementation.

Note also that gcc itself doesn't support all of C99; see
<http://gcc.gnu.org/c99status.htmlfor details.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bilbo | last post by:
Hello! I have a matrix type of variable, lets call it matrix...can I see on screen it via printf?? how?? Thanks
17
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t...
6
by: David Mathog | last post by:
size_t varies from system to system. This occasionally leads to coding issues such as: (void) sprintf(msg,"length of buffer: %d",strlen(msg)); which worked fine on a 32 linux but triggered...
5
by: diadia | last post by:
#include <sys/types.h> #include <sys/stat.h> #include "ctype.h" #include <stdio.h> int estimateLen(struct stat buf, FILE *fp) { size_t _size = buf.st_size / 4;
25
by: Joakim Hove | last post by:
Hello, I have code which makses use of variables of type size_t. The code is originally developed on a 32 bit machine, but now it is run on both a 32 bit and a 64 bit machine. In the code...
16
by: Jordan Abel | last post by:
I have written this function and was wondering if anyone else could point out if there's anything wrong with it. The purpose is to substitute for printf when in a situation where low-level I/O has...
5
by: dank | last post by:
http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx describes the integer format modifiers accepted by Microsoft's printf. ...
25
by: Podrzut_z_Laweczki | last post by:
Hello, I have question (or 2 :)). Is that true that for a large data using scanf/printf instead of cin/cout makes that the program runs faster? And if it is, is it legal to mix scanf/printf with...
11
by: vectorizor | last post by:
Hi guys, My program deals with with lots of large matrices. To easily debug it, I would like to be able to dump them in files, so that I can easily import then in other software (think matlab,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.