K&R Exercise 6-2

M

mdh

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.

<<<<<<<
 
M

mdh

mdh said:


It's worse than that. They seem to be guiding you towards writing a C
implementation!




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.
 
M

mdh

     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.
 
D

Doug Miller

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.
http://www.improve-usenet.org

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 http://www.milmac.com/np-120.exe
 
A

August Karlstrom

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?


August
 
H

Harald van Dijk

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.
 
A

August Karlstrom

Harald said:
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.

Annoying. Is there a way to make a compiler like GCC emit a warning in
this case?


August
 
A

August Karlstrom

Richard said:
August Karlstrom said:

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.

Thanks for the clarification.


August
 
K

Keith Thompson

Pilcrow said:
Having been one who programmed on an EBCDIC machine, I wonder which
machines might still be using EBCDIC, given the ubiquity of ASCII?

Some IBM mainframes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top