Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessing multiline comments and their embedded newlines at the end of file

This is question about C99/C11 (may be C++ too) preprocessor and their standard-compliance.

Let's consider two source files:

/* I'm 
 * multiline
 * comment
 */

and

/* I'm 
 * multiline
 * comment
 */
i_am_a_token;

If we preprocess both files with gcc or clang (several version was tested), there will be a difference. In the first case preprocessor will not keep newlines from the multiline comment. And in the second case all newlines will be kept.

All mentioned standards says (somewhere inside "Translation phases"):

Each comment is replaced by one space character. New-line characters are retained.

Why there is the difference in handling multiline comments at the end of file? And is this behaviour standard-compliant?

like image 830
osgx Avatar asked Jan 08 '14 18:01

osgx


1 Answers

The reason is simple - line numbers and error reporting. Since the compiler reports errors with line numbers, it is convenient so that line numbers in the pre-processed file correspond to line numbers in the original file. That's the reason the lines occupied by comment are preserved when they are followed by code, whereas they don't have to be preserved at the end of file.

As for the standards. The standards

  • C99: ISO/IEC 9899:1999
  • C11: ISO/IEC 9899:2011

specify the language, preprocessing macros etc., but they don't specify how the language should be processed. You can see it in the scope definition of C11:

ISO/IEC 9899:2011 does not specify

  • the mechanism by which C programs are transformed for use by a data-processing system;

which means that preprocessor output is pretty much internal issue, out of the scope of the standard.

like image 170
Tomas Avatar answered Oct 17 '22 08:10

Tomas