What is the difference between fgetc, fputc, getc, and putc in C language? getcputc

Source: Internet
Author: User

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:

 1 #include<stdio.h>
 2 void func3()
 3 {
 4     printf("Hello,World!");
 5 }
 6 void func2()
 7 {
 8     func3();
 9 }
10 void func1()
11 {
12     func2();
13 }
14 int main(int argc,char *argv[])
15 {
16     func1();
17     return 0;
18 }

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.