for the codes under for loop this error massage is showing
19:37 C:\Users\azr\Desktop\Untitled1.c invalid suffix "xy" on integer constant

``````#include<stdio.h>
#include<conio.h>
#define func(x,y) 3xy

int main()
{
int x0,y0,n,y1,k1,k2,k3,k4,h;
printf("enter the values");
printf("enter x0:");
scanf("enter x0:%d",&x0);
printf("enter number of x values:");
scanf("no. of x values:%d",&n);
printf("value of hight h");
scanf("value of hight h is :%d",&h);
printf("the value of y0");
scanf("the value of y0 is :%d",&y0);

for(;x0<n;x0=x0+h){
k1=func(x0,y0);
k2=func(x0+.5*h,y0+.5*h*k1);
k3=func(x0+.5*h,y0+.5*h*k2);
k4=func(x0+h,y0+h*k3);
y1=h+(1/6)*(k1+2*k2+2*k3+k4);
printf("y1=%d",y1);
y0=y1;
}
getch();
}
``````

thanks

`#define func(x,y) 3xy` <-- This dosent makes sense. 3xy should be an integer, yours is like a string.

Refer to MSDN and

What about `#define func(x,y) 3 * x * y`?

`What about #define func(x,y) 3 * x * y?` That is valid.

It's valid, but it very likely won't do what the OP wants it to. You'll need parentheses around the arguments and the whole expression (though the latter doesn't matter in this case).

Or they could make their life 1000 times easier and define it as a function instead of a …

## All 13 Replies

`#define func(x,y) 3xy` <-- This dosent makes sense. 3xy should be an integer, yours is like a string.

Refer to MSDN and Cprogramming.com for proper usage of preprocessors.

What about `#define func(x,y) 3 * x * y`?

`What about #define func(x,y) 3 * x * y?` That is valid.

It's valid, but it very likely won't do what the OP wants it to. You'll need parentheses around the arguments and the whole expression (though the latter doesn't matter in this case).

Or they could make their life 1000 times easier and define it as a function instead of a macro.

thanks a lot. it worked!
@sepp2k i cant understand your suggestion.can you make it clear for me please??

You mean about adding parentheses or using a function? For the former I meant that it should be `#define func(x,y) (3 * (x) * (y))` or you'll get wrong results because of precedence in the expanded code.

For the latter I meant that you should just use `int func(int x, int y) { return 3 * x * y; }` and avoid any such issues.

should i have to mention the type such as int,char etc while defining a function
like you have written

``````int func(int x, int y) { return 3 * x * y; }
``````

and why it is necessary to declare the type and as well as the return statement??

Yes, according to ANSI C, you need to specify the return and argument types when defining or declaring a function. Not doing so is an error.

You need the return statement because the return statement is used to return values from a function. If you use the return value of a function, but that function did not return a value using a return statement, the behavior is undefined (even in pre-ANSI C).

It might seem to you that using a macro (which doesn't require any types or return statements) might be easier because of this, but as we just saw, using macros means that you have to be very careful with parentheses if you want to get correct results. So not using macros will save you a lot of pain and is definitely worth it.

thanks a lot!!

It's not called "Macro Hell" for no reason. ;)

so you are suggesting to creat a user defined function and call it to get the value for k1,k2,k3 ??

It depends, but I have had some defines written by others, that just brought the work to a screeching halt, while we figured out WTH these defines were doing.

clever cute defines - I hate them, and pray to the gods to strike them down to hell! ;)

seems you had a lot of troubles with macro defines when you have learnt them ;). i will try it with user defined function .hope to post the codes here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.