1. 以下代码结果是多少?
#include <stdio.h>
int func(int x)
{
int count =0;
while(x)
{
count++;
x=x&(x-1);
}
return count;
}
int main(int argc, char const *argv[])
{
printf("%d\n", func(9999));
return 0;
}
解析:func函数返回值是形参X转化成二进制后包含的1的数量;
结果为:
8
2.下面两段的输出结果有什么不同?
1).
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a ,x;
for (a=0,x = 0; a <1 && !x++;a++ )
{
a++;
}
printf("a = %d x = %d \n",a,x);
return 0;
}
结果为:
a = 2 x = 1
2).
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a ,x;
for (a=0,x = 0; a <1 && !x++;a++ )
{
a++;
}
printf("a = %d x = %d \n",a,x);
return 0;
}
结果为:
a = 1 x = 1
解析:考for循环的循环顺序
3. 以下代码结果是多少?
#include <stdio.h>
int main(int argc, char const *argv[])
{
int b=3;
int arr[]={6,7,8,9,10};
int *ptr=arr;
*(ptr++)+=123;
printf("%d %d\n", *ptr,*(++ptr));
return 0;
}
结果为:
8 8