Comments in C programming Language | Single line Comments and Multi-line Comments in C language
Introduction:
In our previous article, We discussed the “Datatypes in C Language”. In todays article, We will look into the Comments in C programming and different types of comments.
Comments in C Language:
Comments are the sequence of Characters which doesn’t have any meaning to computer. In other words Compiler ignores comments, In fact, comments will not go to Translation stage which are Removed by C language Pre-processor while Preprocessing.
- How pre-processor remove comments learn more at – Compilation Stages in C language.
Comments are used to explain how the block of code is implemented. Usually, programmers write a short explanation about the program, So that source code will be more readable and understandable to the other programmers and colleagues.
Commenting code is one of the most important coding practice because it helps others to understand what you’re trying to do. Comments Increases the Readability of program.
📢. Comments are Useful Description about the program logic. Comments in C are Ignored by the C Compiler.
Many times, Programmers work on different projects and there is good possibility of forgetting the logic which is written by us as well. So In a way comments also helps us to recall the logic of the program.
We have two types of comments in C programming, They are.
- Single Line Comments.
- Multi line Comments.
Let’s see how to create single line comments and multi-line comments.
Single Line Comments in C Language :
We can create a Single Line comment using the Two forward slashes ( //)
The Single Line Comments Starts with Two Forward Slashes ( //) and Ends with Newline Character ( '\n') character.
As the name suggests, The single line comments Maximum length is One Line only.
Syntax of Single Line Comment:
1 2 |
// Your Comment goes here int n; |
As you can see from above syntax, "Your comment goes here" is a comment and will be ignored by the C Compiler.
The int n; is not a comment and It is a valid C statement, Which will be executed by the compiler.
Example Program to demonstrate Single Line Comments:
Let’s write a program to calculate the sum of the two integer numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// C Program to calculate the Sum of the two numbers. #include<stdio.h> int main() { // Taking three variables to a, b, and sum int a, b, sum; // Here I am taking Input from user printf("Enter a and B : "); // scanf reads users values and Stores into a and b scanf("%d%d", &a, &b); // Using the Addition operator ('+') on 'a' and 'b' sum = a + b; // Finally, Printing the sum of 'a' and 'b' values printf("Sum is : %d \n", sum); return 0; } |
Program Output:
1 2 3 4 5 6 7 8 |
// Compiling the program using GCC $ gcc comments.c // Running the program $ ./a.out Enter a and B : 100 500 Sum is : 600 $ |
We have used the single line comments extensively in the above program, Starting with
1 |
// C Program to calculate the Sum of the two numbers. |
Then at each line we added a single-line comment demonstrating the meaning of that line of code.
In the above example program, All the lines which are started with two forward slashes ( //) are Single Line comments, Which will be ignored by the compiler.
Multi-Line Comments in C Language :
We can create the multi line comments using the combination of backslash ( /) and Asterisk or Star ( *) characters.
The Multi-line Comments are starts with Forward slash and asterisk (star) combination ( /* ) and Ends with Asterisk and forward slash Combination ( */). Everything Written in between these tokens are treated as Comments.
Syntax of Multi-Line comments:
1 2 3 4 5 |
/* ....... ....... ....... */ |
As you can see from the syntax, The multi line comments starts with /* then you can continue the comments as many lines as you want. You can end the comment by using the */ character combination.
The multi line comments are also ignored by the compiler, Comments are only useful for programmers.
Example Program for the Multi-Line Comments in C :
We are going to create two multi line comments in this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* C Program to calculate the Addition of two numbers. Program should take two integers from user and provide the output on the console */ #include<stdio.h> int main() { /* We are going to take three variables 'a', 'b' and 'sum' Then we will take the two integers from user and store them in 'a' and 'b' variables After that we will calculate the addition of 'a' and 'b' then store the results in 'sum' variables Finally We will print the 'sum' value on the console. */ int a, b, sum; printf("Enter a and B : "); scanf("%d%d", &a, &b); sum = a + b; printf("Sum is : %d \n", sum); return 0; } |
Program Output:
1 2 3 4 5 6 7 8 |
// Compiling the program $ gcc multi-line-comment.c // Executing the program $ ./a.out Enter a and B : 44 33 Sum is : 77 $ |
In the above program, Everything which is in-between the /* and */ characters is a Multi line comment.
We have two multi line comments in above program those comments are
1 2 3 4 5 |
/* C Program to calculate the Addition of two numbers. Program should take two integers from user and provide the output on the console */ |
and second multi-line comment is
1 2 3 4 5 6 |
/* We are going to take three variables 'a', 'b' and 'sum' Then we will take the two integers from user and store them in 'a' and 'b' variables After that we will calculate the addition of 'a' and 'b' then store the results in 'sum' variables Finally We will print the 'sum' value on the console. */ |
Conclusion:
In this article, We have discussed about the comments in programming, What is use of comments and what are the different types of comments in C Programming Language.
1 Response
[…] Pre-Processor Directives also removes the Comments. […]