How are comments written in C?

Prepare for the C Certified Entry-Level Programmer Test using flashcards and multiple choice questions with detailed hints and explanations. Sharpen your programming skills and succeed in your certification exam!

In C programming, comments are crucial for adding explanations or annotations to the code without affecting its execution. The correct method for writing comments involves using the syntax /* for multi-line comments and // for single-line comments.

The multi-line comment style begins with /* and ends with */. This allows programmers to enclose a block of text across multiple lines, making it convenient for longer explanations. For instance:


/*

This is a multi-line comment.

It can span several lines.

*/

The single-line comment is initiated with //, which tells the compiler to ignore everything that follows on that specific line. This is useful for adding brief notes or disabling parts of the code temporarily:


// This is a single-line comment

int x = 5; // This initializes x with value 5

Both of these comment styles help improve code readability and maintainability, which is vital when working collaboratively or returning to code after some time. The other options mentioned do not conform to the syntax defined in the C programming language, making them unsuitable for writing comments effectively.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy