C Tutorial/Language/Comments

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Adding Comments

Comments in a C program have a starting point and an ending point.

Everything between those two points is ignored by the compiler.


<source lang="cpp">/* This is how a comment looks in the C language */</source>

Source code header comments

  1. At the beginning of the program is a comment block.
  2. The comment block contains information about the program.
  3. Boxing the comments makes them stand out.

The some of the sections as follows should be included.

  1. Heading.
  2. Author.
  3. Purpose.
  4. Usage.
  5. References.
  6. File formats. A short description of the formats which will be used in the program.
  7. Restrictions.
  8. Revision history.
  9. Error handling.
  10. Notes. Include special comments or other information that has not already been covered.


<source lang="cpp">/********************************************************

   * hello -- program to print out "Hello World".         *   
   *                                                      *   
   * Author:  FirstName, LastName                         *   
   *                                                      *   
   * Purpose:  Demonstration of a simple program.         *   
   *                                                      *   
   * Usage:                                               *   
   *      Runs the program and the message appears.       *   
   ********************************************************/  
   #include <stdio.h> 
   int main() 
   {  
       /* Tell the world hello */ 
       printf("Hello World\n");   
       return (0);
   }</source>
Hello World

The comments are enclosed in "/*...*/"

<source lang="cpp">#include <stdio.h> main(){

 printf("Hi \n");  /* This is the comments.*/

}</source>

Hi

The "//" is used as single line comment

<source lang="cpp">#include <stdio.h> main(){

   int i,j,k;
  
   i = 6;    
   j = 8;
   k = i + j;
  
   printf("sum of two numbers is %d \n",k); // end of line comment

}</source>

sum of two numbers is 14

Using Comments to Disable

Comments are ignored by the compiler.

You can use comments to disable certain parts of your program.


<source lang="cpp">#include <stdio.h>

int main(){

  /* printf("%15s","right\n"); */
  printf("%-15s","left\n");
  return(0);

}</source>

left