← Back to C Course | Chapter 1: Introduction & Basics | Lesson 7 of 21

C Comments

Comments are notes in your code for people to read, and the computer ignores them. It is like writing a reminder in the margin of your homework.
Syntax
c
// single-line comment

/* multi-line
   comment */

Single-Line Comments

A single-line comment starts with // and tells the compiler to ignore everything from that point to the end of the line -- handy for a quick note next to a specific statement without disrupting the surrounding code.

Example: Single-Line Comments

c
#include <stdio.h>
int main() {
	// This line explains the printf below
	printf("Hello");
	return 0;
}

Multi-Line Comments

A multi-line comment starts with /* and ends with */, and everything in between is ignored regardless of how many lines it spans -- useful for explaining a whole block of logic or temporarily disabling several lines at once.

Example: Multi-Line Comments

c
#include <stdio.h>
int main() {
	/* This comment
	   spans multiple lines */
	printf("Hello");
	return 0;
}

Documenting Code

Good comments explain *why* a piece of code exists or works the way it does, not just *what* it does line by line -- the code itself already shows what's happening, but only you know the reasoning behind a tricky decision.

Example: Documenting Code

c
#include <stdio.h>
int main() {
	// Using 1024 because memory is measured in kibibytes here
	int kilobyte = 1024;
	printf("%d", kilobyte);
	return 0;
}

Commenting Out Code

Wrapping a block of code in comment markers instead of deleting it lets you disable it temporarily while debugging, so you can test whether a specific section is the source of a bug without permanently losing that code.

Example: Commenting Out Code

c
#include <stdio.h>
int main() {
	printf("Active code");
	// printf("Disabled while debugging");
	return 0;
}

Good Comment Habits

Comments that are too long or that restate obvious code add noise rather than clarity, and comments left unchanged after the code around them changes become actively misleading -- treat stale comments as a bug to fix.

Example: Good Comment Habits

c
#include <stdio.h>
int main() {
	int x = 5; // avoid: "sets x to 5" -- explain reasoning instead
	printf("%d", x);
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Nesting /* */ comments, where the first */ ends the whole comment and the rest becomes broken code.
  2. Using // comments in a strict C89 compiler mode, where only /* */ is allowed.
  3. Leaving outdated commented-out code around that no longer matches what the program does.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.