Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using \ to extend single-line comments

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?

like image 556
Edenia Avatar asked Jun 14 '18 13:06

Edenia


People also ask

How to make single line comments in HTML?

You can make comments in HTML by using the <!-- and --> tags. Everything between these tags is ignored by the browser. You have to use these tags for both making single line and multi-line comments. Let's look at examples of how to make single line comments and multi-line comments in HTML. <!-- This is a comment --> <!--

Can a comment span more than one line?

It can span multiple lines. And it can even contain HTML! <p> This is a paragraph inside a multi-line comment. </p> --> Keep in mind that if you use multi-line comments, you will have to make sure you use the --> tag to close the comment. If you don't, the rest of the markup will be ignored and potentially break your page.

What is a single line comment in Python?

What is a single line comment in python? Single line comments are those comments which are written without giving a line break or newline in python. A python comment is written by initializing the text of comment with a #and terminates when the end of line is encountered.

Can I use regex to parse single line comments?

If you want to match such string, use * instead of + in the regex. Finally, although you can use regex to parse such single line comments, as Jerry Coffin said in comment, don't try to parse programming source codes using regexes, because the language constituted by all legal source codes is commonly not a regular language.


1 Answers

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

2.1.1.2 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.

like image 110
J...S Avatar answered Sep 23 '22 22:09

J...S