Thread: Macros problem

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    13

    Question Macros problem

    I was creating a program here

    Code:
    #include<stdio.h>#include<conio.h>
    #include"lib.h"
    
    
    void main()
     {
      clrscr();
    
    
      gotoxy(0,0);
      HLINE(12);
      gotoxy(1,1);
      HLINE(14);
      getch();
     }
    and
    Code:
    #define HLINE(val) (for(i=0;i<=val;i++)\
    		   printf("%c",179);)
    
    it shows me 7 errors

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Did you see today's other question on macros?
    Help with macros
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please post those 7 errors. Copy-paste them exactly from your compiler, otherwise we can't help you.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    13
    @Salemyes i read the post but it is no way connected to my bug

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    13
    Quote Originally Posted by anduril462 View Post
    Please post those 7 errors. Copy-paste them exactly from your compiler, otherwise we can't help you.
    The errors are expression syntax error at each both the occerence of HLINE, and missing statement ;

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    13
    And also undefined symbol i

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    So why are you not using the do .. while ( 0 ) trick?

    Why are you using ( ) where you should be using braces ?

    Where is i declared?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by slain View Post
    @Salemyes i read the post but it is no way connected to my bug
    Actually, there's a really, really good chance it is very strongly connected to your bug. Salem is a smart guy and knows his stuff, he wouldn't suggest that post if he didn't think it was related. You should at least try out the suggestions from the other thread before dismissing it. Ask for clarification if you don't understand how to apply that suggestion to your code.
    Quote Originally Posted by slain View Post
    The errors are expression syntax error at each both the occerence of HLINE, and missing statement ;
    That sounds like two errors per occurrence of HLINE, and with two occurrences, that's a total of 4 errors. What about the other 3 errors? What are they?
    EDIT: Just saw your post about undeclared 'i'. That makes 5 of 7, still missing 2 errors.

    Seriously though, what is so difficult about the following request:
    Quote Originally Posted by anduril462 View Post
    Please post those 7 errors. Copy-paste them exactly from your compiler, otherwise we can't help you.
    It's very difficult for us to help you if you give faulty/incorrect/incomplete information. Furthermore, people won't want to help you if you ignore the advice or suggestions they give.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It's probably because you are adding a ';' at the end of HLINE() in your code

    Change your macro to what Salem suggested to fix this problem - It will allow you to use local variables as well

    Create a macro from the following. Notice that the last ';' after the "while" is omitted, so that it can be added in the actual code.
    Code:
    do
    {
      int i;
      for(...)
      {
        printf(...);
      }
    }
    while(0)
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    13
    @Salem Thank you for your concern and taking time to reply me, if i was rude in any way i am sorry for it.

    @anduril462 I understood the mistake i was making over there. And your post was helpful.

    and remaining people who took time to reply thank you

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    13
    From the suggestion the error free code is
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include"lib.h"
    
    
    void main()
     {
      int i =0;
      clrscr();
    
    
      gotoxy(0,0);
      HLINE(12)
      gotoxy(1,1);
      printf("\n");
      HLINE(14)
      getch();
     }
    and lib.h

    Code:
    #define HLINE(val) do{       \
    		     for(i=0;i<=val;i++)\
    		     {           \
    		     printf("%c",196); \
    		     }            \
    		     }while(0);
    Thank you for making me understanding macros better

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by slain View Post
    From the suggestion the error free code is
    Code:
     #define HLINE(val) do{       \
                 for(i=0;i<=val;i++)\
                 {           \
                 printf("%c",196); \
                 }            \
                 }while(0);
    It works that way but typically you leave out the final semicolon from the macro. That way in your code you can write

    HLINE(foo);

    in the normal fashion.

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    13
    Even this works great !!
    Code:
    #define HLINE(val) for(i=0;i<=val;i++)\
                        {           \
                          printf("%c",196); \
                        }            
    
    Last edited by slain; 02-01-2013 at 07:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with macros and templates
    By Nickedname in forum C++ Programming
    Replies: 14
    Last Post: 10-26-2011, 06:52 AM
  2. problem with program number divisible by 10? using macros
    By MarjunC in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2010, 05:43 AM
  3. Problem with macros..
    By sanddune008 in forum C Programming
    Replies: 4
    Last Post: 07-07-2009, 02:00 AM
  4. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  5. Macros Using #s
    By Krak in forum C++ Programming
    Replies: 21
    Last Post: 07-18-2005, 01:03 AM

Tags for this Thread