Thread: MACRO-need help

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    17

    MACRO-need help

    i write some code. the code is like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define CHECK(x) if((x>=48)&&(x<=57))\
                        printf("It's a number.\n");\
                    else if((x>=65)&&(x<=90))\
                        printf("It's a Upper case letter.\n");\
                    else if((x>=97)&&(x<=122))\
                        printf("It's a lower case letter.\n");\
                    else\
                        printf("it's a symbol\n");
    
    #define MINM(x,y) if(x>y)\
                            (x);\
                        else\
                            (y);
    
    int main()
    {
        char i;
        int j,k,l;
        scanf("%c", &i);
        CHECK(i);
        scanf("%d %d",&j,&k);
        l=MINM(j,k);
        printf("%d is the biggest",l);
        return 0;
    }
    but, when i compiled it, the compiler is saying that "error: syntax beffore if" in line 24.
    can someone help me to find out what's wrong with my code?
    Last edited by new-b; 06-08-2009 at 09:34 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > l=MINM(j,k);
    This expands to l = if ( j > k )
    Does that make any sense to you.

    I hope this macro abuse is just an exercise.
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    17
    yes, this is just an exercise

    i have known that actually
    but, i still confuse, which part that makes the compiler said error?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It could have said "RHS of expression does not evaluate to an int".

    "syntax error" is a general catch-all meaning "something's wrong, you figure it out".
    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.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    17
    Quote Originally Posted by Salem View Post
    It could have said "RHS of expression does not evaluate to an int".
    sorry, i don't understand this. What is "RHS"?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right hand side.


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

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you must do macros in this horrible manner, use the ternary operator ?:.

    For a contrived and useless example of its usage:

    Code:
    int bleh(int x)
    {
        return x>=0?1:0;
    }
    This would return 1 if x>=0 and 0 otherwise.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> yes, this is just an exercise

    Good, because this is a really bad idea. Consider what happens if you invoke it like so:

    Code:
    CHECK(x++);
    Anyway, one hack you can use is to enclose all of the statements in a
    Code:
    do{/*bunch of statements*/}while(0)
    .
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #define CHECK(x) if((x>=48)&&(x<=57))\
                        printf("It's a number.\n");\
                    else if((x>=65)&&(x<=90))\
                        printf("It's a Upper case letter.\n");\
                    else if((x>=97)&&(x<=122))\
                        printf("It's a lower case letter.\n");\
                    else\
                        printf("it's a symbol\n");
    This is definitely and absolute MACRO ABUSE - you deserve what's coming to you if you write code like this. This should be a function.

    Macros that as more than one line should be avoided unless there REALLY is no other way (and even then, there's USUALLY a way to use a SIMPLE macro and a function).

    I use macros for three purposes:
    1. to use the preprocessor to optionally (not) produce source code (e.g. ifdef/ifndef for debug code or in header guards)
    2. Debug code that needs line-number and filename info.
    3. Initializations/identifications where a macro can produce extra information from for exampe the name of a variable.



    --
    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.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    4. For great fun and amusement.


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

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by quzah View Post
    4. For great fun and amusement.


    Quzah.
    Oh, yes for obfuscation in homework and such!

    --
    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
    Join Date
    May 2009
    Posts
    17
    i know if that's a bad idea to use MACRO like that.
    it's just an exercise of MACRO.

    what i want to know is just why my compiler report error when i compile it?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by new-b View Post
    i know if that's a bad idea to use MACRO like that.
    it's just an exercise of MACRO.

    what i want to know is just why my compiler report error when i compile it?
    Because it is used incorrectly. See the very first response by Salem which explains it.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    17
    thanks all for your replies. :-)
    it helped me

    i will use the simpler MACRO.
    it's totally an abuse.

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. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM