Are getchar() and putchar() functions or macros?

不羁岁月 提交于 2020-01-05 00:49:40

问题


I referred to two reliable sources for the information and both seems to have different definitions of the same thing:

http://www.cplusplus.com/reference/clibr%E2%80%A6

http://www.ocf.berkeley.edu/~pad/tigcc/doc/html/stdio_fputchar.html

The first source says putchar() is a function, as is getchar(), but in the second link it says putchar() is a macro. My book says getchar() is a macro. Which is correct?


回答1:


getchar and putchar are functions, but may additionally be defined as macros. Whether they are or not depends on the implementation. The C standard says regarding standard library functions (C99, 7.1.4@1):

Any function declared in a header may be additionally implemented as a function-like macro defined in the header.




回答2:


For information, on my mac 'man getchar' gives this:

 The fgetc() function obtains the next input character (if present) from the stream pointed at by stream, or the
 next character pushed back on the stream via ungetc(3).

 The getc() function acts essentially identically to fgetc(), but is a macro that expands in-line.

 The getchar() function is equivalent to getc(stdin).



回答3:


It's entirely dependent how it's implemented in your compiler.



来源:https://stackoverflow.com/questions/16165672/are-getchar-and-putchar-functions-or-macros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!