现在的位置: 首页 > 综合 > 正文

C语言笔记五则

2013年10月10日 ⁄ 综合 ⁄ 共 1633字 ⁄ 字号 评论关闭

 

晚上讨论班回来,发现一些问题以前不清楚。具体来说:




atexit

注册的多个函数会形成一个链表。当用户调用
exit
后,被注册过的函数按照注册的反向顺序依次执行。另外,被
atexit

注册的函数除了类型必须为
void (*)(void)
外,就是一个普通的函数,在其中可以调用其它函数,例如在其中可以调用
main
函数,这一点应该会很有用,下面是自己写的一个演示代码:

#include


<stdio.h
>

#include


<stdlib.h
>

 

int



done;

 

void


fn1
() {

   

printf



(

"process
exit, 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 in
main/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 /i
0x00401000

,要在汇编级单步
跟踪,使用命令 

disp

/i
$pc




http://www.trucy.org/blog/archives/eoiae/000087.html

有非常全非常
好的

gdb

使用讲解。

五:有一种奇妙的
printf

的用法:
printf

(“address:
%p/n”);

把这条语句作为函数的第一条语句,似乎是会打印出函数的返回地址。具体作用待调试。但用法非常巧妙和简洁。

【上篇】
【下篇】

抱歉!评论已关闭.