Programming in C: Unit I (b): Introduction to C

Using Comments

C Program

Many a time the meaning or the purpose of the program code is not clear to the reader. Therefore, it is a good programming practice to place some comments in the code to help the reader understand the code clearly.

USING COMMENTS

Many a time the meaning or the purpose of the program code is not clear to the reader. Therefore, it is a good programming practice to place some comments in the code to help the reader understand the code clearly. Comments are just a way of explaining what a program does. It is merely an internal program documentation. The compiler ignores the comments when forming the object file. This means that the comments are non-executable statements. C supports two types of comments.

• // is used to comment a single statement. This is known as a line comment. A line comment can be placed anywhere on the line and it does not require to be specifically ended as the end of the line automatically ends the line.

Programming Tip: Not putting the */ after the termination of the block comment is a compiler error.

 • /* is used to comment multiple statements. A /* is ended with */ and all statements that lie within these characters are commented. This type of comment is known as block comment.

Note that commented statements are not executed by the compiler. Rather, they are ignored by the compiler as they are simply added in the program to make the code uple who understandable by the programmer to other people who read it. It is a good habit to always put a comment at the top of a program that tells you what the program does. This will help in defining the usage of the program the moment you open it.

Ppppppppppppppppppppppppp

Commented statements can be used anywhere in the program. You can also use comments in between your code to explain a piece of code that is a bit complicated. The code given below shows the way in which we can make use of comments in Our first program.

/* Author: Reema Thareja

Description: To print 'Welcome to the world of C' on the screen */

#include <stdio.h>

int main()

{

printf("\n Welcome to the world of C ");

// prints message

return 0; // returns a value 0 to the operating system

}

Output

Welcome to the world of C

Since comments are not executed by the compiler, they do not affect the execution speed and the size of the compiled program. Therefore, using comments liberally in your programs aid other users in understanding the operations of the program as well as in debugging and testing.

Programming in C: Unit I (b): Introduction to C : Tag: : C Program - Using Comments