MRaZY MRain is CraZY! This is old CodeBeta.

27六/080

C++之我头晕:关于stdio

一样的先来看一段代码:

#include <stdio.h>
int main(void)
{
     int age;
     char name[80];
     puts("Enter your age.");
     scanf("%d", &age);
     puts("Enter your name.");
     scanf("%s", name);
 
     printf("Age is %d.\n", age);
     printf("Name is %s.", name);
 
     return 0;
}

当然在程序里是看不出什么有趣的地方..

如果你把它运行起来...第一个输入"23.00"..会发生什么奇妙的事吗?

显然发生了...这都是伟大的scanf函数的功劳..

首先是读入一个整形变量...输入是"23.00"..他把"23"读走了..还剩".00"留在stdin里..

于是到了第二个scanf函数里..读入一个字符串..就把".00"取走了..于是就奇妙了..

22五/086

C++之严谨篇第一话:强制类型转换及编译器优化

#include <iostream>
using namespace std;
int main()
{
	const int i=10;
	int *pi,*pt;
	pi=(int *)&i;
	pt=(int *)&i;
	cout<<*pi<<endl;
	*pi=11;
	cout<<*pi<<" "<<i<<" "<<*pt<<endl;
	cout<<pi<<" "<<&i<<" "<<pt<<endl;
	return 0;
}

以上的代码输出会是什么呢?

在Linux下或是在Windows下结果都一样...用了微软的C++以及开源的G++编译器

最后结果相同.

首先是那几个变量的地址是一样的.

在输出*pi,*pt时结果是11,输出i的结果却是10