If then statement in Macro

This is a discussion on If then statement in Macro within the C Programming forums, part of the General Programming Boards category; I am trying to write a macro that returns true if its parameter is divisible by ten. False if otherwise. ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28

    If then statement in Macro

    I am trying to write a macro that returns true if its parameter is divisible by ten. False if otherwise.

    This is for an "On your own learning" with a book and this problem is stumping me. it complains" expected primary expression before else"

    I appreciate any help

    Thank you



    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    
    
    /* Macro funcions*/
    #define modulus(x) {if(x%10 == 0){printf("true\n"); else printf("false\n");}}
                       
                 
                       
    
    
    /* Declare variables*/
    float centi;
    float fahr;
    char line[100];
    
    
    int main()
    {   
        int count = 12;
        modulus(count);
        
        
        
        
        
        system("pause");                     /* the fix for the collapsing window*/
        return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    Quote Originally Posted by psppb
    I am trying to write a macro that returns true if its parameter is divisible by ten. False if otherwise.
    Macros don't actually return anything. They just get replaced by some code.

    So, let's examine the code:
    Code:
    #define modulus(x) {if(x%10 == 0){printf("true\n"); else printf("false\n");}}
    If we expand the replacement code out nicely:
    Code:
    {
        if(x%10 == 0){
            printf("true\n");
            else printf("false\n");
        }
    }
    Now, it is clear that the else does not match the if because of the misplaced braces. What you should do is either write out the replacement code first and then compress it into one line, or use backslashes at the end of the line as line continuations so you can write:
    Code:
    #define modulus(x) do { \
        if (x % 10 == 0) { \
            printf("true\n"); \
        } else { \
            printf("false\n"); \
        } \
    } while (0)
    I've used a do while(0) so that a missing terminating semi-colon would result in an error, whereas just using braces for a code block will not, which can look weird sometimes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    May I suggest also choosing a name for the macro that is more closely related to what it does? Prefarably uppercase too.
    For example DIVISIBLE_BY_10 would work.

    I also suspect that they are looking for something much simpler than what you have, that does not actually involve printing anything, just giving the result. I.e. something that allows this code to work:
    Code:
    if (DIVISIBLE_BY_10(count))
        printf("true\n");
    else
        printf("false\n");
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28
    Thank you both! Awesome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. macro
    By cable in forum C Programming
    Replies: 6
    Last Post: 09-17-2011, 06:54 AM
  2. What does following macro mean?
    By sanddune008 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 06:27 PM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 02:18 PM
  4. need a macro that does nothing
    By Nyda in forum C Programming
    Replies: 5
    Last Post: 11-18-2004, 09:16 AM
  5. macro's
    By Steven Snell in forum C Programming
    Replies: 3
    Last Post: 10-14-2002, 12:04 AM

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