|  | K&R Exercise 6-2 |  | |
| | | mdh |  |
| Posted: Fri Aug 29, 2008 3:34 am Post subject: K&R Exercise 6-2 |  |
In most of the exercises that K&R write, there seems to be a relationship to some library function or some aspect of C that is used later. So, quick question to those more experienced programmers. Other than this being an illustration of the use of structs, is this illustrative of some aspects of C that you use all the time. Thanks as usual.
Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don't count words within strings and comments. Make 6 a parameter that can be set from the command line.
<<<<<<< |
| |
| | | Richard Heathfield |  |
| Posted: Fri Aug 29, 2008 3:34 am Post subject: Re: K&R Exercise 6-2 |  |
| |  | |
mdh said:
| Quote: | In most of the exercises that K&R write, there seems to be a relationship to some library function or some aspect of C that is used later.
|
It's worse than that. They seem to be guiding you towards writing a C implementation!
| Quote: | So, quick question to those more experienced programmers. Other than this being an illustration of the use of structs, is this illustrative of some aspects of C that you use all the time. Thanks as usual.
Exercise 6-2 Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don't count words within strings and comments. Make 6 a parameter that can be set from the command line.
|
In C90, an implementation must be able to distinguish between external identifiers that are unique in the first 6 characters (without regard to case!). A program such as the one required in Exercise 6-2 could be useful for diagnosing source code that ignores this limit (as I suspect a vast amount of source code does).
(Incidentally, this rather strange rule was a pragmatic recognition of the fact that, at the time the Standard was written, there really were linkers around that couldn't handle long identifiers. Rather than outlaw those linkers, the Standard worked them in. In C99, the limit was revised to 31, so, if that /was/ the intent of the question, its last sentence turns out to have been rather prescient.
-- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
| |
| | | mdh |  |
| Posted: Fri Aug 29, 2008 4:01 am Post subject: Re: K&R Exercise 6-2 |  |
| |  | |
On Aug 28, 8:57 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
| Quote: | mdh said:
In most of the exercises that K&R write, there seems to be a relationship to some library function or some aspect of C that is used later.
It's worse than that. They seem to be guiding you towards writing a C implementation!
So, quick question to those more experienced programmers. Other than this being an illustration of the use of structs, is this illustrative of some aspects of C that you use all the time. Thanks as usual.
Exercise 6-2 Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don't count words within strings and comments. Make 6 a parameter that can be set from the command line.
In C90, an implementation must be able to distinguish between external identifiers that are unique in the first 6 characters (without regard to case!). A program such as the one required in Exercise 6-2 could be useful for diagnosing source code that ignores this limit (as I suspect a vast amount of source code does).
|
Thanks Richard. |
| |
| | | Eric Sosman |  |
| Posted: Fri Aug 29, 2008 10:11 am Post subject: Re: K&R Exercise 6-2 |  |
mdh wrote:
| Quote: | In most of the exercises that K&R write, there seems to be a relationship to some library function or some aspect of C that is used later. So, quick question to those more experienced programmers. Other than this being an illustration of the use of structs, is this illustrative of some aspects of C that you use all the time. Thanks as usual.
Exercise 6-2 Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don't count words within strings and comments. Make 6 a parameter that can be set from the command line.
|
The exercise seems designed to point out the difference between strcmp() and strncmp(), in the context of a comparison function for qsort(). So there's certainly a library tie-in, if that's what you felt was missing.
-- Eric Sosman esosman@ieee-dot-org.invalid |
| |
| | | pete |  |
| Posted: Fri Aug 29, 2008 11:11 am Post subject: Re: K&R Exercise 6-2 |  |
mdh wrote:
| Quote: | On Aug 29, 5:11 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote: mdh wrote: In most of the exercises that K&R write, there seems to be a relationship to some library function ....... Exercise 6-2
The exercise seems designed to point out the difference between strcmp() and strncmp(), in the context of a comparison function for qsort(). So there's certainly a library tie-in, if that's what you felt was missing.
Thank you Eric.
|
However, the definition of "alphabetical order" according to strcmp, isn't exactly the same on different character sets.
In ASCII, a variable name like "A6A", comes before "ABA". In EBCDIC, a variable name like "A6A", comes after "ABA".
Also uppercase letters come before lower case in ASCII, but not in EBCDIC.
-- pete |
| |
| | | Doug Miller |  |
| Posted: Fri Aug 29, 2008 11:17 am Post subject: Re: K&R Exercise 6-2 |  |
In article <16OdnWCI1MoDaCrVnZ2dnUVZ_q_inZ2d@earthlink.com>, pfiland@mindspring.com wrote:
| Quote: | However, the definition of "alphabetical order" according to strcmp, isn't exactly the same on different character sets.
In ASCII, a variable name like "A6A", comes before "ABA". In EBCDIC, a variable name like "A6A", comes after "ABA".
Also uppercase letters come before lower case in ASCII, but not in EBCDIC.
More specifically, |
EBCDIC: a..z < A..Z < 0..9 ASCII: 0..9 < A..Z < a..z
-- Regards, Doug Miller (alphageek-at-milmac-dot-com)
Join the UseNet Improvement Project: killfile Google Groups. LINK
Get a copy of my NEW AND IMPROVED TrollFilter for NewsProxy/Nfilter by sending email to autoresponder at filterinfo-at-milmac-dot-com You must use your REAL email address to get a response.
Download Nfilter at LINK |
| |
| | | mdh |  |
| Posted: Fri Aug 29, 2008 12:37 pm Post subject: Re: K&R Exercise 6-2 |  |
On Aug 29, 5:11 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
| Quote: | mdh wrote: In most of the exercises that K&R write, there seems to be a relationship to some library function .......
Exercise 6-2
The exercise seems designed to point out the difference between strcmp() and strncmp(), in the context of a comparison function for qsort(). So there's certainly a library tie-in, if that's what you felt was missing.
|
Thank you Eric. |
| |
| | | August Karlstrom |  |
| Posted: Fri Aug 29, 2008 12:51 pm Post subject: Re: K&R Exercise 6-2 |  |
Richard Heathfield wrote: [...]
| Quote: | In C90, an implementation must be able to distinguish between external identifiers that are unique in the first 6 characters (without regard to case!). A program such as the one required in Exercise 6-2 could be useful for diagnosing source code that ignores this limit (as I suspect a vast amount of source code does).
|
So a library which exports e.g. the identifiers string_prepend and string_append are not really C90 compliant?
August |
| |
| | | Harald van Dijk |  |
| Posted: Fri Aug 29, 2008 1:05 pm Post subject: Re: K&R Exercise 6-2 |  |
On Fri, 29 Aug 2008 16:51:56 +0200, August Karlstrom wrote:
| Quote: | Richard Heathfield wrote: [...] In C90, an implementation must be able to distinguish between external identifiers that are unique in the first 6 characters (without regard to case!). A program such as the one required in Exercise 6-2 could be useful for diagnosing source code that ignores this limit (as I suspect a vast amount of source code does).
So a library which exports e.g. the identifiers string_prepend and string_append are not really C90 compliant?
|
Correct. However, they are already not C90 compliant -- and not C99 compliant either -- for another reason: external identifiers starting with str and followed by a lowercase letter are reserved for the implementation. |
| |
| | | Richard Heathfield |  |
| Posted: Fri Aug 29, 2008 1:11 pm Post subject: Re: K&R Exercise 6-2 |  |
| |  | |
August Karlstrom said:
| Quote: | Richard Heathfield wrote: [...] In C90, an implementation must be able to distinguish between external identifiers that are unique in the first 6 characters (without regard to case!). A program such as the one required in Exercise 6-2 could be useful for diagnosing source code that ignores this limit (as I suspect a vast amount of source code does).
So a library which exports e.g. the identifiers string_prepend and string_append are not really C90 compliant?
|
Right, for two reasons:
(1) the names invade implementation namespace; (2) the names are a touch on the long side.
In practice, (1) is often ignored, and (2) is practically always ignored. But strictly speaking, yes, you are correct.
Of the two reasons, (1) is probably the more important. Whilst it is certainly possible that some people are still using 6-sig-char linkers nowadays, it isn't terribly likely that terribly many people are. Even twenty years ago (and probably more), people were ignoring the 6-character thing, and it doesn't seem to have caused any major software crises.
-- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|