Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Issues with array, strlen, etc |
| Date | 2015-09-08 09:04 -0700 |
| Organization | None to speak of |
| Message-ID | <lnlhcgsxs9.fsf@kst-u.example.com> (permalink) |
| References | <1866812f-e415-44b4-8127-8e21c4a13653@googlegroups.com> <1e5f8717-847a-451c-8e50-7e87115ead51@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
> Thank you very much for these explanations.
>
> I have revised the program based on your explanations and suggestions.
>
> Bartekltg, yes, I understand (now) what happens when '0' is being
> subtracted :) Given '0' equals 48, by subtracting '0' from any given
> character I get the correct digit (integer, not an ascii character).
'0' is equal to 48 only if you're using an ASCII-based character
set, which the standard doesn't guarantee. For example, EBCDIC
assigns the value 240 to the character '0'.
*However* the C standard guarantees that the character codes for
the digits '0' through '9' have consecutive codes, so subtracting
'0' from a digit *is* give you the value ('9' - '0' == 9 for any
conforming implementation, even if '0' != 48).
Keep in mind that there's no such guarantee for letters; 'z' -
'a' == 25 in ASCII but not in EBCDIC.
[...]
> for (i = (strlen(card_number) - 2); i >= 0; i -= 2)
> {
> number = (card_number[i]-'0') * 2;
> printf (" [%i] ", number);
> }
[...]
This evaluates strlen(card_number) each time through the loop, which is
inefficient. The strlen() function has to scan the entire string to
find the terminating '\0', which can turn an O(N) loop into an O(N**2)
loop. You probably won't notice the difference for reasonably short
strings, and micro-optimization is *usually* less important than
clarity, but for something like this, it's worth it to save the length
in a variable:
size_t len = strlen(card_number);
for (i = len - 2, ...)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-07 10:47 -0700
Re: Issues with array, strlen, etc James Kuyper <jameskuyper@verizon.net> - 2015-09-07 14:47 -0400
Re: Issues with array, strlen, etc bartekltg <bartekltg@gmail.com> - 2015-09-07 22:03 +0200
Re: Issues with array, strlen, etc Paul N <gw7rib@aol.com> - 2015-09-07 13:08 -0700
Re: Issues with array, strlen, etc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-07 22:55 +0100
Re: Issues with array, strlen, etc Rosario19 <Ros@invalid.invalid> - 2015-09-08 07:59 +0200
Re: Issues with array, strlen, etc Rosario19 <Ros@invalid.invalid> - 2015-09-08 08:05 +0200
Re: Issues with array, strlen, etc Barry Schwarz <schwarzb@dqel.com> - 2015-09-07 20:35 -0700
Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 01:08 -0700
Re: Issues with array, strlen, etc Keith Thompson <kst-u@mib.org> - 2015-09-08 09:04 -0700
Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-10 01:22 -0700
Re: Issues with array, strlen, etc Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 09:38 +0100
Re: Issues with array, strlen, etc Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-10 07:34 -0400
Re: Issues with array, strlen, etc David Brown <david.brown@hesbynett.no> - 2015-09-10 13:47 +0200
Re: Issues with array, strlen, etc Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 13:11 +0100
Re: Issues with array, strlen, etc Keith Thompson <kst-u@mib.org> - 2015-09-10 10:21 -0700
Re: Issues with array, strlen, etc Geoff <geoff@invalid.invalid> - 2015-09-10 10:42 -0700
Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 01:11 -0700
Re: Issues with array, strlen, etc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 09:51 +0100
Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 02:39 -0700
csiph-web