C语言笔记五则
来源:互联网 发布:苹果手机网络锁查询 编辑:程序博客网 时间:2024/05/29 06:51
晚上讨论班回来,发现一些问题以前不清楚。具体来说:
一:atexit注册的多个函数会形成一个链表。当用户调用exit后,被注册过的函数按照注册的反向顺序依次执行。另外,被atexit注册的函数除了类型必须为void (*)(void)外,就是一个普通的函数,在其中可以调用其它函数,例如在其中可以调用main函数,这一点应该会很有用,下面是自己写的一个演示代码:
#include <stdio.h>
#include <stdlib.h>
int done;
void fn1() {
printf("processexit, check whether things are done or not/n");
if(!done)
main();
}
void do_something(){
static int cnt = 0;
if(++cnt == 5) {
printf("do something successfully, exit normal/n");
done =1;
} else {
printf("There is some thing wrong/n");
}
exit(0);
}
int main() {
done = 0;
atexit(fn1);
printf("this is inmain/n");
do_something();
return 0;
}
二:shell启动一进程时,传给这个命令的参数是经过过滤的。即重定向符号这部分用户程序是没有机会处理的。例如自己写个命令行程序解析参数,假设程序名为cmdarg.exe,则如给它传这样的参数:cmdarg hello world redirection > out.txt,在cmdarg程序中是看不到重定向这些参数的。在bash的man手册页上写道:
REDIRECTION
一:atexit注册的多个函数会形成一个链表。当用户调用exit后,被注册过的函数按照注册的反向顺序依次执行。另外,被atexit注册的函数除了类型必须为void (*)(void)外,就是一个普通的函数,在其中可以调用其它函数,例如在其中可以调用main函数,这一点应该会很有用,下面是自己写的一个演示代码:
#include <stdio.h>
#include <stdlib.h>
int done;
void fn1() {
printf("processexit, check whether things are done or not/n");
if(!done)
main();
}
void do_something(){
static int cnt = 0;
if(++cnt == 5) {
printf("do something successfully, exit normal/n");
done =1;
} else {
printf("There is some thing wrong/n");
}
exit(0);
}
int main() {
done = 0;
atexit(fn1);
printf("this is inmain/n");
do_something();
return 0;
}
二:shell启动一进程时,传给这个命令的参数是经过过滤的。即重定向符号这部分用户程序是没有机会处理的。例如自己写个命令行程序解析参数,假设程序名为cmdarg.exe,则如给它传这样的参数:cmdarg hello world redirection > out.txt,在cmdarg程序中是看不到重定向这些参数的。在bash的man手册页上写道:
REDIRECTION
Before a command is executed, its input and output may be redirected
using a special notation interpreted by the shell. Redirection may
also be used to open and close files for the current shell execution
environment. The following redirection operators may precede or appear
anywhere within a simple command or may follow a command. Redirections
are processed in the order they appear, from left to right.
三:ungetc不能连续多次调用。两次ungetc调用之间必须至少有一次读操作或者文件指针移动操作(fseek,rewind等),ungetc只影响输入流,而不影响与输入流关联的外存文件。对于以二进制方式打开的流,ungetc会使读指针减1,如果当前位置为0,则减1后结果无法预测。紧跟在fscanf后面的ungetc也可能会失败,因为fscanf中就用到了ungetc。MSDN上有详细讲解。
四:gdb中,set-disassembly-flavor intel可以设置使用INTEL风格的汇编语法。查看内存使用x命令,如x /i0x00401000,要在汇编级单步跟踪,使用命令 disp /i $pc。
在http://www.trucy.org/blog/archives/eoiae/000087.html有非常全非常好的gdb使用讲解。
五:有一种奇妙的printf的用法:printf(“address:%p/n”);把这条语句作为函数的第一条语句,似乎是会打印出函数的返回地址。具体作用待调试。但用法非常巧妙和简洁。
三:ungetc不能连续多次调用。两次ungetc调用之间必须至少有一次读操作或者文件指针移动操作(fseek,rewind等),ungetc只影响输入流,而不影响与输入流关联的外存文件。对于以二进制方式打开的流,ungetc会使读指针减1,如果当前位置为0,则减1后结果无法预测。紧跟在fscanf后面的ungetc也可能会失败,因为fscanf中就用到了ungetc。MSDN上有详细讲解。
四:gdb中,set-disassembly-flavor intel可以设置使用INTEL风格的汇编语法。查看内存使用x命令,如x /i0x00401000,要在汇编级单步跟踪,使用命令 disp /i $pc。
在http://www.trucy.org/blog/archives/eoiae/000087.html有非常全非常好的gdb使用讲解。
五:有一种奇妙的printf的用法:printf(“address:%p/n”);把这条语句作为函数的第一条语句,似乎是会打印出函数的返回地址。具体作用待调试。但用法非常巧妙和简洁。
- C语言笔记五则
- c语言学习笔记五
- C语言笔记(五)
- C语言笔记(五)
- 《C语言宝典》学习笔记(五):变量
- C语言学习笔记(五)指针
- C语言程序学习(五)笔记
- C语言学习笔记(五)
- C语言学习笔记(五)--顺序程序设计
- 黑马程序员-C语言笔记(五)内存
- C语言学习笔记(五) -系统函数 System Function
- 黑马程序员---C语言学习笔记之五
- 零基础学C语言 笔记五 算术表达式
- C语言笔记系列(五)--字符输入输出和字符函数
- iosiOS开发学习笔记-C语言(五)
- C语言学习笔记(五)——复合类型
- C语言学习笔记(五)----高精度的加减法
- C语言(五)
- 精通一门语言的标准
- 大型互联网网站架构心得之一:分
- 大型网站架构演变和知识体系
- 10个让人受益的管理原理
- 别让你的简历15秒后被扔掉的秘籍
- C语言笔记五则
- 标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast(转载)
- __declspec,__cdecl,__stdcall都是什么意思?
- 什么是.net的接口和抽象类
- window下查看端口命令
- avi文件格式
- 关于线程同步 PulseEvent()
- 专题设计
- 跟踪和恢复大文件下载