第二章:
问题3:你需要用printf()函数打印出下面的这段文本(包括两边的双引号)。你应该使用什么样的字符串常量参数。
"Blunder??!??"
答:printf("\"Blunder\?\?!?\"");
问题5:下面的声明存在什么错误?(如果有的话)?
int x/*blah blah*/y;
答:预处理器用一个空格替换注释,使得所产生的语句是非法的。如上述语句产生的结果为:int x y;这是不合法的声明。
编程题:
编写一个程序,它由3个函数组成,每个函数分别保存在一个单独的源文件中。函数increment接受一个整形参数,它的返回值是该参数的值加1。increment函数应该位于increment.c文件中。第2个函数称为negate,它也接受一个整型参数,它的返回值是该参数的负值。
编写一个程序,它从标准输入读取c源代码,并验证所有的花括号都正确地成对出现。注意:你不必担心注释内部,字符串常量内部和字符常量形式的花括号。
问题3:你需要用printf()函数打印出下面的这段文本(包括两边的双引号)。你应该使用什么样的字符串常量参数。
"Blunder??!??"
答:printf("\"Blunder\?\?!?\"");
问题5:下面的声明存在什么错误?(如果有的话)?
int x/*blah blah*/y;
答:预处理器用一个空格替换注释,使得所产生的语句是非法的。如上述语句产生的结果为:int x y;这是不合法的声明。
编程题:
编写一个程序,它由3个函数组成,每个函数分别保存在一个单独的源文件中。函数increment接受一个整形参数,它的返回值是该参数的值加1。increment函数应该位于increment.c文件中。第2个函数称为negate,它也接受一个整型参数,它的返回值是该参数的负值。
/*demo.h*/
#include <stdlib.h>
#include <stdio.h>
int increment(int a);
int negate(int v);
/*increment.cpp*/
#include "demo.h"
int increment(int a) {
return a + 1;
}
/*negate.cpp*/
#include "demo.h"
int negate(int v) {
return -v;
}
/*main.cpp*/
#include "demo.h"
int main() {
printf("%d %d\n",increment(10),negate(10));
printf("%d %d\n", increment(0), negate(0));
printf("%d %d\n", increment(-10), negate(-10));
system("PAUSE");
return 0;
}
编写一个程序,它从标准输入读取c源代码,并验证所有的花括号都正确地成对出现。注意:你不必担心注释内部,字符串常量内部和字符常量形式的花括号。
int main() {
char ch;
int numbers = 0;
while ((ch = getchar()) != EOF) {
if (ch == '{') {
numbers += 1;
}
if (ch == '}') {
if (numbers == 0) {
printf("不匹配");
} else {
numbers -= 1;
}
}
if (numbers > 0) {
printf("%d\n", numbers);
}
}
system("PAUSE");
return 0;
}