C程序设计语言(K&R) 笔记2

(1)

#include <stdio.h>

main(){

        int c;
        while((c = getchar()) != EOF){
                putchar(c);
        }
}

注意,因为 != 的优先级比 赋值= 的优先级高。如果改为

        while(c = getchar() != EOF)  c被赋值为0或1
将会出现错误输出:

(2)计数

#include <stdio.h>

main(){

        long count=0;
        while(getchar() != ‘q‘){
                ++count;
        }
        printf("%ld\n",count);
}
时间: 2024-08-26 09:53:15

C程序设计语言(K&R) 笔记2的相关文章

C语言K&R习题系列——句子中一个空格代替多个空格的四种方法

原题: Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. 第一种: 这种最常用,设置一个inspace作为布尔变量,标志当前输入是否在字符中,或在字符外 #include <stdio.h>   int main(void) {   int c;   int inspace=0;     while((c = getcha

C语言K&R习题系列——统计文档中每个单词所占字母个数,以直方图形式输出

原题: Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging. 这也是我第一个过百行的代码(带注释,空格什么的) 主要分两个部分:输入和输出 #include < stdio.h > #define

C语言K&R习题系列——统计一段文字中各个字母出现的频率

原题: /*Write a program to print a histogram of the frequencies of *difficent characters in it inputs */ 这个和上一个类似 输入部分 #include < stdio.h >    #define NUM_CHARS 256    main ( void )  { int c; int done = 0; int thisIdx = 0; long frequrr[NUM_CHARS + 1];

C语言K&R习题系列——使用缓冲区函数接受长字符输入

原题: Write a program to print all input lines that are longer than 80 characters.  ,实现起来不算难,关键是用到了缓冲区,很不错的一种思想! /* Write a program to print all input lines  * that are longer than 80 characters  */    #include < stdio.h >    #define MINLENGTH 81    /

《C程序设计语言》读书笔记----习题1-21

题目就不写了,大概意思就是:尽量用制表符'\t'替换掉字符串中的空格. 同学们需要注意的是,打印一个制表符'\t',其所占长度不是固定的. 这里要理解“制表符”和“制表符终止位”.“制表符”的作用是使得光标移动到下一个“制表符终止位”上.举个例子,假设制表符终止位是4.8.12.16......已经已经输入了10个字符,然后按一下Tab键,那么光标会移动到位置12上,同学们新建一个文本文档试一下就了解了. 这个题目看似简单,但是写一个简单.清晰的程序还是需要花一点脑筋的. /* 这个程序看似简单

《C程序设计语言》读书笔记----习题1-20

练习1-20:编写程序detab,将输入中的制表符替换成适当数目的空格,使得空格充满到下一个制表符终止位的地方,.假设制表符终止位的位置时固定的,比如每隔n列就会出现一个终止位. 这里要理解“制表符”和“制表符终止位”.“制表符”的作用是使得光标移动到下一个“制表符终止位”上.举个例子,假设制表符终止位是4.8.12.16......已经已经输入了10个字符,然后按一下Tab键,那么光标会移动到位置12上,同学们新建一个文本文档试一下就了解了. 代码如下: 1 #include<stdio.h>

《C程序设计语言》学习笔记

1. 在C程序中,如果字符串过长而需要跨行时,要在换行时加上“\”. printf("Hello, world "); // error printf("Hello, world\n"); // ok

C语言K&amp;R习题系列——句子中一个空格代替多个空格的四种方法

原题: Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. 第一种: 这种最常用,设置一个inspace作为布尔变量,标志当前输入是否在字符中,或在字符外 #include <stdio.h> int main(void) { int c; int inspace=0; while((c = getchar()) != EO

C语言K&amp;R习题系列——统计一段文字中各个字母出现的频率

原题: /*Write a program to print a histogram of the frequencies of *difficent characters in it inputs */ 这个和上一个类似 输入部分 #include < stdio.h > #define NUM_CHARS 256 main ( void ) { int c; int done = 0; int thisIdx = 0; long frequrr[NUM_CHARS + 1]; long t

《C专家编程》笔记(一)——C语言的历史、K&amp;R C和ANSI C

1. C语言的许多特性是为了方便编译器设计者而建立的.于是C语言的语言特性有:数组下标从0而非1开始:C语言的基本数据类型直接与底层硬件相对应:auto关键字只对创建符号表入口的编译器设计者有意义:表达式中的数组名可以看作是指针:float被自动扩展为double(ANSI C中不再如此):不允许嵌套函数(简化了编译器):register关键字,为编译器设计者提供线索,却把包袱丢给了程序员. 2. C编译器不曾实现的一些功能必须通过其他途径实现(编译器实现的功能,如加减乘除.指针.取地址).在C