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.


/* This is how a comment looks in the C language */

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.


/********************************************************           
    * 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);
    }
Hello World

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

#include <stdio.h>
main(){
  printf("Hi \n");  /* This is the comments.*/
}
Hi

The "//" is used as single line comment

#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
}
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.


#include <stdio.h>
 
int main(){
  
   /* printf("%15s","right\n"); */
   printf("%-15s","left\n");
   return(0);
}
left