Output problems in 64-bit environments

Asked 2 years ago, Updated 2 years ago, 45 views

#include <stdio.h>

int main() {
    int num = 20;
    int* pnum = &num;

    printf("Size of pointer variable: %dbytes\n", sizeof(pnum)));

    return 0;
}

When I changed the build platform to 64bit (x64) in Visual Studio 2017, and compiled the above code, the warning message below appears.

warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type '::size_t'
note: consider using '%zd' in the format string

The program itself works well, but how do I get rid of the warning message?

c

2022-09-22 16:55

1 Answers

printf("%d", size of(pnum)) as shown in warning;is requested to change to printf("%zd", sizeof(pnum);

I don't think there will be any problem if I change it. Or you can write:

int size = sizeof(pnum);

printf("size of pointer variable: %dbytes\n", size);


2022-09-22 16:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.