Monday, November 4, 2013

Comment philosophy...


We had seen earlier that, comments are the statements ignored by the C compiler. Now, we are going to learn more about comments. The following program illustrates the usefulness of comments.

Program 7:
/* The message program */
/* Created by :Mysterio Date: 20 November 2013 */
#include <stdio.h>
#include <conio.h>
void main()
{
          /* Clear the screen.*/
clrscr();
/* Display the message on the screen. */
printf(“\n Fear less, Hope more;\
           \n Whine less, Breathe more;\
           \n Talk less, Say more;\
           \n Hate less, Love more;
           \n And all good things are yours.”);
/*Pause the program output.*/
getch(); /*End of the program. */
}

We know that, all the statements enclosed in between /* and */ are comments. Comments are useful to make our program more readable and easy to understand. Remember that comments are for programmer and not for compiler. Adding comments into the program does not increase the size of executable file. In the above program, we split the printf() over the multiple lines by appending “\” at the end of each line. It tells the compiler that what is on the next line is the continuation of the previous line. The printf() function  can be used to print the strings in various ways. Our forthcoming topic “Printing strings in various way…” will explain this concept.

Warning: You cannot nest comments i.e. one comments inside another.

So, the following comment is invalid.
/*Program to find IQ /*Created by Genius */ Date :21 Nov 2013. */

Tip: We are using Turbo C++ compiler to write our C programs. So, we can also write single line comment as follows. But it is not valid in pure Turbo C compiler.

//Hey! I am C++ style comment.

Note: You can place comments anywhere in your program.
The following program illustrates this.

Program 8:         
#include <stdio.h>
#include <conio.h>
void main(/* Program execution begins here. */)
{
     clrscr(/* use me to clear the screen. */);
     printf(“\n I %c C! And you?”,3);
    getch(/* use me to pause program display.*/);
}

Warning: Don’t write comment in angle bracket after #include statement.
E.g.
#include <stdio.h /*Standard I/O header file. */> because, if you do so, the compiler assumes it as a file name and the error will occur.

You may be curious about the second pritf() statement. The %c format specifier is used to print the character corresponding to ASCII value on the right side. The ASCII value of “©” character is 3, so it will print the “©” character and so the output of second printf() will be:

I © C! And you?

No comments:

Post a Comment