Q: lvalue required as left operand of assignment for a macro with arguments I am trying to print a string 'x' numbers of times using macro as arguments with the following code:-
 1 #include<string.h>
 2 #include<stdio.h>
 3 #define print(x,c)      while(x>0)\
 4 {\
 5         puts(c);\
 6         printf("\n");\
 7         --x;\
 8 }
 9 
 10 int main()
 11 {
 12         char c[20];
 13         strcpy(c,"Hallelujah");
 14         print(5,c);
 15 }

But while compiling, I get the following error:-
 macro2.c: In function 'main':
 macro2.c:7:2: error: lvalue required as decrement operand 
   --x;\
   ^
 macro2.c:14:2: note: in expansion of macro 'print'
   print(5,c);
   ^

I am unable to figure out the problem, kindly help thank you.

A: After marco expansion, this statement
print(5,c);

becomes
while(5>0) { puts(c); printf("\n"); --5; };

As you can see, you can't decrement a literal value (--5). You need a variable (a modifible lvalue) to be able to achieve that. Your marco looks superfluous. You can simply use a loop:
int x = 5;

while(x > 0) { 
   puts(c); 
   printf("\n"); 
   --x; 
}

If you really want to use a macro, then you could do:
 #define print(x,c)  do { \
 int t = x; \
 while(t>0) {\
         puts(c);\
         printf("\n");\
         --t;\
 } \
} while(0)


A: Macros are expanded by the preprocessor and the result is handed over to the compiler.
Your code will be expanded to:
 int main()
 {
      char c[20];
      strcpy(c,"Hallelujah");
      while(5>0)
      {
         puts(c);
         printf("\n");
         --5;
      }
  }

As you can see, the x is replaced with the actual argument expression in each instance. It does not work like in a regular function where the argument variable is a local copy of the expression value.
I suggest that you either convert the macro to a C function or that you declare a variable to hold the value before calling the macro.
The second option would look like this:
int n = 5;
print(n,c);

