×

Commenting in C

Commenting in C

Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that is widely used for the documentation. They are the part of the code snippets which will directly be ignored by the compiler as they have no programmatical use in that particular application.

They are the type of concept that makes every programming code and application more readable and efficient to the programmer or a user and helps the programmer by explaining the logic behind the code.

It is said that a well documented program is a good practice for every developer and also makes the debugging more easier as anyone can find the commented section and what function it does.

It can be considered the sentence that only a programmer reads and the compiler does not, which is the explanation or the annotation of the main code. Neither the compiler nor the interpreter executes or reads the commented part, and it will directly be ignored by the computer.

Comments can be a detailed or precise description of the source code of a program or explanations.

There are two types of comments used in the C programming language.

  1. Single line comments
  2. Multi line comments

Single line comments: They are the type of comments that use double slash, represented as following “//” to comment on a specific part of any program or application.

E.g.:

 #include <stdio.h>
 int main() 
 { 
 int num1, num2;  //integer data type
 int sum, sub, mult, mod;  //integer data type
 float div;  //float data type
 printf("Input any two numbers separated by comma : ");//user input 
 scanf("%d,%d", &num1, &num2); 
 sum = num1 + num2;  //addition of numbers
 sub = num1 - num2;  //subtraction of numbers
 mult = num1 * num2;  //multiplication of numbers
 div = (float)num1 / num2;  //division of numbers
 printf("The sum of the given numbers : %d\n", sum); 
 printf("The difference of the given numbers : %d\n", sub); 
 printf("The product of the given numbers : %d\n", mult); 
 printf("The quotient of the given numbers : %f\n", div); 
 return 0;  
 } 

Output:

 Input any two numbers separated by a comma: 2, 4
 The sum of the given numbers: 6
 The difference of the given numbers: -2
 The product of the given numbers: 8
 The quotient of the given numbers: 0.500000 

Multi line comments: They are the type of comments that start and end with a symbol including a slash with an asterisk represented in the following manner “/*.... */”. This type of commenting slash can be placed anywhere within the code or application, and the sentences in between the slashes will be commented. It can be filled with any number of characters and any number of lines. It should be sure that the closing of multi line comments should be done correctly else the whole program will throw an error.

 /*Comment starts
 continues
 continues
 ...
 Comment ends*/ 

E.g.:

 #include<stdio.h>
 int main()
 {
 /*
 Print
 Message
 as an Output
 */
 printf("Welcome to Tutorials and Examples\n");
 printf("I love C tutorials");
 return 0;
 /* this will give an output */
 } 

Output:

 Welcome to Tutorials and Examples
 I love C tutorials 

You can also create a comment that displays at the end of a line of code. But generally, it’s a better practice to put the comment before the line of code.

When are comments in programming necessary tools?

  1. If no comments regarding the program's features are given, a person reading a big code will be perplexed.
  2. Comments are a method to put additional details into a code to make it more understandable.
  3. If comments are not included in an extensive code base, the reader will be perplexed. To make code more intelligible, comments might describe an algorithm.
  4. If code is to be reused over a lengthy period of time, comments might be helpful for one as well.
  5. A good programmer who produces codes that are only understood by the machine is better than the one who generates codes that were only comprehended by the machine.
  6. As a result, including comments to your code is highly recommended as a good programming technique. Comments do not affect programming since they are overlooked by the compiler.
  7. Comments help the developer understand the logic/algorithm of the code if he revisits it after a long time.

Related Topics

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.