C和指针(Chapter 1 & Chapter 2)

主要整理自己看c和指针(英文版)遇到的一些知识点。部分可能用英文表述。

1.What is the advantage of putting declarations, such as function prototypes, in the header files and then using #include to bring the declarations to the source files where they are needed ?
The declarations only needs to be written only once, making it easier to maintain if there are some modifications later. Also writing it only once eliminates the chance that additional copies are written differently from each other.

2.Write a program that reads a line from the standard input. Each line is printed on the standard output preceded by its line number. Try to write the program that has no built-in limit on how long a line it can handle.

#include <stdio.h>
#define N 50
int main()
{
                 int number = 0;
                 char input[N];
                 while (gets(input) != NULL ){
                                number++;
                                printf( "line%d:%s\n", number, input);
                }
                 return 0;
}

当一个数组作为函数参数,这个函数无法知道数组大小,因此gets函数无法避免overflow the input array。而fgets函数,则另外需要一个数组大小作为函数参数(argument)则可以避免这个问题。
不妨用getchar()来写,代码如下

#include <stdio.h>
int main()
{
                 int ch;
                 int line = 1;//行数
                 int at_beginning = 1;
                
                 while ((ch = getchar()) != EOF ) {
                                 if (at_beginning == 1) {
                                                printf( "line%d ", line++);
                                                at_beginning = 0;
                                }
                                putchar(ch);
                                 if (ch == '\n' ) {
                                                at_beginning = 1;
                                }
                }
                 return 0;
}

</pre><pre name="code" class="cpp">3.Write a C program that reads  C source code from the standard input and ensure that braces are paired correctly. 

#include <stdio.h>
int main()
{
                 int ch;
                 int count = 0;
                 while ((ch = getchar()) != '\n' ) {
                                 if (ch == '{' ) {
                                                count++;
                                }
                                 else if (ch == '}') {
                                                 if (count == 0) {
                                                                printf( "not match\n");
                                                                 return;
                                                }
                                                 else {
                                                                count--;
                                                }
                                }
                }
                 if (count == 0)
                                printf( "match\n");
                 else
                                printf( "not match\n");
                 return 0;
}


4.三字母词(trigraphs)
老式键盘没有一些类似于[]{}|^~#\所以用??加上其他符号代替
所以当你在输入printf("??(\n"); 屏幕会上显示 [
三字母词表如下(9个):
??( [
??) ]
??< {
??> }
??! |
??' ^
??= #
??/ \
??- ~
不过我自己用的VS2013好像默认不支持了,因为现在键盘上都有这些字符,需要 GCC调-trigraphs 参数来控制是否启用三字母词

5.转义字符(escape sequences or character escapes)
\? \" \' \\
比如想要输出\n 或者"就必须这么写
printf( "\"");
printf("\\n");
想要在屏幕上输出如下 "blunder??!??"
printf("\"Blunder\?\?!??\"");
另外\ddd代表1到3位的八进制数,\xddd代表1到3位的十六进制数

最后是给出一道C和指针书上的习题



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值