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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With