开发者

C: Expected output

开发者 https://www.devze.com 2023-03-07 12:31 出处:网络
#include <stdio.h> int main() { long long x = 0x8ce4b16b; long long y = x<<4; printf(\"%lx, %lx, 开发者_开发百科abc\\n\", x, y);
#include <stdio.h>
int main()
{
    long long x = 0x8ce4b16b;
    long long y = x<<4;
    printf("%lx, %lx, 开发者_开发百科abc\n", x, y);
    return 0;
}

I'm getting

8ce4b16b, 0, abc... Is this okay?

However if I change printf like printf("%lld, %lx, abc\n", x, y);

The output becomes:

2363797867, ce4b16b0, abc

Why could have been this behaviour!! :(


Using incorrect format specifier in printf invokes Undefined Behaviour. The correct format specifier for long long is %lld.

Also make sure that you dont have signed integer overflow in your code because that's UB too.


You should use printf("%llx, %llx, abc\n", x, y); in my mind. %lx for long integer.

0

精彩评论

暂无评论...
验证码 换一张
取 消