Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Comments in C/C++

Tags:

c++

c

comments

This is an interview question:-

Write a C program which when compiled and run, prints out a message indicating whether the compiler that it is compiled with, allows /* */ comments to nest.

The solution to this problem is given below :-

Sol:- you can have an integer variable nest:

int nest = /*/*/0*/**/1;

if it supports nested comments then the answer is 1 else answer is 0.

How is this working ? I don't understand the variable declaration.

like image 308
Pritpal Avatar asked Jul 14 '11 18:07

Pritpal


1 Answers

If the compiler doesn't allow nesting, the first */ will terminate the opening of the multiline comment, meaning the 0 won't be commented out. Written with some spaces:

int nest = /*/*/ 0 * /**/ 1;

resulting in the code

int nest = 0 * 1; // -> 0

If it allows nesting, it will be

int nest = /*/*/0*/**/ 1;

resulting in

int nest = 1;
like image 54
Femaref Avatar answered Sep 20 '22 05:09

Femaref