Expert C Programming

(Jeff_L) #1

Some C programmers have adopted the convention that an argument of " -- " means "from this point


on, no arguments are switches, even if they start with a hyphen." A better solution would put the
burden on the system, not the user, with an argument pro-cessor that divides arguments into options
and non-options. The simple argv mechanism is now too well entrenched for any changes. Just don't
send mail to Effie under pre-1990 versions of Berkeley UNIX.


Space—The Final Frontier


A lot of people will tell you that white space isn't significant in C; that you can have as much or as
little of it as you like. Not so! Here are some examples where white space rad-ically changes the
meaning or validity of a program.



  • The backslash character can be used to "escape" several characters, including a newline. An
    escaped newline is treated as one logical line, and this can be used to continue long strings. A
    problem arises if you inadvertently slip a space or two in between the backslash and the


carriage return, as \ whitespace newline is different than \newline. This error can be hard to


find, as you are looking for something invisible (the presence of a space character where a
newline was intended). A newline is typically escaped to continue a multiline macro
definition. If your compiler doesn't have excellent error messages, you might as well give up
now. Another reason to escape a newline is to continue a string literal, like this:



  • char a[]= "Hi! How are you? I am quite a \


long string, folded onto 2 lines";


The problem of multiline string literals was addressed by ANSI C introducing the convention
that adjacent string literals are glued together. As we point out elsewhere in this chapter, that
approach solved one potential problem at the expense of introducing another.


  • If you squeeze spaces out altogether, you can still run into trouble. For example, what do you
    think the following code means?



z = y+++x;


The programmer might have meant z = y + ++x, or equally could have had z = y++


+ x in mind. The ANSI standard specifies a convention that has come to be known as the


maximal munch strategy. Maximal munch says that if there's more than one possibility for the
next token, the compiler will prefer to bite off the one involving the longest sequence of

characters. So the above example will be parsed as z = y++ + x.


This can still lead to trouble, as the code

z = y+++++x;


will therefore be parsed as z = y++ ++ + x, and cause a compilation error along the


lines of "++ operator is floating loose in space". This will happen even though the compiler

could, in theory, have deduced that the only valid arrangement of spaces is z = y++ +


++x.

Free download pdf