1.题略 #include <stdio.h> int main(void) { ; printf("Please enter text here(end with Ctrl + Z):\n"); while (ch=getchar() != EOF) i++; printf("There are %d characters in text.\n", i); ; } 运行结果 输入第一个Ctrl+Z时,并没有结束,下一行再输入Ctrl+Z才检测到EOF.…
缓冲区 缓冲区分为两类:完全缓冲(fully buffered)I/O和行缓冲(line-buffered)I/O.完全缓冲在缓冲区满时被清空(内容被发送至目的地).这种类型常出现在文件输入中.缓冲区的大小取决于系统.行缓冲在遇到换行字符时被清空.键盘输入是标准的行缓冲. 文件.流和键盘输入 c程序处理一个流而不是一个文件.键盘输入由一个被称为stdin的流表示,而到屏幕(或电子传真机.或其他输出设备)上的输出由一个被称为stdout的流表示. 文件结尾 检测文件结尾的一种方法是在文件中放置一个…
1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i; printf("ch[%d] = %c\n", i-97, ch[i-97]); } printf("That is all! thanks~\n"); return 0; } 这是之前写的,有点乱,改了些如下: #include int main(void) {…
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; printf("Convert mins to hours and minutes\n"); printf("Please enter the mins: \n"); scanf("%d", &mins); while (mins >…
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[20]; printf("My Handsome Master, please enter your name: \n"); scanf("%s %s", &name, &family); //名字和姓氏一起读的话,需要先输入名字再输入姓氏,中间要有空白字…
1. /*rain.c 针对若干年的降水量数据,计算年降水总量.年降水平均量,以及月降水平均量*/ #include <stdio.h> #define MONTHS 12 #define YEARS 5 int main (void) { //把数组初始化为2000到2004年的降水量数据 const float rain[YEARS][MONTHS] = //const声明和初始化数组可保护数据 { {4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4,…
Review long代替int类型变量的原因是什么? 在您的系统中,long可以容纳比int更大的数:如果您确实需要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型会使程序的可移植性更好.(PS:用sizeof(int)查看我电脑中的int发现是4字符即32位,和long一样,但是long是标准的32位,int在我这64位的系统中定义的是32位,在其他系统可能是16位.无论如何,有个标准,最好按标准来设定,这样移植起来就方便) 要获得一个32位的有符号整数,可以使用哪些可以值得数…
1.题略 /*返回较小值,设计驱动程序测试该函数*/ #include <stdio.h> double min (double a, double b); int main (void) { double x, y; printf ("Please enter two numbers: \n"); scanf ("%lf %lf", &x, &y); printf ("The smaller one is %lf\n"…
作业练习 1. #include <stdio.h> int main(void) { char ch; int spare, other, n; //空格,其他字符,换行 spare = other = n = 0; while ((ch = getchar()) != '#') { if (ch == ' ') spare++; else if (ch == '\n') n++; else other++; } printf("spare:%d\nn:%d\nother:%d&q…
1. #include <stdio.h> int main(){ char ch; int ct = 0; while ((ch=getchar()) != EOF) ct++; printf("%d characters read.", ct); return 0; } 2. #include <stdio.h> int main(){ char ch; int ct = 0; while ((ch = getchar()) != EOF) { if (ch…