C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 13th, 2012, 11:00 AM
r15 r15 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 1 r15 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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) ;
}

Reply With Quote
  #2  
Old September 13th, 2012, 11:05 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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.

Reply With Quote
  #3  
Old September 13th, 2012, 11:45 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,383 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
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!

Reply With Quote
  #4  
Old September 13th, 2012, 12:43 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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

Reply With Quote
  #5  
Old September 13th, 2012, 08:46 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
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++).
Comments on this post
b49P23TIvg agrees: I swear I've never before forgotten to parenthesize macro arguments!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > ValidateAlphaNumerice values

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap