getchar的相关问题请教
如图1所示,有两个getchar分别跟在scanf后面;
图1
如图2所示,把两个getchar移到程序的最后面
图2
两个图运行后的结果是,图1中的程序会自动退出,图2中的程序会等待用户输入回车程序才结束。
我想问的问题是,为什么getchar移动了一下位置,执行结果竟然会不相同呢?
附代码:
#include "stdafx.h"
#include <string.h>
#define DENSITY 62.4
int _tmain(int argc, _TCHAR* argv[])
{
float weight,volume;
int size,letters;
char name[40];
printf("Hi!What's your first name?\n");
scanf("%s",name);
printf("%s,what's your weight in pounds?\n",name);
scanf("%f",&weight);
getchar();
size=sizeof name;
letters=strlen(name);
volume=weight/DENSITY;
printf("Well,%s,your volume is %2.2f cubic feet.\n",
name,volume);
printf("Also,your first name has %d letters,\n",letters);
printf("and we have %d bytes to store it in.\n",size);
getchar();
getchar();
return 0;
}