fgetc和getc的区别 The function getchar is defined to be equivalent to getc(stdin). The difference betwee
2017-06-20 20:32
381 查看
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, whereasfgetc 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只能作为函数来调用。
一般来说,调用宏比调用函数耗费的时间少。
它们的原型:
int fgetc ( FILE * stream );
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的值会改变,这样的表达式是不应该在宏调用里出现的
#define MACRO_SQRT(x) (x)*(x)
int func_sqrt(int x)
{
return x * x;
}
上面是两个计算平方的方法,一个用宏,一个用函数
int i=2;
MACRO_SQRT(++i) 与 func_sqrt(++i)将是不同的~
int a,b;
/* 下面的代码是正常的 */
int i = 2;
a = func_sqrt(++i);
printf("a = %d, i = %d\n", a, i);
/* 下面的代码则是不正常的
*代码看起来只是让i自加一次,但由于宏的原因,实际可能不止,因而用宏一般不要使用有“副作用”的表达式
* 宏会把所有的x替换成x*x,如果放入++i,那么编译器预处理的时候就会将所有的x替换成++i,可想而知会有多少个
* ++i出现在代码里面呢?又会自加多少次呢?这显然和我们想要的不一样,看代码。
*/
i = 2;
b = MACRO_SQRT(++i);
printf("a = %d, i = %d\n", b, i);
相关文章推荐
- The function getUserId must be used with a prefix when a default namespace is not specified 解决办法
- Is it possible to get the difference from two dynamic arrays regardless of their order?
- The function getUserId must be used with a prefix when a default namespace is not specified 解决办法
- 关于The function get must be used with a prefix when a default namespace is not specified的错误
- The function getXX must be used with a prefix when a default namespace is not specified
- What is the C# equivalent to C++ OutputDebugString?
- php中each和print的区别(What is the difference between echo and print?)
- PHP Warning: strtotime() [function.strtotime]: It is not safe to rely on the system’s timezone settings. 解决方法:
- error 18483: could not connect to server 'dalian' because 'distributor_admin' is not defined as a remote login at the server.
- fix errors 1088: “Failed to execute request because the App-Domain could not be created. Error: 0x80070005 Access is denied” and 1334: “Exception: System.IO.FileLoadException”
- Function is used to Show/Hide the TaskBar
- Solution For "could not be located or a different control is assigned to the same ID after postback"
- WCF分布式开发常见错误(23):This could be due to the fact that the server certificate is not configured properly with HTTP.SYS
- This event supports the .NET Framework infrastructure and is not intended to be used directly from your code?继承自VScrollbar的自定义winform控件,某些事件不触发的问题(Winform控件开发学习)
- To Be Or Not To Be Is The Question
- 有趣的文章:Will Intel be forced to remove the "cripple AMD" function from their compiler?
- Attribute value request.getParameter("digyso") is quoted with " which must be escaped when used within the value
- error : A file found in a source-path must have an externally visible definition. If a definition in the file is meant to be ext
- The Behavior version of the Forest is 0 it must be 2 or greater to perform a domain rename: 该服务器不愿意处理该请求。 :8245
- To be, or not to be; that is the question! 生存还是毁灭,这是个值得思考的问题。