×

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

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 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.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.