What is the difference between fgetc, FPUTC and Getc and PUTC in C language?

Source: Internet
Author: User

Read the book, found the four functions, want to know their differences. Results Online Search found that many people say that fgetc, FPUTC F represents the file, that is, these two functions are related to files! But a look at their function declarations, such as:

Found their parameters inside the file pointer Ah! Later went to turn over the apue, found that the F represents the actually function, this is how one thing, and listen to my slow way!

Fgetc and getc Their differences are not in their use, but in their realization! Specifically, the implementation of the F (fgetc, FPUTC) is achieved through functions, without F (PUTC, getc), when implemented by macro definition to achieve! For their differences, take getc and Fgetc, Apue (p115) wrote:

The <1>.GETC parameter should not be an expression with side-effects.

<2> Because fgetc must be a function, you can get its address. This allows the address of the fgetc to be passed as one parameter to another function.

<3>: The time required to call fgetc is likely to be longer than calling Getc, because calling a function typically takes longer than calling a macro.

First of all, how to understand the 1th, this is because the macro is a lot of things, here to give a classic example.

In this program, Z2 I want to calculate 8*8, but the parameters passed is 5+3,6+2, the macro is replaced by the 5+3*6+2, the final result is 25.

So what does the 2nd mean, this is actually a function pointer, the address of a function can be passed to a function pointer, let it call, but the macro is not! For example, the following code:

Because Max is a macro, you can't assign a pointer to a function!

On the 3rd, this involves a bit of compilation of knowledge, in assembly language, the function of the call is through the stack to achieve! is called when the stack, the call finished reload stack (about the assembly I am not very clear, so the narrative is a bit rough, but also hope forgive me!) ), such as 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 debug with GDB, set a breakpoint on line 4th and look at its function call stack as follows:

And through the macro call is not to press the stack, so getc faster than fgetc a little faster.

However, there is a little exception to this, which is that sometimes the calling function is not implemented by a stack, so the author uses a very likely vocabulary. On this issue, this post is very good (fgetc and getc, which fast?) )。

OK, the above is the difference between fgetc and getc! However, it is important to add that the two functions are the same in GNU C, they are all implemented by functions, and then getc simply and rudely add a macro declaration. Specific as follows:

In stdio.h this file, the Getc declaration is this:

And in the Libio.h the _io_getc (_FP) is the declaration of this function:

So, finally, dear friends, I want to say is, do not tangle, these two functions open use it, the difference is not very big, generally will not have what problem!

What is the difference between fgetc, FPUTC and Getc and PUTC in C language?

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.