《C程序设计语言》学习记录(未完结,不定时更新)
1. 不等号的优先级高于等号
若把 while ( (c = getchar ()) != EOF )
写成?while ( c = getchar () != EOF ),就相当于是这条语句:
while ( c =?(getchar () != EOF)?) 意义完全不同
?
2. long 类型需要在使用printf函数时用%ld来说明。
?
3. getchar ()函数会把 换行 字符也读入缓存
?
#include <stdio.h>main (){ long nc = 0; int c; while ( (c = getchar ()) !=EOF ) { printf ("c is %d\n",c); if (c != 10) /* or use: if ( c != '\n' ) */ nc++; } printf ("There's %ld characters.\n",nc);}?
另外,可以直接用scanf ()函数,scanf ()函数本身就不会读取末尾的换行。
?
?
4、gcc编译时报错误:`true' undeclared (first use in this function)?
在编译C语言时有时会遇到这样的错误提示:
`true' undeclared (first use in this function) or `false' undeclared (first use in this function)
就是说 bool, true, false 都是undeclared,原因很简单,因为真正的C中没有这些关键字,c和早期的c++里没有关键字bool,使用BOOL可以,但BOOL不是内置类型了,都是通过typedef或者宏来定义的,通常都会被定义成int类型。后来的c++出现了内置类型bool,值只能为true(1)和false(0)。
解决方法:
1、将文件名.c改为文件名.cpp,用C++方式编译则没问题
2、自己进行一个宏定义:
?
typedef enum __bool { false = 0, true = 1, } bool;
c90是没有bool的,因此支持c90的dev-c++当然也没有。想在c90里用bool,可以自行用宏进行定义。?
c99支持bool,用支持c99的编译器例如gcc就可以的。
?
参考:http://blog.sina.com.cn/s/blog_6a6094d501011g2h.html