home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!pipex!unipalm!uknet!glasgow!unix.brighton.ac.uk!amn
- From: amn@unix.brighton.ac.uk (Anthony Naggs)
- Newsgroups: comp.lang.c
- Subject: Re: Multiple &'s in an if statement
- Keywords: &
- Message-ID: <1992Nov24.012639.5810@unix.brighton.ac.uk>
- Date: 24 Nov 92 01:26:39 GMT
- References: <1992Nov23.164530.19214@iacd>
- Reply-To: amn@vms.brighton.ac.uk
- Distribution: comp.lang.c
- Organization: University of Brighton, UK
- Lines: 51
-
- In article <1992Nov23.164530.19214@iacd> bcochell@ips.iacd.honeywell.com writes:
- >Greetings,
- >
- >Can anyone explain why this works correctly (That is, it checks to see that
- >the last 5 characters of name are all digits and that it is null terminated):
- >
- > if ( isdigit(name[strlen(path)]) &
- > isdigit(name[(strlen(path) + 1)]) &
- > isdigit(name[(strlen(path) + 2)]) &
- > isdigit(name[(strlen(path) + 3)]) &
- > isdigit(name[(strlen(path) + 4)]) )
- > if ( iscntrl(name[(strlen(path) + 5)]) ) {
- > printf("%s\n",name);
- > }
-
- isdigit (which is a macro by the way) will return a value, say 4.
-
- Now 4 & 4 & 4 & 4 & 4 is equal to 4.
-
- iscntrl will return a different value, say 2.
-
- >While the following does not?:
- >
- > if ( isdigit(name[strlen(path)]) &
- > isdigit(name[(strlen(path) + 1)]) &
- > isdigit(name[(strlen(path) + 2)]) &
- > isdigit(name[(strlen(path) + 3)]) &
- > isdigit(name[(strlen(path) + 4)]) &
- > (name[(strlen(path) + 5)] == NULL ) ) {
- > printf("%s\n",name);
- > }
- >
- >Why do I have to check for NULL with a separate if statement?
-
- Because you should be using '&&' which ANDs tests together, rather than
- '&' which ANDs the binary bits.
-
- The result of (name[..] == NULL) will be either 1 or 0. If you bitwise
- AND either with 2 you get 0!
-
- >Any opinions will be appreciated
-
- Sure? Okay, try reading a beginners guide to C, or going on a training
- course. Learn the language, please, before you do real work with it.
-
- Regards,
- Anthony Naggs
- Software/Electronics Engineer P O Box 1080, Peacehaven
- East Sussex BN10 8PZ
- Phone: +44 273 589701 Great Britain
- Email: (c/o Univ of Brighton) amn@vms.brighton.ac.uk or xa329@city.ac.uk
-