CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
The examples so far have used the sort of indentation and line layout that is common in languages belonging to the same family as C. They are 'free format' languages and you are expected to use that freedom to lay the program out in a way that enhances its readability and highlights its logical structure. Space (including horizontal tab) characters can be used for indentation anywhere except in identifiers or keywords without any effect on the meaning of the program. New lines work in the same way as space and tab except on preprocessor command lines, which have a line-by-line structure.
If a line is getting too long for comfort there are two things you can do. Generally it will be possible to replace one of the spaces by a newline and use simply two lines instead, as this example shows.
/* a long line */If you're unlucky it may not be possible to break the lines like that. The preprocessor suffers most from the problem, because of its reliance on single-line 'statements'. To help, it's useful to know that the sequence 'backslash newline' becomes invisible to the C translation system. As a result, the sequence is valid even in unusual places such as the middle of identifiers, keywords, strings and so on. Only trigraphs are processed before this step.
/*The only time that you might want to use this way of breaking lines (outside of preprocessor control lines) is to prevent long strings from disappearing off the right-hand side of a program listing. New lines are not permitted inside strings and character constants, so you might think that the following is a good idea.
/* not a good way of folding a string */That will certainly work, but for strings it is preferable to make use of the string-joining feature introduced by the Standard:
/* This string joining will not work in Old C */The second example allows you to indent the continuation portion of the string without changing its meaning; adding indentation in the first example would have put the indentation into the string.
Incidentally, both examples contain what is probably a mistake. There is no space in front of the 'long' in the continuation string, which will contain the sequence 'verylong' as a result. Did you notice?
Comment, as has been said already, is introduced by the character pair
and terminated by
. It is translated into a single space
wherever it occurs and so it follows exactly the same rules that spaces do.
It's important to realize that it doesn't simply disappear, which it used to do
in Old C, and that it is not possible to put comment into strings or character
constants. Comment in such a place becomes part of the string or constant:
Old C was a bit hazy about what the deletion of comment implied. You could argue that
int/**/egral();should have the comment deleted and so be taken by
the compiler to be a call of a function named integral
. The Standard C rule is that comment is to be
read as if were a space, so the example must be equivalent to
which declares a function egral
that returns type int
.
The various character translation, line joining, comment recognition and other early phases of translation must be specified to occur in a certain order. The Standard says that the translation is to proceed as if the phases occurred in this order (there are more phases, but these are the important ones):
Each stage is completed before the next is started.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 821
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved