简体   繁体   English

程序打印错误的输出

[英]Program prints the wrong output

This program should print "SNP" but it prints "SNW".该程序应该打印“SNP”,但它会打印“SNW”。

#include <stdio.h> 
int main() { 
    int o = '!?zx' 
            ^ '!h4+'; 
    printf("%s\n", &o); 
    return (o ^ o); 
} 

I tried your program and got SNW .我尝试了您的程序并获得了SNW

As ^ is the XOR operation, I made it by hand and got the same result:由于^XOR操作,我手工制作并得到相同的结果:

        Here is the problem
                |||
                VVV
 00100001 00111 111  01111010 01111 00   <- !?zx
 00100001 01101 000  00110100 00101011   <- !h4+
--------------------------------------- XOR
 00000000 01010 111  01001110 01011111   <- binary result
       00        57        4E       53   <- hexadecimal result
                  W         N        S   <- characters

Are you sure it would output SNP normally?你确定它会正常输出SNP吗? The XOR operator between ?之间的异或运算符? and h gives 00111111^01101000 which is 01010111 , or W in Unicode or ASCII. h给出00111111^01101000 ,即01010111 ,或 Unicode 或 ASCII 中的W P is 01010000 . P01010000

So I marked the problematic bits above and put them below.所以我在上面标记了有问题的部分并将它们放在下面。 The issue is that, it gives问题是,它给

the bits   111
before |   000
       V  ----- XOR
    01010  111

and 01010 111 makes W, instead of 01010 000 which would make P.和 01010 111生成 W,而不是 01010 000生成 P。

Also, due to endianness, you can get WNS as well as SNW .此外,由于字节顺序,您可以获得WNSSNW

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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