Comments are explanatory statements that you can include in your C code. These comments help anyone reading the source code understand your logic and intent.
The C compiler ignores everything inside a comment, meaning it won't be executed as code. This makes comments perfect for explaining what your code does, leaving notes for yourself or other developers, and even temporarily disabling parts of your code during debugging.
C supports two primary types of comments:
Single-line comments start with two forward slashes //.
Any text placed after // on the same line is ignored by the compiler. This style of comment was introduced in C99 (borrowed from C++) and is now universally supported by modern C compilers. They are incredibly useful for brief, inline explanations.
#include <stdio.h>int main() { // This is a single-line comment. It explains the next line. printf("Hello, World!\n");
int age = 25; // This is an inline comment explaining the variable printf("Age: %d\n", age);
return 0; }
Multi-line comments, also known as block comments, start with /* and end with */.
The compiler ignores any text between these two markers, regardless of how many lines it spans. This makes them ideal for longer explanations, file headers, or commenting out large chunks of code when testing.
#include <stdio.h>/* This is a multi-line comment. It can span multiple lines without needing slashes at the start of each line. It's great for detailed explanations. */ int main() { int x = 10; int y = 20;
/* We are going to calculate the sum of x and y and print it to the console. */ printf("Sum: %d\n", x + y);
return 0; }
Which symbols are used to start and end a multi-line comment in C?