Discuss ValidateAlphaNumerice values in the C Programming forum on Dev Shed. ValidateAlphaNumerice values C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 1
Time spent in forums: 26 m 39 sec
Reputation 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) ;
}
Posts: 3,383
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 44 m 29 sec
Reputation Power: 383
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!
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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
Posts: 1,936
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Quote:
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++).