NO

Author Topic: Help with macros  (Read 1289 times)

Yadav

  • Guest
Help with macros
« on: June 15, 2013, 02:46:31 pm »
Hi guys,
             I was working on macros and I am curious abt the result of the sample program below. The result (Product) of the program is 7, I was expecting 16. If I change the code to Product(x) then it wrks fine. I wanted to know wt happens if the argument to the macros is Product(x+1). Please help!!!!!!


#include<stdio.h>

#define PRODUCT(x) (x*x)

int main(void)
{
   int i=6,j;

   j=PRODUCT(i+1);    //Dono how it is processed???
   printf("Product=%d\n",j);

   return 0;
}

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1686
Re: Help with macros
« Reply #1 on: June 15, 2013, 06:23:44 pm »
For i=3 (not 6 as you wrote) you expected 16 as result, but got 7 because there are no parenthesis in the macro.
Macro substitution is a 'literal' substitution so X*X when x=i+1 gives i+1*i+1=3+1*3+1=7  ;)
If you write your macro as:
Code: [Select]
#define PRODUCT(x) ((x)*(x))then you will get X*X => (i+1)*(i+1)=(3+1)*(3+1)=4*4=16....

Yadav

  • Guest
Re: Help with macros
« Reply #2 on: June 15, 2013, 09:47:28 pm »
Ya got it....Rookie mistake:-[ .


Offline bitcoin

  • Member
  • *
  • Posts: 18
Re: Help with macros
« Reply #3 on: January 27, 2019, 11:01:46 pm »
Can you help me to write macros to encrypt strings?
#define CRYPT something_do_with_str

such as char s[12] = CRYPT("secret");

any simple XOR , if it possible.