Thread: Macro confusion

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19

    Macro confusion

    Thought these things would be simple, but apparently I don't understand them. All I want to do is have an input of a message, and the macro picks it up and prints it until you close the window. What needs to change, or am I way off in left field with this one?

    Code:
    #include <stdio.h>
    #define ECHO (str) gets (message);\
                       for (i=0; i != NULL; ++i)\
                       puts(message);
    
    int main ()
    {
    int i;
    i=0;
    printf("Enter a message:");
    Echo (str);
    
    return 0;
    
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    #include <stdio.h>
    #define ECHO (str) gets (message);\
                       for (i=0; message[i] != NULL; ++i)\
                       putchar(message[i]);
    
    int main ()
    {
    int i;
    i=0;
    printf("Enter a message:");
    Echo (str);
    
    return 0;
    
    }
    I think that was your intent. Code's pretty funky, but, I think it'll work.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    Now the error message is that Echo is undefined. That makes no sense to me since right before Echo is the word define. What gives?

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Case sensitive

    Code:
    #include <stdio.h>
    #define ECHO (str) gets (message);\
                       for (i=0; message[i] != NULL; ++i)\
                       putchar(message[i]);
    
    int main ()
    {
    int i;
    i=0;
    printf("Enter a message:");
    ECHO(str);
    
    return 0;
    
    }

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    That pretty well sums it up. Thanks for helping this lowly noob.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Also you should remove space between ECHO and (

    I doubt it will work because message is undefined, str is undefined (not not used by the macro, why do you need it?)

    Don't use gets - use fgets (see FAQ)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    this will do what u want.

    Code:
    #include <stdio.h>
    #define ECHO(message) fgets(message,80,stdin);\
                            fflush(stdout);\
                       fputs(message,stdout);
    
    int main ()
    {
        int i=0;
        char str[80];
        
        printf("Enter a message:");
        ECHO(str);
    
        getchar();
        return 0;
    }
    /*my output
    Enter a message:hello there
    hello there
    */
    ssharish2005

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What needs to change, or am I way off in left field with this one?
    Like you should be using a function if you possibly can, not an inline macro.

    Consider the mess you get into by doing say
    if ( condition ) ECHO(message);
    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.

  9. #9
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Multiline macros can be done quite well when using a do ... while(0) construct. Of course one should use functions an let the compiler decide

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Cofusion regarding NULL macro
    By Bargi in forum C Programming
    Replies: 3
    Last Post: 01-16-2007, 09:11 AM
  5. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM