关于编译C程序

来源:百度知道 编辑:UC知道 时间:2024/06/06 05:10:59
/* Program 2.12 Finding the size of a type */
#include <stdio.h>

int main(void)
{
printf("\nVariables of type char occupy %d bytes", sizeof(char));
printf("\nVariables of type short occupy %d bytes",sizeof(short));
printf("\nVariables of type int occupy %d bytes",sizeof(int));
printf("\nVariables of type long occupy %d bytes",sizeof(long));
printf("\nVariables of type float occupy %d bytes",sizeof(float));
printf("\nVariables of type float occupy %d bytes",sizeof(double));
printf("nVariables of type long double occupy %d bytes",sizeof(long double));
getch();
return 0;
}
为什么会出错?
加在哪里?
#include<conio.h>

把getch()改为getchar(),程序就没有任何问题了,完成程序如下:

#include <stdio.h>

int main(void)
{
printf("\nVariables of type char occupy %d bytes", sizeof(char));
printf("\nVariables of type short occupy %d bytes",sizeof(short));
printf("\nVariables of type int occupy %d bytes",sizeof(int));
printf("\nVariables of type long occupy %d bytes",sizeof(long));
printf("\nVariables of type float occupy %d bytes",sizeof(float));
printf("\nVariables of type float occupy %d bytes",sizeof(double));
printf("nVariables of type long double occupy %d bytes",sizeof(long double));
getch();
return 0;
}

在TURBOC和UNIX下都执行成功。

getch(); 在头文件conio.h中
加上#include<conio.h>就可以了
加在#include<stdio.h>的上面或下面都可以
这事包含头文件,预编译的,放在程序的开头