home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17077 < prev    next >
Encoding:
Internet Message Format  |  1992-11-24  |  2.2 KB

  1. Path: sparky!uunet!pipex!unipalm!uknet!glasgow!unix.brighton.ac.uk!amn
  2. From: amn@unix.brighton.ac.uk (Anthony Naggs)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Multiple &'s in an if statement
  5. Keywords: &
  6. Message-ID: <1992Nov24.012639.5810@unix.brighton.ac.uk>
  7. Date: 24 Nov 92 01:26:39 GMT
  8. References: <1992Nov23.164530.19214@iacd>
  9. Reply-To: amn@vms.brighton.ac.uk
  10. Distribution: comp.lang.c
  11. Organization: University of Brighton, UK
  12. Lines: 51
  13.  
  14. In article <1992Nov23.164530.19214@iacd> bcochell@ips.iacd.honeywell.com writes:
  15. >Greetings,
  16. >
  17. >Can anyone explain why this works correctly (That is, it checks to see that
  18. >the last 5 characters of name are all digits and that it is null terminated):
  19. >
  20. >   if ( isdigit(name[strlen(path)]) &
  21. >        isdigit(name[(strlen(path) + 1)]) &
  22. >        isdigit(name[(strlen(path) + 2)]) &
  23. >        isdigit(name[(strlen(path) + 3)]) &
  24. >        isdigit(name[(strlen(path) + 4)]) )
  25. >      if ( iscntrl(name[(strlen(path) + 5)]) ) {
  26. >         printf("%s\n",name);
  27. >      }
  28.  
  29. isdigit (which is a macro by the way) will return a value, say 4.
  30.  
  31. Now 4 & 4 & 4 & 4 & 4 is equal to 4.
  32.  
  33. iscntrl will return a different value, say 2.
  34.  
  35. >While the following does not?:
  36. >
  37. >   if ( isdigit(name[strlen(path)]) &
  38. >        isdigit(name[(strlen(path) + 1)]) &
  39. >        isdigit(name[(strlen(path) + 2)]) &
  40. >        isdigit(name[(strlen(path) + 3)]) &
  41. >        isdigit(name[(strlen(path) + 4)]) &
  42. >        (name[(strlen(path) + 5)] == NULL ) ) {
  43. >      printf("%s\n",name);
  44. >   }
  45. >
  46. >Why do I have to check for NULL with a separate if statement?
  47.  
  48. Because you should be using '&&' which ANDs tests together, rather than 
  49. '&' which ANDs the binary bits.
  50.  
  51. The result of (name[..] == NULL) will be either 1 or 0.  If you bitwise
  52. AND either with 2 you get 0!
  53.  
  54. >Any opinions will be appreciated
  55.  
  56. Sure?  Okay, try reading a beginners guide to C, or going on a training
  57. course.  Learn the language, please, before you do real work with it.
  58.  
  59. Regards,
  60. Anthony Naggs
  61. Software/Electronics Engineer                   P O Box 1080, Peacehaven
  62.                                                 East Sussex  BN10 8PZ
  63. Phone: +44 273 589701                           Great Britain
  64. Email: (c/o Univ of Brighton) amn@vms.brighton.ac.uk  or  xa329@city.ac.uk
  65.