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程序中是看不到重定向这些参数的。在bashman手册页上写道:
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中就用到了ungetcMSDN上有详细讲解。
四: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”);把这条语句作为函数的第一条语句,似乎是会打印出函数的返回地址。具体作用待调试。但用法非常巧妙和简洁。
原创粉丝点击