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

版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/cnxxrj/article/details/29813109

#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;


输出的为不可见字符。

posted on 2019-04-17 16:37  xfgnongmin  阅读(106)  评论(0编辑  收藏  举报

导航