C Comments
In this page:
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
#include <stdio.h>
int main() {
// This line explains the printf below
printf("Hello");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <stdio.h>
int main() {
/* This comment
spans multiple lines */
printf("Hello");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <stdio.h>
int main() {
// Using 1024 because memory is measured in kibibytes here
int kilobyte = 1024;
printf("%d", kilobyte);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <stdio.h>
int main() {
printf("Active code");
// printf("Disabled while debugging");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <stdio.h>
int main() {
int x = 5; // avoid: "sets x to 5" -- explain reasoning instead
printf("%d", x);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers