- What multi-line (
/* */) comments are, and the block-comment syntax - When to prefer block comments over single-line
//comments - How to declare and use three integer variables
- How to read two integers with a single
scanfcall - How the addition operator
+works on integer operands - How to format output with
%dfor integers
Multi-line comments, and when to use them
A multi-line comment begins with /* and ends with */. Everything between the delimiters — across any number of lines — is ignored by the compiler. Unlike the single-line // comment, which ends at the newline, a block comment can span an arbitrary number of lines, or sit inside a single line, or even in the middle of a statement.
When should you use block comments instead of single-line comments? There are three common cases. First, when explaining a multi-line block of code — a comment block above a function or a group of related statements reads better as a single /* */ paragraph than as five separate // lines. Second, when temporarily disabling a chunk of code during debugging — wrap it in /* */ and it disappears. Third, when writing a header comment that names the program, its purpose, its author, and its date — the kind of thing that belongs at the top of the file.
A word of caution: block comments do not nest. If you write /* outer /* inner */ outer */, the comment ends at the first */, and the rest becomes active code — usually causing a compile error. This is one of the reasons many style guides prefer // for line-by-line comments; it avoids the nesting trap entirely. Use /* */ when you genuinely need a multi-line block, not as a default.
💡 In one line: /* ... */ starts a block comment that spans from /* to the next */. Use it for multi-line explanations, disabled code chunks, and header comments. Don't nest it.
The program adds two integers. It asks the user for both, reads them with a single scanf call, computes the sum, and prints it. Every logical step — the includes, the variable declarations, the input, the computation, the output — has its own block comment explaining its purpose. That's the exercise: document a real program thoroughly using multi-line comments.
Designing the addition program
The design has four decisions that matter:
| Decision | Choice | Reason |
|---|---|---|
| Variable type | int for all three | Integer addition is exact. For fractional input, use double and %lf. |
| Number of variables | a, b, sum | Two operands plus one result. Could reuse a for the sum, but naming the result clarifies intent. |
| Input method | scanf("%d %d", &a, &b) | Reads both integers in one call, separated by whitespace in the input stream. |
| Comment style | /* ... */ blocks | The assignment specifically asks for multi-line comments to document the purpose of every step. |
The name sum is descriptive — a reader sees immediately what the variable holds. Names like x, tmp, or res are shorter but less clear. For a variable whose purpose is "the sum of a and b," sum is the obvious name. Good naming is half of good commenting — the clearer the names, the less you need to explain in prose.
The scanf("%d %d", &a, &b) call reads two integers. The whitespace between the two %d specifiers is a "match any amount of whitespace" — it tolerates any combination of spaces, tabs, or newlines between the two numbers. So the user can type 3 5, 3 5, or even 3 then Enter then 5 on the next line. All work correctly. This is part of the standard's behavior for %d and similar numeric specifiers — leading whitespace before each numeric field is automatically skipped.
🎯 Key idea: block comments are best for documenting blocks of code — a function, a loop body, an input section. For a single-line remark, // is clearer. Match the comment style to the scale of what you're documenting.
Complete C Program
Program: add two integers, with multi-line comments on every step
/*
* Program: Addition of Two Numbers
* Purpose: Read two integers from the user, compute their sum,
* and print the result. Every step is documented
* with a multi-line comment to make the code's
* intent clear to a reader.
*/
#include <stdio.h>
int main(void) {
/*
* Declare three integer variables:
* a and b hold the two operands,
* sum holds the result of a + b.
*/
int a, b, sum;
/*
* Prompt the user to enter two integers.
* The \n after the prompt puts the user's
* input on a fresh line.
*/
printf("Enter two integers: \n");
/*
* Read the two integers into a and b.
* The & operator takes the address of each
* variable — scanf writes into the addresses,
* not into the values themselves.
*/
scanf("%d %d", &a, &b);
/*
* Compute the sum.
* The + operator performs integer addition.
* Since both operands are int, the result
* is also int, with no truncation.
*/
sum = a + b;
/*
* Print the result.
* %d is the format specifier for int.
* \n at the end puts the next prompt
* (or the shell prompt) on a fresh line.
*/
printf("Sum = %d\n", sum);
return 0;
}
Every logical step is documented by a block comment. The header comment at the top names the program, states its purpose, and explains the overall approach. Each block in the body — declarations, input, computation, output — has its own comment explaining what it does and why.
This is more commenting than most real-world code needs. In production, you'd comment the tricky bits and let the obvious ones speak for themselves. But for a learning exercise — and for the specific assignment asking you to "document the purpose of every step" — this level of detail is exactly right.
And here is a tighter version, closer to production style, that comments only the parts that aren't self-evident:
Variant: lighter commenting, production style
/*
* Program: Addition of Two Numbers
* Reads two integers from stdin, prints their sum.
*/
#include <stdio.h>
int main(void) {
int a, b, sum;
printf("Enter two integers: \n");
scanf("%d %d", &a, &b);
/* Integer addition — both operands are int, no truncation. */
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
The overall program header states what the program does in two lines. The declaration and input are self-explanatory, so they're not commented. Only the addition — the part that could confuse a beginner who's used to overflow, or who wonders whether int + int might truncate — gets a comment. That's the judgment call good developers make: comment what isn't obvious, leave the rest alone.
Program Output
Here is typical output for the sum of 3 and 5:
Enter two integers:
3 5
Sum = 8
The prompt is printed, then the user types 3 5 on the next line. Both integers are read in one scanf call. The program computes 3 + 5 = 8 and prints the result. Correct.
For negative numbers:
Enter two integers:
-10 4
Sum = -6
Integer addition handles negatives naturally — no special case needed. -10 + 4 = -6. Correct.
For two large numbers:
Enter two integers:
1000000 2000000
Sum = 3000000
1,000,000 + 2,000,000 = 3,000,000. Well within the range of int (which can hold up to about 2.1 billion on typical systems). Correct.
For inputs that exceed int range:
Enter two integers:
2000000000 2000000000
Sum = -294967296
The sum 2,000,000,000 + 2,000,000,000 = 4,000,000,000 exceeds the maximum value of a 32-bit int (2,147,483,647). The result wraps around to -294,967,296 due to integer overflow. This is undefined behavior in the C standard — the compiler is technically allowed to do anything. In practice, it wraps around to a negative number because of two's complement representation. But you shouldn't rely on this; overflow is a real bug source in production code.
To handle large sums correctly, use long long (guaranteed at least 64 bits) or check for overflow before adding. The correct output would be 4,000,000,000, which fits in long long.
🔍 Try this: change int to long long and %d to %lld in both scanf and printf, then rerun the overflow example. The answer will be 4,000,000,000 as expected. This demonstrates the importance of choosing the right type for the range of values you expect.
Step-by-Step Explanation
Let's walk through the program line by line, focusing on what each block does and why it's written the way it is.
1. The header comment block
The program opens with a /* ... */ block that names the program, states its purpose, and explains the approach. This is a common convention: every source file should start with a comment stating what it does and (optionally) who wrote it, when, and any assumptions it makes. In professional code, this block would typically also include a license, a version, and any dependencies.
The opening /* can be followed by a newline, or (as here) placed immediately before the content. Both are valid. The closing */ ends the comment — everything after is active code again. The asterisks on each line inside the block are a stylistic convention — they're part of the comment text and have no special meaning.
2. The #include directive
#include <stdio.h> — as in the previous assignment, this is a preprocessor directive that pastes the contents of the standard I/O header into the source before compilation. Without it, the compiler wouldn't know what printf and scanf are.
3. The declaration block
int a, b, sum; — declares three integer variables in one statement. a and b hold the two operands; sum holds their sum. All are int, which on most modern systems is 32 bits — enough for about ±2.1 billion. If you need larger values, use long long.
The comment block above the declaration explains what each variable holds. That's helpful because a reader encountering sum for the first time can see its role without tracing through the rest of the code.
4. The input prompt
printf("Enter two integers: \n"); — the prompt ends with \n, so the user's input appears on the next line. This is slightly different from assignment 1, where the prompt didn't have \n. Both are fine; the choice depends on whether you want the input on the same line as the prompt (drop the \n) or on the next line (include it).
5. Reading both integers
scanf("%d %d", &a, &b); — reads two integers in one call. The format string "%d %d" tells scanf to expect two integer values separated by whitespace (spaces, tabs, or newlines). The user types 3 5, and scanf assigns 3 to a and 5 to b.
Two points to note. First, both &a and &b are addresses — scanf needs addresses so it can write into the variables. Second, the whitespace between %d and %d is significant in the sense that it matches "any amount of whitespace in the input" — which is what allows the user to type 3 5 with multiple spaces, or to put the two numbers on separate lines. It's a flexible match.
6. Computing the sum
sum = a + b; — reads the values of a and b, adds them with the + operator, and stores the result in sum. Since all three variables are int, the arithmetic is done in int precision — no implicit conversions, no truncation (unless overflow occurs, which is a separate concern).
The comment above the assignment explains that both operands are int and the result is int. That's useful for a beginner who might wonder whether + on integers could produce a float (it doesn't — the types of the operands determine the type of the result).
7. Printing the result
printf("Sum = %d\n", sum); — prints "Sum = ", then the value of sum as a decimal integer, then a newline. The %d format specifier matches the int type of sum exactly. If you used %f (the specifier for float), the output would be garbage — printf would reinterpret the bits of the int as a float, producing a nonsensical value. Always match the specifier to the type.
8. Why three variables instead of two?
You could compute the sum with only two variables: a = a + b; would overwrite a with the sum, and then you'd print a. That would work, but it destroys the original value of a — which might not matter here (we don't use it afterward), but is bad practice in general. Using a separate sum variable preserves both operands, making the code easier to reason about and modify later.
The choice between "reuse a variable" and "introduce a new one" depends on context. In a tight loop where you're accumulating a running sum, you'd reuse a single accumulator variable. In a one-off calculation, clarity wins — use a new variable. Our code uses a new variable, which is the clearer choice here.
9. The block comment style
Each block comment in the program is formatted with the opening /*, one or more lines of explanation, and a closing */. The formatting varies — the first comment has /* on its own line, while others put content immediately after. Both are valid; pick a style and be consistent. A common convention is to align the asterisks vertically and end with */ on its own line, but that's aesthetic — it doesn't affect the compiler.
10. When not to use block comments
Block comments don't nest. If you have a function with a /* */ comment inside it and you want to temporarily disable the whole function, wrapping it in another /* */ doesn't work as intended — the inner */ closes the outer comment, and the rest of the function becomes live code. To disable a function that already contains block comments, either convert the outer comment to #if 0 ... #endif (a preprocessor trick), or use // on each line. This is one more reason to prefer // for ordinary comments and reserve /* */ for header blocks and large disabled regions.
11. Overflow and integer limits
As the output example showed, adding two large integers can overflow. On typical 32-bit int systems, the maximum value is 2,147,483,647. Adding 2,000,000,000 + 2,000,000,000 gives 4,000,000,000, which doesn't fit — the arithmetic wraps around to a negative number. This is a real bug in production code that handles large values, though for a beginner assignment with small inputs, it's not a concern.
To handle large sums, use long long and %lld. The long long type is guaranteed to be at least 64 bits, capable of holding values up to about 9.2 quintillion. If you're writing a general-purpose addition program, that's the type to use.
🎯 Key idea: block comments are great for explaining groups of related statements — a declarations section, an input section, a computation section. They're not great for single-line remarks, and they don't nest. Use them where they fit, and switch to // elsewhere. Good commenting is about matching the tool to the job.
Frequently Asked Questions
1. What's the difference between a block comment and a single-line comment?
A single-line comment starts with // and runs to the end of the line. A block comment starts with /* and ends with */ — it can span one line or many. Functionally, both are ignored by the compiler. Stylistically, // is more common for short remarks; /* */ is used for multi-line explanations, header blocks, and disabled code. Both work; pick based on the size of what you're documenting.
2. Can block comments be nested?
No. The C standard says block comments don't nest. If you write /* outer /* inner */ outer */, the comment ends at the first */, and everything after is active code — probably a compile error. To "comment out" code that already contains block comments, use #if 0 ... #endif, or convert each line to a // comment. This is one reason many style guides prefer // for ordinary comments and reserve /* */ for headers.
3. Why does scanf("%d %d", &a, &b) work with both "3 5" and "3\n5"?
Because the whitespace in the format string matches "any amount of whitespace in the input." After reading the first %d, scanf skips any whitespace (spaces, tabs, newlines) before reading the second %d. So whether the user types both numbers on one line or on two lines, scanf reads them correctly. This is part of the standard's behavior for %d and similar numeric specifiers — leading whitespace is automatically skipped.
4. What happens if the user types a letter instead of a number?
Undefined behavior, but in practice on most compilers: scanf leaves a and b unchanged (still uninitialized), returns 0 (meaning "zero items read"), and the program continues with garbage values in a and b. That's why you should always check scanf's return value: if (scanf("%d %d", &a, &b) != 2) { /* handle error */ }. For a beginner program with friendly input, this check is often omitted; for production code, it's essential.
5. Why does the program say "Sum = -294967296" for large inputs?
Because of integer overflow. On most 32-bit int systems, the maximum int value is 2,147,483,647. Adding 2,000,000,000 + 2,000,000,000 = 4,000,000,000 exceeds this, and the result wraps around to a negative number. The standard calls this undefined behavior — the result is technically unpredictable, though in practice it usually wraps. To handle large sums, use long long and %lld. This is a common trap, and the reason type selection matters.
6. Is there any performance cost to using comments?
No. Comments are stripped out by the preprocessor before the compiler sees the code. The compiled binary contains no trace of them — they don't affect program size, startup time, or execution speed. The only cost of commenting is the time you spend writing them, which pays back many times over in maintainability. Even a program with millions of comments compiles to exactly the same binary as the same program with no comments (assuming nothing else differs).
💡 Tip: once this works, try the variant with lighter commenting. Notice how a well-named variable (like sum) and a self-explanatory statement (sum = a + b;) need almost no comment, while the addition with its potential for overflow does. Good naming reduces the need for comments — aim for code that speaks for itself, and reserve comments for the parts that don't.