Using / to extend single-line comments

  • A+

I just noticed that I can use / to extend the single-line comment to the next line, similarly to doing so in pre-processor directives.

Why is nobody speaking for this language feature? I didn't even see it in books.. What language version supports this?


It's part of C. Called line splicing.

The K&R book talks about it

Lines that end with the backslash character / are folded by deleting the backslash and the following newline character. This occurs before division into tokens.

This occurs in the preprocessing phase.

So single line comments can be made to appear like multi line like

//This is / still a single line comment 

Likewise with the case of strings

char str[]="Hello / world. This is / a string"; 

Edit: As noted in the comments, single line comments were not there in ANSI C but were introduced as part of the standard in C99 though many compilers already supported it.

From C99,

Except within a character constant, a string literal, or a comment, the characters // introduce a comment that includes all multibyte characters up to, but not including, the next new-line character. The contents of such a comment are examined only to identify multibyte characters and to find the terminating new-line character.

As far as line splicing is concerned, it is specified in C89 itself Translation phases

  1. Each instance of a new-line character and an immediately preceding backslash character is deleted, splicing physical source lines to form logical source lines. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character.

Look at KamiKaze's answer to see the relevant part of C99.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: