Course contentsShow
← C
Lesson 34 of 1,2191. FoundationsFree lesson

Multi-line comments `/* */`

Multi-line comments /* */

What you'll learn: How to write longer notes in your code that span several lines without confusing the computer.

The digital sticky note

In programming, comments are parts of your file that the computer completely ignores. If single-line comments are like a quick jotting in a margin, multi-line comments are like a large sticky note or a paragraph in a diary.

Sometimes, a single line isn't enough to explain a complex logic choice, list the authors of a project, or provide a legal disclaimer. Instead of putting a marker at the start of every single line, C gives us a way to "open" a comment and "close" it, wrapping everything inside like a sandwich.

How it works: The sandwich method

To start a multi-line comment, you use a forward slash followed by an asterisk: /*. To end it, you do the opposite: an asterisk followed by a forward slash: */.

Think of /* as the "Silence" sign in a library and */ as the "Resume Speaking" sign. Anything you type between these two symbols is invisible to the C compiler.

/* 
   Project: Mars Rover Navigation
   Author: Alex C.
   Date: October 2023
   Description: This code calculates the distance 
   between the rover and the nearest crater.
*/

#include <stdio.h>

int main() {
    /* We are initializing the distance to zero 
       because the sensors haven't fired yet. */
    int distance = 0;

    printf("Distance initialized.");
    return 0;
}

When to use them

Multi-line comments are perfect for "Header Comments" at the very top of your file. This is where you tell other humans what the file does. They are also useful for "commenting out" large chunks of code. If you have ten lines of code that aren't working and you want to hide them temporarily to test something else, you can simply wrap the whole block in /* and */ instead of deleting them.

One important rule: you cannot nest these comments. You can't put a /* */ inside another /* */. The computer will see the first */ it encounters and think the entire comment is over, leaving the rest of your notes to cause errors.

Key takeaway

Use /* to start a comment and */ to end it, allowing you to write notes across as many lines as you need.