Expert C Programming

(Jeff_L) #1

because the former allows the base type of the array to change (from, say, int to char) without


needing a change to the #define, too.


The Sun ANSI C compiler team felt that moving from "unsigned preserving" to "value preserving"
was a totally unnecessary change to C's semantics that would surprise and dismay anyone who
encountered it unexpectedly. So, under the "principle of least astonishment," the Sun compiler
recognizes and compiles ANSI C features, unless the feature would give a different result under K&R
C. If this is the case, the compiler issues a warning and uses the K&R interpretation by default. In
situations like the one above, the programmer should use a cast to tell the compiler what the final
desired type is. Strict ANSI semantics are available on a Sun workstation running Solaris 2.x by using


the compiler option -Xc.


There are plenty of other updates to K&R C in ANSI C, including a few more so-called "quiet
changes" where code compiles under both but has a different meaning. Based on the usual
programmer reaction when they are discovered, these really should be called "very noisy changes
indeed". In general, the ANSI committee tried to change the language as little as possible, consistent
with revising some of the things that undeniably needed improvement.


But that's enough background on the ANSI C family tree. After a little light relief in the following
section, proceed to the next chapter and get started on code!


Some Light Relief—The Implementation-Defined Effects of


Pragmas...


The Free Software Foundation is a unique organization founded by ace MIT hacker Richard Stallman.
By the way, we use "hacker" in the old benevolent sense of "gifted programmer;" the term has been
debased by the media, so outsiders use it to mean "evil genius." Like the adjective bad, "hacker" now
has two opposing meanings, and you have to figure it out from the context.


Stallman's Free Software Foundation was founded on the philosophy that software should be free and
freely available to all. FSF's charter is "to eliminate restrictions on copying, redistribution,
understanding and modification of computer programs" and their ambition is to create a public-
domain implementation of UNIX called GNU (it stands for "GNU's Not UNIX." Yes, really.)


Many computer science graduate students and others agree with the GNU philosophy, and have
worked on software products that FSF packages and distributes for free. This pool of skilled labor
donating their talent has resulted in some good software. One of FSF's best products is the GNU C
compiler family. gcc is a robust, aggressive optimizing compiler, available for many hardware
platforms and sometimes better than the manufacturer's compiler. Using gcc would not be appropriate
for all projects; there are questions of maintenance and future product continuity. There are other tools
needed besides a compiler, and the GNU debugger was unable to operate on shared libraries for a long
time. GNU C has also occasionally been a little, shall we say, giddy in development.


When the ANSI C standard was under development, the pragma directive was introduced.


Borrowed from Ada, #pragma is used to convey hints to the compiler, such as the desire to expand


a particular function in-line or suppress range checks. Not previously seen in C, pragma met with


some initial resistance from a gcc implementor, who took the "implementation-defined" effect very
literally—in gcc version 1.34, the use of pragma causes the compiler to stop compiling and launch a
computer game instead! The gcc manual contained the following:

Free download pdf