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

How to remove syntex error from the followin code

P: 7
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. #define pr (int n) printf("%d",n);
  5.  
  6. void main(void)
  7. {
  8.  
  9. int f;
  10.  
  11. scanf("%d",&f);
  12. pr(f);
  13. }
  14.  
Nov 25 '10 #1

✓ answered by Banfa

remove the int as well you can't type function like macro parameters

Share this Question
Share on Google+
5 Replies


Expert 100+
P: 2,398
It helps if you tell us what syntax error you're getting.

You define macro pr as not having any parameters, but then you try to pass it a parameter when you call it.
  • There can't be any spaces between the macro name and the open-parenthesis in the macro definition if you wish to define a macro that takes arguments.
  • A macro is not a function, you don't specify the types of the macro arguments in the macro definition.

If we make those corrections to your code we get this:
Expand|Select|Wrap|Line Numbers
  1. #define pr(n) printf("%d",n);
  2. void main(void)
  3. {
  4.   int f;
  5.   scanf("%d",&f);
  6.   pr(f);
  7. }
Which after macro substitution turns into this:
Expand|Select|Wrap|Line Numbers
  1. void main(void)
  2. {
  3.   int f;
  4.   scanf("%d",&f);
  5.   printf("%d",f);;
  6. }
Note the two semicolons on line 5. The first comes from the macro expansion, the second comes from your original source.

Consider what would happen here:
Expand|Select|Wrap|Line Numbers
  1. #define pr(n) printf("%d",n);
  2. ...
  3. if (flag)
  4.    pr(f);
  5. else
  6.    pr(g);
After macro substitution you get:
Expand|Select|Wrap|Line Numbers
  1. if (flag)
  2.    printf("%d",f);;
  3. else
  4.    printf("%d",g);;
which is logically equivalent to
Expand|Select|Wrap|Line Numbers
  1. if (flag)
  2.    printf("%d",f);
  3. ;
  4. else
  5. printf("%d",g);
  6. ;
It is considered bad form to include a command-terminating semicolon in a macro definition.
Nov 26 '10 #2

P: 7
The error i get is "expression syntex error" when i call the macro in main function
Nov 26 '10 #3

Banfa
Expert Mod 5K+
P: 8,916
remove the space between pr and ( in the #define
Nov 26 '10 #4

P: 7
I tried removing space between pr and ( in #define but it gives another error. When i call pr in main function then it gives an error that pr must have prototype.
Nov 26 '10 #5

Banfa
Expert Mod 5K+
P: 8,916
remove the int as well you can't type function like macro parameters
Nov 26 '10 #6

Post your reply

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