Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function with the same name as a macro

Tags:

c++

c

puzzle

#include<stdio.h>
void f(int a)
{
printf("%d", a);
}
#define f(a) {}

int main()
{
 /* call f : function */
}

How to call f (the function)? Writing f(3) doesn't work because it is replaced by {}

like image 695
Ferdy Avatar asked May 21 '11 12:05

Ferdy


People also ask

Is a macro the same as a function?

A macro is defined with the pre-processor directive. Macros are pre-processed which means that all the macros would be processed before your program compiles. However, functions are not preprocessed but compiled.

Can a function be a macro?

Macros are generally used to define constant values that are being used repeatedly in program. Macros can even accept arguments and such macros are known as function-like macros. It can be useful if tokens are concatenated into code to simplify some complex declarations.

What is the use of a macro instead of using a function?

Speed versus size The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called.

Is it better to use macro or function?

Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function.


2 Answers

Does (f)(3); work?

The C preprocessor doesn't expand the macro f inside ( ).


like image 132
Prasoon Saurav Avatar answered Oct 11 '22 18:10

Prasoon Saurav


int main()
{
#undef f  // clear f!
 f(3);
}
like image 30
iammilind Avatar answered Oct 11 '22 16:10

iammilind