0% found this document useful (0 votes)
17 views7 pages

Comments in C

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Comments in C

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

6/16/24, 10:57 AM Comments in C

Comments in C
Using comments in a C program
increases the readability of the code.
You must intersperse the code with
comments at appropriate places. As
far as the compiler is concerned, the
comments are ignored. In C, the
comments are one or more lines of
text, that the compiler skips while
C Programming Tutorial
building the machine code.

C - Home The comments in C play an


important part when the program
C - Overview needs to be modified, especially by
somebody else other than those who
have written it originally. Putting
C - Features
comments is often not given
importance by the programmer, but
C - History
using them effectively is important to
improve the quality of the code.
C - Environment Setup

C - Program Structure Why to Use Comments in C


Programming?
C - Hello World Any programming language,
including C, is less verbose as
C - Compilation Process compared to any human language
like English. It has far less number of
C - Comments keywords, C being one of the
smallest languages with only 32
C - Tokens keywords. Hence, the instructions in
a C program can be difficult to
understand, especially for someone
C - Keywords
with non-programming background.

C - Identifiers The C language syntax also varies


and is also complicated. Usually, the
C - User Input programmer tries to add complexity
to optimize the code. However, it

https://www.tutorialspoint.com/cprogramming/c_comments.htm 1/7
6/16/24, 10:57 AM Comments in C

makes the code hard to understand.


Comments provide a useful
explanation and convey the
intention behind the use of a
particular approach.

For example, take the case of the ?:


operator in C, which is a shortcut for
the if-else statement.

So, instead of the following code −

if (a % 2 == 0){
printf("%d is Even\n", a);
} else {
printf("%d is Odd\n", a);
}

One can use the following statement


(a % 2 == 0) ? printf("%d is Even\n", a) :

Obviously, the second method is


more complicated than the first. If
useful comments are added, it makes
easier to understand the intention
and logic of the statement used.

Types of Comments in C
In C, there are two types of
comments −

Single-line comments
Multi-line comments

Single-line Comment in C

The C++-style single-line comments


were incorporated in C compilers with

https://www.tutorialspoint.com/cprogramming/c_comments.htm 2/7
6/16/24, 10:57 AM Comments in C

C99 standards. If any text begins


with the // symbol, the rest of the
line is treated as a comment.

The text followed by a double oblique


or forward slash [//] in a code is
treated as a single-line comment. All
that text after // is ignored by the C
compiler during compilation. Unlike
the multi-line or block comment, it
need not be closed.

Syntax of Single-line C
Comment

//comment text

The // symbol can appear anywhere.


It indicates that all the text following
it till the end of the line is a
comment. The subsequent line in the
editor is again a place to write a valid
C statement.

Example: Single-line
Comment in C

Take a look at the following program


and observe how we have used
single-line comments inside its main
function −

/* Online C Compiler and Editor */


#include <stdio.h>
#include <math.h>

/*forward declaration of function*/


float area_of_square(float);

float area_of_square(float side){


float area = pow(side,2);
return area;

https://www.tutorialspoint.com/cprogramming/c_comments.htm 3/7
6/16/24, 10:57 AM Comments in C

// main function - entire line is a comment


int main(){

// variable declaration (this comment is


float side = 5.50;

float area = area_of_square(side); // c


printf ("Side = %5.2f Area = %5.2f", side

return 0;
}

Output

When you run this code, it will


produce the following output −

Side = 5.50 Area = 30.25

Multi-line Comment in C

In early versions of C (ANSI C), any


length of text put in between the
symbols /* and */ is treated as a
comment. The text may be spread
across multiple lines in the code file.
You may call it a multi-line comment.
A block of consecutive lines is treated
as a comment.

Syntax of Multi-line C
Comment

The general structure of a multi-line


comment is as follows −

/* The comment starts here


Line 1
Line 2

https://www.tutorialspoint.com/cprogramming/c_comments.htm 4/7
6/16/24, 10:57 AM Comments in C

…..
…..
Comment ends here*/

For example −

/*
Example of a multi-line comment
program to print Hello World
using printf() function
*/

Obviously, since the comments are


ignored by the compiler, the syntax
rules of C language don't apply to the
comment text.

Comments can appear anywhere in


a program, at the top, in between the
code, or at the beginning of a
function or a struct declaration, etc.

Example: Multi-line Comment


in C

In this example, we have a multi-line


comment that explains the role of a
particular user-defined function used
in the given code −

/* program to calculate area of square */

/* headers */
#include <stdio.h>
#include <math.h>

/* forward declaration of function */


float area_of_square(float);

/* main function */
int main(){

https://www.tutorialspoint.com/cprogramming/c_comments.htm 5/7
6/16/24, 10:57 AM Comments in C

/* variable declaration */
float side = 5.50;

/* calling function */
float area = area_of_square(side);
printf("Side = %5.2f Area = %5.2f", side

return 0;
}

/* User-defined function to calculate


the area of square. It takes side as the ar
and returns the area */

float area_of_square(float side){


float area = pow(side, 2);
return area;
}

Output

When you execute the code, it will


produce the following output −

Side = 5.50 Area = 30.25

While inserting a comment, you must


make sure that for every comment
starting with /*, there must be a
corresponding */ symbol. If you
start a comment with /* and fail to
close it, then the compiler will throw
an error.

Note: A blocked comment or multi-


line comment must be put inside /*
and */ symbols, whereas a single-
line comment starts with the //
symbol and is effective till the end of
the line.

https://www.tutorialspoint.com/cprogramming/c_comments.htm 6/7
6/16/24, 10:57 AM Comments in C

Placing comments in a program is


always encouraged. Programmers
usually avoid the practice of adding
comments. However, they can
sometimes find it difficult to debug
and modify their code if it is not
properly commented. Comments are
especially vital when the
development is done in collaboration.
Using comments effectively can be of
help to all the members of a team.

Even though comments are ignored


by the compiler, they should be clear
in meaning and concise. Whenever
the code needs modification, the
reason, the timestamp, and the
author should be mentioned in
comments.

https://www.tutorialspoint.com/cprogramming/c_comments.htm 7/7

You might also like