top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

What is the difference between marcos and functions in C?

+1 vote
What is the difference between marcos and functions in C?
posted Jul 5, 2017 by Ajay Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
Best answer

The basic difference is that functions are compiled whereas macros are preprocessed. When you use a function call it will be translated into ASM CALL with all these stack operations to pass parameters and return values. When you use a MACRO, C preprocessor will translate all strings using macro, do the necessary replacement and than compile.

See the following example

#define square(x) x*x

int square1(int x) 
  return x*x;  

   printf("Macro output - %d\n", square(2+2));
   printf("Function output - %d\n", square1(2+2)); 

Macro output - 8
Function output - 16

Now in case 1 square(2+2) is replaced with 2+2*2+2 and then code is compiled which gives output 8 and in case 2 square1(2+2) 2+2 is calculated at the compile time and passed to the function...

answer Jul 5, 2017 by Salil Agrawal
0 votes

Macros are pre processed where as functions are compiled.. take a look at this

answer Aug 3, 2017 by Sathish K
Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.