A comment is very important in coding. Assume you have a very big team with a lot of loaded code in the application. There will be many functions and logic, which after a year or month will be not remembered by humans. So to know what every function or any stuff important it’s required to add a comment above it. In this tutorial we will learn how to do “Comments in c //” and type with examples.
Types of comments in c
There are 2 types of comments in c language.
- Single line Comments
- Multiline Comments
This tutorial you will learn both type comment with tips and some interview questions.
Syntax
Simple syntax for comments.
/* comments */
or multiline
/* * comment here */
or
/* comment here */
Single line Comments Example
Here is an example of comments in c where the First 2 statement in the code is a comment. Here simple Hello world example in C with single line comment.
#include<stdio.h> /* Author - Eyehunts */ int main() { printf("hello world"); return 0; }
Multiline Comments Example
Comment can be split into multiple lines in C. Which we called multiline comments.
#include<stdio.h> /* Author - Eyehunts Create date : 05/01/2018 */ int main() { printf("hello world"); return 0; }
Important Tips & Points
Here are some useful Tips and point on when doing comments in c code.
- A comment is not necessary but a good practice to show the use of statements in the program.
- You can write a comment in code as many you want.
- No need to write so many comments, it suggested being specific and limited.
- Can write any place in the program.
- Normal language rules not applied in a comment like small, uppercase, etc.
Question: What are the Advantages of using comments in programming?
Answer: A Advantage of comments in c programming is when a team building a very big application that time a well-commented code is easy to understand to other team members. And also for you when you are going for some changes in code.
Sometimes you don’t need only for describing code. It can use a show author name and date of created at stating in code, same as above example.
Question: Where I can write a comment in the C program?
Answer: You can write a comment at any place in the program, for example before the statement or after the statement, or within the statements. See the below example.
/* Adding 2 number function (Before staetment) */ int addNumbers(int a,int b) { int result; result = a+b; // Adding number (within statement) return result; } /* after staetment */
This tutorial is for the beginner. Do comment if you have any doubts or questions.
Note: This example (Project) is developed in Dev-C++ and Version 5.11
Download link for IDE – Dev-C++