Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>What it does</h3> <p>If you read Plauger's <a href="https://smile.amazon.com/dp/0131315099" rel="nofollow noreferrer">The Standard C Library</a> (1992), you will see that the <code>&lt;stdio.h&gt;</code> header is allowed to provide <code>getchar()</code> and <code>getc()</code> as function-like macros (with special permission for <code>getc()</code> to evaluate its file pointer argument more than once!). However, even if it provides macros, the implementation is also obliged to provid actual functions that do the same job, primarily so that you can access a function pointer called <code>getchar()</code> or <code>getc()</code> and pass that to other functions.</p> <p>That is, by doing:</p> <pre><code>#include &lt;stdio.h&gt; #undef getchar extern int some_function(int (*)(void)); int core_function(void) { int c = some_function(getchar); return(c); } </code></pre> <p>As written, the <code>core_function()</code> is pretty meaningless, but it illustrates the point. You can do the same thing with the <code>isxxxx()</code> macros in <code>&lt;ctype.h&gt;</code> too, for example.</p> <p>Normally, you don't want to do that - you don't normally want to remove the macro definition. But, when you need the real function, you can get hold of it. People who provide libraries can emulate the functionality of the standard C library to good effect.</p> <h3>Seldom needed</h3> <p>Also note that one of the reasons you seldom need to use the explicit <code>#undef</code> is because you can invoke the function instead of the macro by writing:</p> <pre><code>int c = (getchar)(); </code></pre> <p>Because the token after <code>getchar</code> is not an <code>(</code>, it is not an invocation of the function-like macro, so it must be a reference to the function. Similarly, the first example above, would compile and run correctly even without the <code>#undef</code>.</p> <p>If you implement your own function with a macro override, you can use this to good effect, though it might be slightly confusing unless explained.</p> <pre><code>/* function.h */ … extern int function(int c); extern int other_function(int c, FILE *fp); #define function(c) other_function(c, stdout); … </code></pre> <pre><code>/* function.c */ … /* Provide function despite macro override */ int (function)(int c) { return function(c, stdout); } </code></pre> <p>The function definition line doesn't invoke the macro because the token after <code>function</code> is not <code>(</code>. The <code>return</code> line does invoke the macro.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload