#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    ValidateAlphaNumerice values


    Hi There,
    I have to validate the Alphanumeric values as below, please correct me if Iam wrong.
    The valid values are:
    ' ' (one space)
    1
    2
    3
    4
    I have written the below code, please help

    int isValid(char c)
    int rc = 0;
    {
    if(c == ' ' || c == '1' || c == '2' || c == '3' || c == '4')
    rc = 0;
    else
    rc = 1;
    return (rc) ;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    You need to put the definition of rc inside the braces
    Code:
    int isValid(char c)
    {
        int rc = 0;
    
        if(c == ' ' || c == '1' || c == '2' || c == '3' || c == '4')
            rc = 0;
        else
            rc = 1;
        return (rc) ;
    }
    Also the parenthesis around the value to be returned are usually not used (but they are merely redundant).
    A plain return rc; would have been enough.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    I don't believe in extra code....
    Code:
    #define ISVALID(C) (!(C == ' ' || C == '1' || C == '2' || C == '3' || C == '4'))
    
    int isValid(char c) {
      return !(c == ' ' || c == '1' || c == '2' || c == '3' || c == '4');
    }
    (sure, we could use unequal to this and unequal to that...)
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    I'm pretty sure both the long and the short version compile to the same object code (possibly after turning on some level of optimization) ... checking ... HMMM ... well almost: the difference is very very small :)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by b49P23TIvg
    I don't believe in extra code....
    Code:
    #define ISVALID(C) (!(C == ' ' || C == '1' || C == '2' || C == '3' || C == '4'))
    
    int isValid(char c) {
      return !(c == ' ' || c == '1' || c == '2' || c == '3' || c == '4');
    }
    (sure, we could use unequal to this and unequal to that...)
    Your macro is problematic:

    1. If C is an expression with side effects (like `getchar()'), those effects will occur five times - certainly a bug.
    2. Due to operator precedence, if C is e.g. `x ^ 0x1', the macro breaks.

    Point 2 can be fixed by parenthesizing all occurences of C in the macro definition, but it's difficult to fix point 1 without introducing other problems. I would actually advise C programmers not to use macros at all. If you just want to avoid the overhead of a function call, use an inline function instead (supported by both C99 and C++).

    Comments on this post

    • b49P23TIvg agrees : I swear I've never before forgotten to parenthesize macro arguments!

IMN logo majestic logo threadwatch logo seochat tools logo