How to remove syntex error from the followin code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AHSANKATHIA
    New Member
    • Nov 2010
    • 7

    How to remove syntex error from the followin code

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    #define pr (int n) printf("%d",n);
    
    void main(void)
    {
    
    int f;
    
    scanf("%d",&f);
    pr(f);
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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:
    Code:
    #define pr(n) printf("%d",n);
    void main(void)
    {
      int f;
      scanf("%d",&f);
      pr(f);
    }
    Which after macro substitution turns into this:
    Code:
    void main(void)
    {
      int f;
      scanf("%d",&f);
      printf("%d",f);;
    }
    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:
    Code:
    #define pr(n) printf("%d",n);
    ...
    if (flag)
       pr(f);
    else
       pr(g);
    After macro substitution you get:
    Code:
    if (flag)
       printf("%d",f);;
    else
       printf("%d",g);;
    which is logically equivalent to
    Code:
    if (flag)
       printf("%d",f);
    ;
    else
    printf("%d",g);
    ;
    It is considered bad form to include a command-terminating semicolon in a macro definition.

    Comment

    • AHSANKATHIA
      New Member
      • Nov 2010
      • 7

      #3
      The error i get is "expression syntex error" when i call the macro in main function

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        remove the space between pr and ( in the #define

        Comment

        • AHSANKATHIA
          New Member
          • Nov 2010
          • 7

          #5
          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.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

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

            Comment

            Working...