0

What are macros?please explain in simple words with example

I am new to C programming

I have seen different web pages and SO pages and what i am able to grasp is that although macros and function are not same exactly but there appears to be some sort of simiarity between both?If yes,my perception is correct,what are those similarities?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
LECS
  • 121
  • 12
  • This is not a tutorial service, thanks. – Sourav Ghosh Jul 30 '20 at 13:54
  • Please see the [ask] help page and [The perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) blog post by Jon Skeet. – Sourav Ghosh Jul 30 '20 at 13:55
  • 1
    A macro is simply a text substitution - if you create a macro like `#define CUBE(x) ((x)*(x)*(x))`, then all instances of `CUBE(10)` in your source code will be replaced with the text `((10)*(10)*(10))` before being passed to the compiler, `CUBE(i+1)` will be replaced with `((i+1)*(i+1)*(i+1))`, etc. Macro arguments are not *evaluated*, they are simply expanded in place. This can cause problems when an argument has side effects - `CUBE(i++)` expands to `((i++)*(i++)*(i++))`, which won't work as expected. – John Bode Jul 30 '20 at 14:04

0 Answers0