Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using %zu correct syntax in a printf format string as shown in some C code found on Wikipedia?

Tags:

c

printf

sizeof

I just found this code on Wikipedia.
Link: http://en.wikipedia.org/wiki/Sizeof#Use

The code:

/* the following code illustrates the use of sizeof 
 * with variables and expressions (no parentheses needed),
 * and with type names (parentheses needed)
 */

char c;

printf("%zu,%zu", sizeof c, sizeof(int));

It states that: "The z prefix should be used to print it, because the actual size can differ on each architecture."

I tried it on my compiler, but it gives the following result:

zu,zu
like image 972
m4design Avatar asked May 28 '10 16:05

m4design


People also ask

Which one is the correct syntax of printf ()?

The syntax of printf() function is given below: printf("format string",argument_list);

What format is used to print a string with the printf function?

The C++ Printf Prototype When you use the printf() function, it prints the string pointed out by the format to the standard output stdout. The format can also contain some specifiers that start with a % and replace values of variables with the printf() function.

What is the format specific used to print a string in C?

"printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (lexing aka. parsing).

What is %d %f %s in C?

The first argument to printf is a string of identifiers. %s refers to a string %d refers to an integer %c refers to a character. Therefore: %s%d%s%c\n prints the string "The first character in sting ", %d prints i, %s prints " is ", and %c prints str[0]. Follow this answer to receive notifications.


4 Answers

Yes that syntax is correct (at least for C99). Looks like your compiler isn't set up to handle it though. Just take out the z and you'll probably be fine. To be correct, make sure your printf format specifiers match the size of the types. Turning on all the warnings your compiler will give you probably helps out in that respect.

Your quotation:

The z prefix should be used to print it, because the actual size can differ on each architecture

is referring to the fact that size_t (which is the type returned by the sizeof operator) can vary from architecture to architecture. The z is intended to make your code more portable. However, if your compiler doesn't support it, that's not going to work out. Just fiddle with combinations of %u, %lu, etc. until you get the output making sense.

like image 151
Carl Norum Avatar answered Sep 25 '22 10:09

Carl Norum


The z length modifier was added to C in the C99 standard; you might have a compiler that doesn't support C99.

If your C compiler doesn't support that, you can probably treat the sizes as unsigned long:

printf("%lu,%lu", (unsigned long)sizeof c, (unsigned long)sizeof(int));
like image 34
nos Avatar answered Sep 24 '22 10:09

nos


Yes, but it only works on C99-compliant compilers. From wikipedia:

z: For integer types, causes printf to expect a size_t sized integer argument.

like image 22
BlueRaja - Danny Pflughoeft Avatar answered Sep 25 '22 10:09

BlueRaja - Danny Pflughoeft


Did you tell your compiler that you want it thinking with a C99 brain? There is probably a switch to do that. For instance, -std=c99 for gcc.

If your compiler does not support it, but you know others will, you can do a PRId64 style work around (disclaimer - PSEUDO CODE AHEAD ..):

#ifdef __SOME_KNOWN_C99_COMPILER
#define PORTUNSIGNED "zu"
#else
#define PORTUNSIGNED "u"
#endif

printf("%-11" PORTUNSIGNED " ways to skin a cat\n");

Its probably better to get a compiler that has functional support for c99, however.

like image 22
Tim Post Avatar answered Sep 23 '22 10:09

Tim Post