What is the difference between fputc and putc




















The fputc and putw subroutines serve similar purposes but are true subroutines. The putc macro writes the character Character converted to an unsigned char data type to the output specified by the Stream parameter.

The character is written at the position at which the file pointer is currently pointing, if defined. The putchar macro is the same as the putc macro except that putchar writes to the standard output. The fputc subroutine works the same as the putc macro, but fputc is a true subroutine rather than a macro.

It runs more slowly than putc, but takes less space per invocation. For such cases, use the fputc subroutine instead. Also, use fputc whenever you need to pass a pointer to this subroutine as a parameter to another subroutine. NET Framework. How hard would it really be to port to Windows 8? All times are GMT The time now is PM. Copyright TechnologyAdvice. CodeGuru Home. Visual Basic. VB Forums. If this is your first visit, be sure to check out the FAQ by clicking the link above.

Consider this toy example:. Most of the time, this will function as expected, but if you pass in an expression such as f , then the side effect of calling the function f will happen twice , not once , since the preprocessor is just a text transformer which is ignorant of C:.

To put this in perspective, the putc function, if implemented as a macro, may evaluate its stream argument more than once. So, if that stream comes from a function:. The solution, of course, is to just call a regular function like fputc instead of putc. Since it's not a macro, it doesn't have any potential problems with evaluating its arguments more than once.

Macros can be dangerous sometimes, so use them with care. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 8 years, 10 months ago. Active 1 year, 7 months ago. Viewed 12k times. As mentioned here putc needs stdout, vs puts : According to Kernighan's book putc is equivalent to fputc but putc could be implemented as a macro and putc may evaluate its stream argument more than once.

Questions: putc can be implemented as a macro but what is the problem doing same with fputc? Connect and share knowledge within a single location that is structured and easy to search. I heard that both the functions do pretty much the same thing except getc can be used as macros. What is meant by that? First of all, printf vs fprintf is different: fprintf supports a file argument, printf goes to stdout. The reason for fputc and fgetc is that putc and getc were, on historical implementations, macro versions that evaluated the argument more than once.

For example, the version on Unix V7 :. The unlocked versions are used for performance inside the library functions, so the lock is acquired only once at the top of the library function so that when you call printf, it doesn't have to call fputc hundreds of times, doing the lock every time.

According to Kernighan's book putc is equivalent to fputc but putc could be implemented as a macro and putc may evaluate its stream argument more than once. The difference between putc and fputc is that by using putc, you risk running the macro version which is inherently unsafe because it may have to evaluate its stream argument more than once.

This causes complications that most people aren't aware of and thus do not watch out for, so fputc is better to use. Stack Overflow for Teams — Collaborate and share knowledge with a private group.



0コメント

  • 1000 / 1000