Thread: need help with multi line macro usage

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    need help with multi line macro usage

    Consider the following incorrect code segment. It has two pieces to it: PRINT_THREE_TIMES macro and the code
    that uses the macro:

    #define PRINT_THREE_TIMES \
    printf ("one: %d\n", 1); \
    printf ("two: %d\n", 2); \
    printf ("three: %d\n", 3); \


    if (i == 1)
    printf( “Only once: %d\n”, 1 );
    else if (i == 2)
    printf( “i == 2, we don’t need to print anything here!\n”) ;
    else if (i == 3)
    PRINT_THREE_TIMES

    The code does not quite do what is intended.




    show how to do this by changing the macro alone. (You are not allowed to consolidate the three ‘printf’
    statements to a single one.)

    #define PRINT_THREE_TIMES :

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First thing to understand is what happens during the preprocessor phase and what the code looks like afterwards. So lets take a look:
    Code:
    if (i == 1) 
    printf( “Only once: %d\n”, 1 );
    else if (i == 2) 
    printf( “i == 2, we don’t need to print anything here!\n”) ;
    else if (i == 3) 
    printf ("one: %d\n", 1); 
    printf ("two: %d\n", 2); 
    printf ("three: %d\n", 3);
    Now if you can't see the problem yet lets add proper indentation
    Code:
    if (i == 1) 
      printf( “Only once: %d\n”, 1 );
    else if (i == 2) 
      printf( “i == 2, we don’t need to print anything here!\n”) ;
    else if (i == 3) 
      printf ("one: %d\n", 1); 
    printf ("two: %d\n", 2); 
    printf ("three: %d\n", 3);
    Wait thats not what you wanted. You wanted this:
    Code:
    if (i == 1) 
      printf( “Only once: %d\n”, 1 );
    else if (i == 2) 
      printf( “i == 2, we don’t need to print anything here!\n”) ;
    else if (i == 3) 
    {
      printf ("one: %d\n", 1); 
      printf ("two: %d\n", 2); 
      printf ("three: %d\n", 3); 
    }
    So can you guess how you should change the macro?

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Unhappy

    Sorry, I am a novice to using macro; still don't get it.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Are you saying by adding opening/closing braces that would do the job? I have tried a do while, but program does not compile.
    Likewise, do I see a problem without the index been declared?
    Last edited by Cdigitproc1; 04-28-2005 at 04:29 PM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Macros simply replace every instance of the macro name in your code with what you define the macro as. Try just adding opening/closing braces. Think about where you'd put them.
    If you understand what you're doing, you're not learning anything.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #define PRINT_THREE_TIMES \
    printf ("one: %d\n", 1); \
    printf ("two: %d\n", 2); \
    printf ("three: %d\n", 3); \
    Also, you shouldn't have the \ on that last line, as it serves no purpose.

    We could rewrite that macro many ways:
    Code:
    #define THRICE \
        printf("uno: %d", 1 ), \
        printf("dos: %d", 2 ), \
        printf("tres: %d", 1 );
    There's a fun variant.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Smile

    Thanks a trillion for the tips guys.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by quzah
    Code:
    #define PRINT_THREE_TIMES \
    printf ("one: %d\n", 1); \
    printf ("two: %d\n", 2); \
    printf ("three: %d\n", 3); \
    Also, you shouldn't have the \ on that last line, as it serves no purpose.

    We could rewrite that macro many ways:
    Code:
    #define THRICE \
        printf("uno: %d", 1 ), \
        printf("dos: %d", 2 ), \
        printf("tres: %d", 1 );
    There's a fun variant.

    Quzah.
    here's another one

    Code:
    #define THRICE \
    	do { \
    		printf("uno: %d\n", 1 ); \
    		printf("dos: %d\n", 2 ); \
    		printf("tres: %d\n", 1 ); \
    	} while (0)
    also when you forget a semicolon after using THRICE the compiler will complain then because it's missing a semicolon after while(0)

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Sorry, I am a novice to using macro; still don't get it.
    And so endeth the first lesson.
    Whilst there are some problems which are best solved with a macro, this isn't one of them.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM