Expert C Programming

(Jeff_L) #1

Available Hardware Is a Crayon?


One new feature introduced with ANSI C is the convention that adjacent string literals are
concatenated into one string. This replaces the old way of constructing multiline messages using
escaped newlines, and starting each continuation string in column one.


Old style:


printf( "A favorite children's book \


is 'Muffy Gets It: the hilarious tale of a cat, \


a boy, and his machine gun'" );


This can now be written as a series of adjacent string literals that will automatically be joined together
as one at compile-time. The nul character that normally ends a string literal is dropped from all joined
string literals except the last one.


New style:


printf( "A second favorite children's book "


"is 'Thomas the tank engine and the Naughty Enginedriver


who "


"tied down Thomas's boiler safety valve'" );


However, the automatic concatenation means that a missing comma in an initialization list of string
literals no longer causes a diagnostic message. A missing comma now results in a silent marriage of
adjacent strings. This has dire consequences in circumstances like the following:


char *available_resources[] = {


"color monitor",


"big disk",


"Cray" / whoa! no comma! /


"on-line drawing routines",


"mouse",


"keyboard",


"power cables", / and what's this extra comma? /


};


So available_resources[2] is "Crayon-line drawing routines". There's quite a difference between
having a "Cray" with "on-line drawing routines" and just having some routines to draw lines with
crayons...


The total number of resources is one less than expected, so writing to


available_resources[6] will trash another variable. And by the way, that trailing comma


after the final initializer is not a typo, but a blip in the syntax carried over from aboriginal C. Its
presence or absence is allowed but has no significance. The justification claimed in the ANSI C

Free download pdf