By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,652 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

pls check it..(MACRO)

P: 43
# include <stdio.h>
# define MYMAC(MY) MY //Macro Function

void main()
{
char s[10]="3+2-(4*1)/5";
printf("Result : %f",MYMAC(s)); ??Macro Call
}



Guys i need a clarification abt why the above code is not working.. actually the work of macro is to paste its content at the desired place where we call them.. is it?
if so, when i call the
printf("Result : %f",MYMAC(s)); ??Macro Call
Macro, why didn't it is pasting the expression as,
printf("Result : %f",3+2-(4*1)/5); and work.

urgent ya.. please....
Oct 4 '06 #1
Share this Question
Share on Google+
5 Replies


100+
P: 144
There are two problems here.

1.) You haven't declard your character array large enough to hold all of the characters you are trying to initialize it with. Your string expression has 11 characters in it, but your character array only has room for 10. You also need to keep in mind that literal strings will have a null terminating byte (ie. '\0') appended to the end of the string. So, you need to provide storage for 12 characters.

2.) You are using "%f" in your printf statement which is used for printing a floating point value. You need to supply a "%s" instead to print a string.
Oct 4 '06 #2

Banfa
Expert Mod 5K+
P: 8,916

# define MYMAC(MY) MY //Macro Function

if so, when i call the
printf("Result : %f",MYMAC(s)); ??Macro Call

Macro, why didn't it is pasting the expression as,
printf("Result : %f",3+2-(4*1)/5); and work.
macros do a string substitution. When the preprocessor encounters them it replaces them with there definition.

At this line of code the preprocessor finds the macro MYMAC and performs it's string substitution.

printf("Result : %f",MYMAC(s));

You have defined the macro as

# define MYMAC(MY) MY //Macro Function

so

MYMAC(MY) --> MY

so

MYMAC(s) --> s

so the statement ends up as

printf("Result : %f",s);
Oct 4 '06 #3

P: 43
macros do a string substitution. When the preprocessor encounters them it replaces them with there definition.

At this line of code the preprocessor finds the macro MYMAC and performs it's string substitution.

printf("Result : %f",MYMAC(s));

You have defined the macro as

# define MYMAC(MY) MY //Macro Function

so

MYMAC(MY) --> MY

so

MYMAC(s) --> s

so the statement ends up as

printf("Result : %f",s);
thanks for sparing your time... can't i make that code to evaluate the expression.. sorry for not minding abt the size of array.. lets ignore the size.. i need the expression part.. let the size of array be 80.. pls find some solution for me guys.. HOW TO MAKE THE CODE WORK AS EXPRESSION EVALUATOR.. CAN I ??
Oct 4 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
HOW TO MAKE THE CODE WORK AS EXPRESSION EVALUATOR.. CAN I ??
Only by writing a function that will parse the string by looking at each character in turn and taking appropriate action.

You will not be able to write a macro that evaluates a string in this mannor.
Oct 4 '06 #5

P: 43
Only by writing a function that will parse the string by looking at each character in turn and taking appropriate action.

You will not be able to write a macro that evaluates a string in this mannor.
THANK YOU YAAR... THAKS A LOT
Oct 4 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.