×

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

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

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.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

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

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

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

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

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

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.