简体   繁体   中英

same printf gives different results

This is my first post and hope this is not a repost. I wrote the following c++ program on mac using g++ to compile.

#include <iostream>
int main ()
{

    double b = 1;
    printf("%x\n", b);
    printf("%x\n", b);
    printf("%x\n", b);
    printf("%x\n", b);
    return 0;

}

why the program gives me different results of the same code? Here is the output.

5ca5ebf8
100
200
300

This behavior is not shown in VS on Windows though. thanks.

This is the warning you should get with any decent compiler: warning: format specifies type 'unsigned int' but the argument has type 'double'

So you get undefined behaviour, anything can happen :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM