What is the purpose of comments in a C program?
A
To make the code run faster B
To explain the code to the compiler C
To document and explain code for humans D
To print messages to the console
Analysis & Theory
Comments help programmers understand and document the code; they are ignored by the compiler.
Which of the following is a valid single-line comment in C?
A
# This is a comment B
// This is a comment C
-- This is a comment D
/* This is a comment */
Analysis & Theory
`//` starts a single-line comment in C.
Which of the following is a valid multi-line comment in C?
A
// This is a comment // Another line B
/* This is a
multi-line comment */ C
# This is a
multi-line comment D
!! This is a multi-line comment !!
Analysis & Theory
C uses `/* ... */` for multi-line comments.
What happens to comments when a C program is compiled?
A
They are converted to code B
They are stored in memory C
They are ignored by the compiler D
They are shown in the output
Analysis & Theory
Comments are removed by the preprocessor and not included in the compiled code.
Which of these will cause a compile-time error?
A
// Comment B
/* Comment */ C
/* Unclosed comment D
// Comment line
Analysis & Theory
A multi-line comment that is not properly closed with `*/` will cause a compile-time error.
Which comment style should you use to comment out multiple lines quickly?
A
// B
*/ C
/* */ D
#
Analysis & Theory
`/* */` can be used to comment out a block of code or multiple lines.
What will this code output?
```c
#include <stdio.h>
int main() {
// printf("Hello");
printf("World");
return 0;
}
```
A
Hello B
World C
Hello World D
Error
Analysis & Theory
`// printf("Hello");` is commented out, so only `World` is printed.
Can you nest multi-line comments in C like this?
```c
/* This is /* nested */ comment */
```
A
Yes, it works fine B
No, it causes an error C
Only in modern C compilers D
Only if you use a macro
Analysis & Theory
C does not support nested multi-line comments. The example will cause an error.
Which of these is NOT a type of comment in C?
A
Single-line comment B
Multi-line comment C
Inline comment D
HTML-style comment
Analysis & Theory
HTML-style comments (`<!-- -->`) are not valid in C.
Why should comments be used carefully in code?
A
They slow down execution B
They are required for compiling C
Too many comments can clutter the code D
They increase program speed
Analysis & Theory
Comments should be meaningful. Excessive or unclear comments can clutter code and reduce readability.