面试宝典之程序设计基本概念

来源:互联网 发布:网络兼职骗局信誉代刷 编辑:程序博客网 时间:2024/05/01 19:01
#include <stdio.h>int i = 1;int main(void){int i = i;int a = a;printf("%d\n",i);printf("%d\n",a);return 0;}


输出结果:



------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>using namespace std;int main(void){int x = 2, y, z;x *= (y = z = 5); cout << x << endl;z = 3;x == (y = z); cout << x << endl;x = (y == z); cout << x << endl;x = (y&z);    cout << x << endl;x = (y && z); cout << x << endl;y = 4;x = (y | z);  cout << x << endl;x = (y || z); cout << x << endl;return 0;}

输出结果:



注意:书上的结果缺少了最后面的那个1


-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>using namespace std;int func(int x){int count = 0;while(x){count ++;x = x&(x-1);}return count;}int main(void){cout << func(9999) << endl;return 0;}

输出结果:



因为9999的二进制为:



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>using namespace std;int main(void){int a, x;for (a = 0, x = 0; a <= 1 && !x++; a++){a++;}cout << a << x << endl;return 0;}


#include <iostream>using namespace std;int main(void){int a, x;for (a = 0, x = 0; a <= 1 && !x++;){a++;}cout << a << x << endl;return 0;}


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>int main(void){int b = 3;int i = 0;int arr[] = {6, 7, 8, 9, 10};signed int *ptr = arr;*(ptr++) += 123;printf("%d, %d\n", *ptr, *(++ptr));for (i = 0; i < 5; i++){printf("%d\n", arr[i]);}return 0;}

结果为:


----------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>int main(void){unsigned int a = 0xFFFFFFF7;unsigned char i = (unsigned char )a;char *b = (char *)&a;printf("%08x, %08x", i, *b);return 0;}

结果为:


-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>//#include <stdio.h>//#include <string.h>//#include <conio.h>using namespace std;int main(void){float a = 1.0f;cout << (int )a << endl;cout << &a << endl;cout << (int &)a << endl;cout << boolalpha << ((int) a == (int &)a) << endl;float b = 0.0f;cout << (int )b << endl;cout << &b << endl;cout << (int &)b << endl;cout << boolalpha << ((int) b == (int &)b) << endl;return 0;}


-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>#include <stdio.h>using namespace std;int main(void){unsigned char a = 0xA5;unsigned char b = ~a >> 4 + 1;cout << b << endl;printf("%d\n", b);return 0;}


注意:

cout << b << endl;


输出的为不可见字符。

1 0