有如下程序 #include <stdio.h> main() { int i, array[5] = {3, 5, 10, 4}; for (i=0; i<5; i++) printf("%d,", array[i] &

admin2020-06-16  25

问题 有如下程序
#include <stdio.h>
main()
{
    int i, array[5] = {3, 5, 10, 4};
    for (i=0; i<5; i++)
        printf("%d,", array & 3);
    printf("\n");
}
程序运行后的输出结果是

选项 A、3,1,2,0,0,
B、3,5,10,4,0,
C、3,3,3,3,0,
D、3,2,2,2,0,

答案A

解析 在对数组进行初始化时,如果在说明数组时给出了长度,但没有给所有的元素赋予初始值,而只依次给前面的几个数组元素赋予初值,那么C语言将自动对余下的元素赋初值0,则array={3,5,10,4,0}。按位与运算"&",当参加运算的两个二进制数的对应位都为1,则该位的结果为1,否则为0。将数组元素与3=11B按位与,即3&3=3,5&3=1,10&3=2,4&3=0,0&3=0。For循环输出与运算结果:3,1,2,0,0,A选项正确。
转载请注明原文地址:https://kaotiyun.com/show/MiCp777K
0

最新回复(0)