Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function-like macro with the same name as an object-like macro

Is it possible to have in C something like:

#define MACRO_EX 333

#define MACRO_EX(X,Y) ((X) < (Y) ? : (X) : (Y))

Can they coexist?

like image 246
coredump Avatar asked Sep 25 '12 09:09

coredump


People also ask

What is object-like macro and function-like macro?

3.1 Object-like Macros An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants. foo = (char *) malloc (1024);

What are function-like macros?

Function-like macro definition: An identifier followed by a parameter list in parentheses and the replacement tokens. The parameters are imbedded in the replacement code. White space cannot separate the identifier (which is the name of the macro) and the left parenthesis of the parameter list.

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.

What is macro and its object?

A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).

What are the two types of macro?

There are two types of macros: Object-like Macros. Function-like Macros.

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.


1 Answers

The C standard says (ISO/IEC 9899:1999, §6.10.3, 2):

An identifier currently defined as an object-like macro shall not be redefined by another #define preprocessing directive unless the second definition is an object-like macro definition and the two replacement lists are identical. Likewise, an identifier currently defined as a function-like macro shall not be redefined by another #define preprocessing directive unless the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical.

So the answer is no.

like image 183
undur_gongor Avatar answered Sep 21 '22 05:09

undur_gongor