Macro confusion

This is a discussion on Macro confusion within the C Programming forums, part of the General Programming Boards category; Thought these things would be simple, but apparently I don't understand them. All I want to do is have an ...

  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.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  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;
    
    }

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 08:11 AM
  5. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21