9

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.

2
  • Please use a consistent indenting style. It will help both us and you read your code.
    – bdonlan
    Jul 11, 2011 at 19:33
  • @bdonlan and Bart, Thanks for the suggestion and i am sorry for everything :)
    – thetna
    Jul 11, 2011 at 19:40

5 Answers 5

4

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

4

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.

3
2

For portable code, #include <inttypes.h> and use PRIdPTR in your printf format string.

printf("test value is %" PRIdPTR, P);

The Wikipedia page for inttypes.h has a link to a version of that file that will work with Visual C++, and would probably work with VS2010 as well (if Microsoft didn't add an inttypes.h).

PRIdPTR is for intptr_t, PRIuPTR is for uintptr_t.

1
1

Visual C++, as of VS 2013, does not support %z. If you want future versions to support it then vote on this bug:

https://connect.microsoft.com/VisualStudio/feedback/details/806338/vc-printf-and-scanf-should-support-z

1
  • I believe that support for this was added to VS 2015. Sep 4, 2015 at 5:59
0

To print size_t you need to use %Iu in VS and %zu in gcc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.