fgetc和getc的区别

The function getchar is defined to be equivalent to getc(stdin). The difference between the first two functions is that getc can be implemented as a macro, whereas fgetc cannot be implemented as a macro. This means three things.

 

1.The argument to getc should not be an expression with side effects.

 

2.Since fgetc is guaranteed to be a function, we can take its address. This allows us to pass the address of fgetc as an argument to another function.

 

3.Calls to fgetc probably take longer than calls to getc, as it usually takes more time to call a function.


两个都是用来从stream中取得一个字符的,区别在于调用getc函数时所用的参数stream不能是有副作用的表达式(稍后解释),而fgetc函数则可以,也就是说,getc可以被当作宏来调用,而fgetc只能作为函数来调用。 
一般来说,调用宏比调用函数耗费的时间少。


它们的原型:

Code:
  1. int fgetc ( FILE * stream );       
  2. int getc(FILE * stream);    


所以fgetc是一个函数 
getc是一个宏 
一般来说宏产生较大的代码,但是避免了函数调用的堆栈操作,所以速度会比较快。

相关资料也说明了,有例外的情况: 
1. 很多RISC处理器(如SUN SPARC)使用寄存器窗口重叠技术, 
http://server.chinabyte.com/404/157904_1.shtml), 
在寄存器充足的情况下,根本不需要堆栈操作,fgetc函数比getc宏更快 
2. 在多线程的情况下,为了线程同步,getc和fgetc可能是相同的。

有副作用的表达式,指的是表达式执行后,会改变表达式中某些变量的值 
最简单的如++i,这个表达式执行后,i的值会改变,这样的表达式是不应该在宏调用里出现的

Code:
  1. #define MACRO_SQRT(x) (x)*(x)       
  2. int func_sqrt(int x)       
  3. {       
  4.              return x * x;       
  5. }     



上面是两个计算平方的方法,一个用宏,一个用函数 
int i=2; 
MACRO_SQRT(++i) 与 func_sqrt(++i)将是不同的~ 

Code:
  1. int a,b;      
  2.      
  3. /* 下面的代码是正常的 */      
  4. int i = 2;      
  5. a = func_sqrt(++i);      
  6. printf("a = %d, i = %d\n", a, i);       
  7.      
  8.      
  9. /* 下面的代码则是不正常的     
  10.  *代码看起来只是让i自加一次,但由于宏的原因,实际可能不止,因而用宏一般不要使用有“副作用”的表达式      
  11.  * 宏会把所有的x替换成x*x,如果放入++i,那么编译器预处理的时候就会将所有的x替换成++i,可想而知会有多少个      
  12.  * ++i出现在代码里面呢?又会自加多少次呢?这显然和我们想要的不一样,看代码。      
  13.  */      
  14. i = 2;      
  15. b = MACRO_SQRT(++i);       
  16. printf("a = %d, i = %d\n", b, i);    



 
 总结:fgetc和getc最大的区别在前者是函数,后者是宏,getc由fgetc通过宏实现,调用的时候注意参数stream不能是有副作用的表达式

posted on 2011-05-20 15:41  Hi小新  阅读(10164)  评论(0编辑  收藏  举报

导航