随笔分类 -  c语言

共享内存实例
摘要:源码书上关于进程间通过共享内存实现通信的例子。遇到了问题操作过程中,一直提示 shmat error, 很纳闷。解决调试了很长时间,突然想起shmat会将错误原因存于error中,所以在源代码中加了一句 printf("%s\n", sys_errlist[errno]); 运行结果显示 Permission denied. 原来是没有权限,运行时 加上 sudo , OK。解决.代码如下m1.c#include #include #include #include #include #include #include #define BUF_SIZE 1024#defin 阅读全文
posted @ 2013-08-22 15:17 Still_Raining 阅读(335) 评论(2) 推荐(0)
stdout 与 stderr 的区别
摘要:stdout 与 stderr 的区别一直没有注意 stdout 与 stderr 的区别,以为只是不同的描述方式。看来不是这样的。stdout 主要处理的是使用者输出stderr 主要处理的错误信息输出相比stdout, stderr没有缓冲设置将"正常输出"和"错误信息输出"加以分离,可以让程序以不同的方式对待两种不同的输出,例如可以将错误信息显示在控制台上,而正常输出重新定向到某个文件上。Example#include void main(){ fprintf(stdout, "from stdout\n"); fprintf( 阅读全文
posted @ 2013-08-11 11:31 Still_Raining 阅读(1179) 评论(1) 推荐(1)
printf
摘要:碰到了printf的一个很奇葩的用法。A width or precision may be specified as *, in which case the value is compputed by converting the next argument (which must be an int). For example, to print at most max characters from a string s.#include #include int main() { int max; char *s = "this is a test!\n"... 阅读全文
posted @ 2013-08-10 23:37 Still_Raining 阅读(174) 评论(0) 推荐(0)
Table Lookup
摘要:做OJ的时候,做过类似的,即hash。算法很简单,关键是书上写的和做OJ,是完全不同的风格。有很多值得学习的地方。 /* * Table Lookup * 详见>(英文版*第二版) P143 * * This code is typical of what might be found in the symbl table * management routines of a macro processor or a compiler. * For example, consider the #define statment. When a line like * #define IN 阅读全文
posted @ 2013-08-09 23:46 Still_Raining 阅读(506) 评论(0) 推荐(0)
qsort
摘要:qosrt的这种写法,很简单,也很好理解。 一开始没看懂,因为这一句swap(v, left, (left + right)/2); 后来转念一想,不是把left当成分割点,而是把(left+right)/2当分割点,恍然大悟。 代码如下:#include void swap(int v[], int i, int j) { int t = v[i]; v[i] = v[j]; v[j] = t;}void qsort(int v[], int left, int right) { int i, last; if(left >= right) ... 阅读全文
posted @ 2013-08-07 20:58 Still_Raining 阅读(230) 评论(0) 推荐(0)
Macro Substitution
摘要:看《C程序设计语言》(英文版)学到的两个用法。 两个很简单的宏用法。 #的用法: if, however, a parameter name is preceded by a # in the replacement text, the combination will be expanded into a quoted string with the parameter replaced by the actual argument.#include #define dprint(expr) printf(#expr " = %g\n", expr);int main() 阅读全文
posted @ 2013-08-06 09:20 Still_Raining 阅读(360) 评论(0) 推荐(0)