蒋迪的博客
| 关于 | 归档

#C语言 K&R C语言 课后习题

第一章

符号常量

#define TRUE 1
#define FALSE 0

字符输入和输出

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

上述例程中,c不能被定义为char类型的数据,因为EOF 是一个整型数据(int),被定义在<stdio.h>中;

Exercise 1 试试看EOF输出的值为多少;

#include <stdio.h>
int main(int argc, char *argv[]){

	printf("EOF= %x\n",EOF);

	return 0;
}

测试后的结果为EOF= ffffffff;

webIt is a macro definition of type int that expands into a negative integral constant expression (generally, -1).

Exercise 2 统计输入字符流中的\t,\n和1\b等符号;下面以统计\t为示例。

#include <stdio.h>
int main(int argc,char *argv[]){

	int nc = 0;
	int c ;
	while((c= getchar()) != EOF)
		if( c == '\t')
		nc++;
	printf("\ncount \\t =  %d\n",nc);
}

Exercise 3 Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

#include <stdio.h>
int main(int argc,char *argv[]){
	int nb;
	int i = 0;
	int c;
	char string[100] = {0};
	while((c = getchar()) != EOF){
		if(c == ' ' && nb == 0){
			string[i++]=c;
			nb++;		
		}
		else if( c == ' ' && nb != 0)
			continue;
		else{
			string[i++]=c;
			nb=0;
		}	
	}
	printf("\n%s\n",string);
	return 0;
}
/*or more simplfy*/
#include <stdio.h>
int main(int argc,char *argv[]){
	int i = 0;
	int c,c_prev;
	char string[100] = {0};
	c_prev=0;
	while((c = getchar()) != EOF){
		if( c == ' ' && c_prev == ' '){
			c_prev=c;
			continue;
		}
		string[i++]=c;
		c_prev=c;
	}
	printf("%s\n",string);
	return 0;
}

Exercise 11 How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?

#include <stdio.h>
#define IN 1 /* inside a word*/

#define OUT 0 /* outside a word*/

int main(int argc , char *argv[]){
	int c ,nl,nw,nc,state;
	state = OUT;
	nl = nw = nc = 0;
	while((c = getchar()) != EOF){
		++nc;
		if(c == '\n')
			++nl;
		if(c == ' ' || c == '\n' || c == '\t')
			state = OUT;
		else if (state == out){
			state = IN;
			++nw;
		}
	}
	printf("nc = %d nw = %d nl =%d\n",nc,nw,nl);
}

Solutions: 如何完善一个程序的单元测试?

Exercise 12 Write a program that prints its input one word per line.

第二章

常量

  1. "hello," "world"等于 "hello, world"

    This is useful for splitting up long strings across several source lines.

类型转换

  1. If either operand is long double, convert the other to long double.
  2. Otherwise, if either operand is double, convert the other to double.
  3. Otherwise, if either operand is float, convert the other to float.
  4. Otherwise, convert char and short to int.
  5. Then, if either operand is long, convert the other to long.
  • 无符号数和有符号数之间的转换
  • 显性符号转换(type name)expression

赋值操作与表达式

  1. expr1 op= expr2 = expr1 = expr1 op expr2;
    • x = y+1 = x = x * (y+1);

位操作

Exercise 2-6

Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

Exercise 2-7

Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.

Exercise 2-8

Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n positions.

第三章

第四章

externel 外部变量

external variables are globally accessible.

By default, external variables and functions have the property that all references to them by the same name, even from functions compiled separately, are references to the same thing.

A declaration announces the properties of a variable (primarily its type); a definition also causes storage to be set aside.

static 变量

statci 变量有两种用法:

  1. 在(函数中)声明static变量(internal),意味着该变量是private,permanent storage within a single function.
  2. 在声明static变量(external)时,同样也能用于定义(声明)函数,通常external变量时全局的,如果一个变量或者函数被声明为static,仅对该文件内可见。

register 变量

In practice, there are restrictions on register variables, reflecting the realities of underlying hardware

变量的初始化

In the absence of explicit initialization, external and static variables are guaranteed to be initialized to zero; automatic and register variables have undefined (i.e., garbage) initial values.

第五章

appendix A 优先级

  1. != 优先级 高于=; 例程 c = getchar() != EOF 等于 c = (getchar() != EOF)
  2. nl = nw = nc = 0 等于nl = (nw = (nc = 0)); 通过一条语句将三个变量全部置0,赋值语句从右到左执行;

Reference

  1. Representing EOF in C code?
  2. What is EOF in the C programming language?