problem in macro

This is a discussion on problem in macro within the C Programming forums, part of the General Programming Boards category; Hi, I have 2 questions regarding macros.. Problem 1: I have to write a program to find greatest of 3 ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    problem in macro

    Hi,

    I have 2 questions regarding macros..

    Problem 1:

    I have to write a program to find greatest of 3 numbers and add greatest number to smallest one.I have done as follow:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAX(a,b,c) ((a>b && a>c)?a:0 || (b>a && b>c)?b:0 || (c>a && c>b)?c:0);
    
    int main()
    {
        int x,y,z;
    	int temp;
    	temp= MAX(44,57,600);
    	printf("%d",temp);
    	getchar();
    }
    I am able to get the greatest number but not able to add with smallest.

    Problem 2:

    Can any body tell me how to return string from a macro ?
    Is it possible or macro always return integer or float values?
    I have searched but didn't get any code related to that...

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Why not write a function instead?
    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
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    good suggestion.....

    But I have to used macro only.....

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Okay... so what exactly is the macro supposed to do? I do not think you find both the maximum and return the sum at the same time with a macro.
    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

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Macros do not "return" something. They REPLACE something. You can certainly defined macros that provide a string result, e.g.
    Code:
    #define NAME "Mats"
    #define ADDRESS "18 Some Street, Some Town, County, AA11 11P, United Kingdom"
    We can also do "funny" things with macros and strings:
    Code:
    #define aorb() (a)?"a":"b"
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    Code:
    #define MAX(a,b,c) ((a>b && a>c)?a:0 || (b>a && b>c)?b:0 || (c>a && c>b)?c:0);
    After u defined a macro you must not put ";"

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Ok...
    i.e I have to define a new macro for getting string values....
    right ??

  8. #8
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    what i mean is: if you define that macro like that you get an error if u do something like:
    Code:
    int var, number = 10;
    var = MAX(a,b,c) + number;

  9. #9
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    Code:
    #define MAX(a,b,c) ((a>b && a>c)?a:0 || (b>a && b>c)?b:0 || (c>a && c>b)?c:0);
    And by the way, by doing that u don't get the greatest number. MAX(a,b,c) will always be 0 or 1 no meter what numbers a,b,c will be.

    you can write smthg like:
    Code:
    #define MAX(a,b,c) (a>b && a>c) ? a : ( (b > a && b > c) ? b : ((c>a && c> b) ? c : 0))
    This return the greatest nr or 0 (if there is no greater nr. example: the numbers are equal).

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you're doing too much in one macro. break it down into two: one macro to get the max of 2 numbers then another to get the max of 3. the macro that gets the max of 3 can use the max of 2 macro to help.
    Code:
    #define MAX(a,b) (a > b ? a : b)
    #define MAX3(a,b,c) (MAX(a, b) > c ? MAX(a, b) : c)
    now it's not as complicated.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ralu. View Post
    Code:
    #define MAX(a,b,c) ((a>b && a>c)?a:0 || (b>a && b>c)?b:0 || (c>a && c>b)?c:0);
    And by the way, by doing that u don't get the greatest number. MAX(a,b,c) will always be 0 or 1 no meter what numbers a,b,c will be.

    you can write smthg like:
    Code:
    #define MAX(a,b,c) (a>b && a>c) ? a : ( (b > a && b > c) ? b : ((c>a && c> b) ? c : 0))
    This return the greatest nr or 0 (if there is no greater nr. example: the numbers are equal).
    The orignal posted code WORKS (in my example), although it may fail for a set of all negative numbers - which the later posted samples do not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    Maybe it works with your compiler but it doesn't mean it will work with every c compiler.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bargi View Post
    Ok...
    i.e I have to define a new macro for getting string values....
    right ??
    What sort of strings do you want?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by ralu.
    Maybe it works with your compiler but it doesn't mean it will work with every c compiler.
    The C Standard does not seem to forbid a semi-colon at the end of a macro's definition, so the only errors will result from the "normal" usage of the macro, not its definition.
    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

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    The C Standard does not seem to forbid a semi-colon at the end of a macro's definition, so the only errors will result from the "normal" usage of the macro, not its definition.
    I think ralu's comment was to my comment that the macro originally posted provides the right result - it does make interesting use of the || operator, which could, as ralu points out, lead to result of 0 or 1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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