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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值