Compiler gives warning when printing the address of a variable

前端 未结 3 1060
一整个雨季
一整个雨季 2021-01-29 05:36

I made a very simple program to print the address of two variables.

#include

int main()
{
    int a,b;
    printf(\"%u\\n%u\",&a,&b);
            


        
3条回答
  •  误落风尘
    2021-01-29 06:30

    The correct format specifier for printing an address (pointer) is %p and you need to cast the argument to void *.

    Hence, the warning is valid and should be there.

    But, when I compiled with GCC-5.x, it gave no warnings

    In case of gcc, please include -WallNote compiler option and try to compile. I believe it will throw the (same) warning we're expecting.


    Note: Actually, -Wformat, which checks for the type of supplied argument to printf() and scanf() family calls. -Wall enables -Wformat.

提交回复
热议问题