Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't %zd printf format work in VS2010?

Following piece of my code does not print the value in visual studio.

int main() { 
    intptr_t P = 10;
    printf("test value is %zd",P);
    return 0;
}

OUTPUT:

test value is zd

I expect the the above code print

test value is 10

i am using intptr_t instead of integer so as to make the code to adjust in both the 32 bit and 64 bit architecture.

like image 645
thetna Avatar asked Jul 11 '11 19:07

thetna


People also ask

What is %ZD format specifier?

rL295112: Use "%zd" format specifier for printing number of testcases executed. This helps to avoid signed integer overflow after running a fast fuzz target for several hours, e.g.: <...>

What does %ZD mean C?

printf with a "%zd" format expects an argument of the signed type that corresponds to the unsigned type size_t . Standard C doesn't provide a name for this type or a good way to determine what it is.

What is %zu in printf?

The correct way to print size_t variables is use of “%zu”. In “%zu” format, z is a length modifier and u stand for unsigned type. The following is an example to print size_t variable.

What is %B in printf?

The Printf module API details the type conversion flags, among them: %B: convert a boolean argument to the string true or false %b: convert a boolean argument (deprecated; do not use in new programs).


2 Answers

The z prefix isn't defined in Microsoft's version of printf. I think the I prefix might work. See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx

like image 186
Mark Ransom Avatar answered Oct 10 '22 22:10

Mark Ransom


Although the z length specifier is supported in C99, Visual Studio 2010 does not support C99, and instead conforms to an older version of C that was missing the z length specifier. As an extension, VS2010 does support the I length specifier instead for size_t, but this is not portable to other platforms.

I would recommend using an unsigned long long with the %llu specifier instead; the overhead is minimal and it's portable to C99 platforms as well.

like image 37
bdonlan Avatar answered Oct 10 '22 23:10

bdonlan