1.

What makes a macro faster than a function in the C programming language?

Answer»

Macros are sections of code in a programme that have been given a name. The compiler substitutes this name with the real piece of code whenever it encounters this name. To define a macro, use the '#define' directive. Macros can be defined either before or within the main method. 

Code 1:

//C#include<stdio.h> #include<conio.h> #define HRS 24 //Macro int main() { printf("%d", HRS); return 0; }

Output:

24

Explanation:

In the above code, we defined a macro NAMED HRS to have a VALUE of 24. Now, when we call the macro in the printf function, 24 GETS printed in the output terminal.

The above code can be INFERRED to be the following code without the use of macros.

Code 2:

//C#include<stdio.h> int hrs() { return 24; } int main() { printf("%d", hrs()); //calling return 0; }

Output :

 24

Explanation:

Here, we defined a function named HRS. When we call the HRS function in the printf function, the function returns 24 and so 24 is printed on the output terminal.

Macros are pre-processed, which implies that all macros are processed before the code is compiled, and functions are processed after the code is compiled.



Discussion

No Comment Found