Let's learn java programming language with easy steps

Tuesday, 14 November 2017

How to Write Comments in C Programming


Comments in C Programming Language

Comments in C Language

We are going to discuss, How to write comments in c programming language with step-by-step with simple examples. Let's start...


What is the purpose of comments in program?

Basically comments are used for description of every line of code which is written so that the programmer and other user can understand the meaning of each line of code in program.

Comments are mostly used for documenting of code.

The particular area of comments where we used are totally ignored by the compiler and its output does not print on the console.


Types of Comments in C Language

There are two types of comments in c language and these are...

  1. Single Line Comments
  2. Multiple Line Comments

Single Line Comments in C Language

We can use double slash ( // ) for representing single line comments in c language.

Syntax:

//this is my first single line comment in c program

Single Line Comments Example

This is single line comments in c program with output.

#include<stdio.h>
#include<conio.h>
void main()
{
//Variables declaration for addition
int a = 40;
int b = 40;
int c;
clrscr();

printf("value of a : %d\n", a);
printf("value of b : %d\n", b);

c = a+b;
printf("value of c : %d", c);
getch();
}

Output: value of a : 40
             value of b : 40
             value of c : 80


Another Example of Single Line Comments in C

This is another single line comment example where we will use more than one single line comments. You can use any number of single line comments at any place.

//single line comments in c
#include<stdio.h>
#include<conio.h>
void main()//main method
{
int a = 30;//variable declaration
clrscr();
printf("value of a : %d", a);
getch();
}

Output: value of a : 30


Multiple Line Comments in C Language

We can use slash asterisk (/*......*/) for representing multiple line comments in c language.

Syntax:

/* This is my 
      first multiple line comments
      in c language. */

            or

/* multiple line comments */

Multiple Lines Comments Example

This is multiple line comments example in c language.

#include<stdio.h>
#include<conio.h>
void main()/* main function */
{
/* variable declaration 
     a variable
     b variable 
     c variable */
int a = 40;
int b = 20;
int c;
clrscr();

c = a+b;
printf("addition of two number : %d ", c);
getch();
}

Output: addition of two number : 60

We cannot define comments into another comments i.e nested comments are not allowed in c.

For example:

#include<stdio.h>
#include<conio.h>
void main()
{
/* 
   /* nested comments not allowed */

*/
printf("nested not allowed");
getch();
}

Output: compile time error

Here we discussed, What is comments in c and the advantages of using comments in programming and how to write comments in c programming.

Learn how to run first program in c







Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate