What is the difference between fgetc, fputc, getc, and putc in C language? getcputc
When I was reading a book, I found these four functions and wanted to know their differences. Results online query found that many people said that fgetc, fputc f Represents file, which is the two functions related to the file! But their function declaration, such:
They found that all their parameters contain file pointers! Later, I flipped through APUE and found that f actually represents a function. What's the matter with that? Let me hear it slowly!
The difference between fgetc and getc is not in their use, but in their implementation! Specifically, f (fgetc, fputc) is implemented through functions without f (putc, getc, the implementation is implemented through macro definition! For their differences, take getc and fgetc for example. This is what is written on APUE (p115:
<1>. getc parameters should not be expressions with side effects.
<2>. Because fgetc must be a function, you can obtain its address. This allows the fgetc address to be transmitted to another function as a parameter.
<3>. It may take longer to call fgetc, because it usually takes longer to call a function than to call a macro.
First of all, how should we understand the first point? This is because there are a lot of macros. Here is a classic example.
In this program, z2 is 8x8, but the input parameter is 5 + 3, 6 + 2. After the macro is replaced, it becomes 5 + 3*6 + 2, the final result is 25.
So what does the second point mean? This is actually a function pointer. The address of a function can be passed to a function pointer for calling, but the macro won't work! For example, the following code:
MAX is a macro, so it cannot be assigned to function pointers!
With regard to the third point, this involves a little compilation knowledge. In assembly languages, function calls are implemented through stacks! That is, the stack is pressed during the call, and the stack is re-played after the call (I am not very clear about the assembly, so the description is a bit rough and I hope you will forgive me !), For example, the following code:
2 void func3()
6 void func2()
10 void func1()
14 int main(int argc,char *argv)
17 return 0;
I used gdb for debugging and set a breakpoint in row 4th. view its function call stack as follows:
Macro calls do not require stack pressure, so getc is faster than fgetc.
However, this is an exception, that is, sometimes calling functions is not implemented through stacks, so the author uses a very likely word. This post is very helpful (fgetc and getc ?).
OK. The above is the difference between fgetc and getc! However, we need to add that the two functions under gnu c are actually the same. They are all implemented through functions, and then getc simply and roughly adds a macro declaration. The details are as follows:
In the stdio. h file, the getc statement is as follows:
The Declaration of the _ IO_getc (_ fp) function in libio. h is as follows:
So, in the end, you guys, what I want to say is, don't worry about it any more. The two functions are open for use. The difference is not very big. Generally, there won't be any problems!