How are getchar() and putchar() Macros?

丶灬走出姿态 提交于 2019-12-26 09:47:33

问题


From what I understand about macros in C, they are predefined constants that will be used throughout the program with their constant value, so we go ahead and define them to avoid further complications and make the code more readable, so people reading it will understand what is supposed to stay constant and what isn't.

I have read here and there (C programming A Modern Approach, K.N King) that we can define these two functions as macro.

Since I'm somewhat new to C, I can't wrap my head around how can these two be defined as macro?


回答1:


There are two types of macros: simple substitution macros and function-like macros.

Substitution macros replace one instance of a symbol with another. For example:

#define LEN 10
char str[LEN];

After preprocessing, this becomes:

char str[10];

A function-like macro can take parameters that can be plugged in to whatever gets substituted:

#define MAX(a,b) ((a) > (b) ? (a) : (b))
int x = MAX(2,3);

After preprocessing:

int x = ((2) > (3) ? (2) : (3));

In the case of getchar and putchar, they can be defined as follows:

#define getchar() getc(stdin)
#define putchar(c) putc(c, stdout)



回答2:


There are basically three types of preprocessor macros:

  1. Simple defined without any value. For example

    #define THIS_IS_A_MACRO
    

    This kind of macros are used for conditional compilation.

  2. Symbolic constants. For example

    #define SOME_SYMBOLIC_CONSTANT  123
    

    These kind of macros are what you're thinking of.

  3. Function-like macros. Foe example

    #define SOME_MACRO(a_macro_argument)  printf("Macro invoked with argument %d\n", a_macro_argument)
    

    This kind of macro is used very much like functions, but are replaced by the preprocessor in the source code before the compiler parser sees the code, with the macro arguments replaced with their actual values.

Lets take the function-like macro and how it will be expanded by the preprocessor:

SOME_MACRO(123);

The above will be replaced like

printf("Macro invoked with argument %d\n", 123);



回答3:


Fully depends on implementation. They can be function also.

Standards don't demand anything explicit about the type of implementation. But you can check here it points Any function declared in a header may be additionally implemented.... as pointed by Eugene.Sh

To say it more clearly, there may be a function in the library or it can be a macro also (for getchar). Classically, the macro for getchar() would be #define getchar() getc(stdin), and getc() might also be a macro.

Standard says that The getc function is equivalent to fgetc, except that if it is implemented as a macro, it may evaluate stream more than once, so the argument should never be an expression with side effects.

Now it boilds down to fgetc in which case we know that it is guaranteed to be a function. Thread safety makes it more likely to be a function.



来源:https://stackoverflow.com/questions/45760438/how-are-getchar-and-putchar-macros

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