C Comments

Comments are notes written inside the code for humans. The compiler ignores them. We use comments to explain what the code does, or to stop one line from running for a while.

Single line comment

It starts with // and goes until the end of the line.

#include <stdio.h>

int main(void) {
    // This line is a comment
    printf("Hello\n");   // comment after a statement
    return 0;
}

Output:

Output of the above program is

Hello

Multi line comment

It starts with /* and ends with */. It can be on many lines.

#include <stdio.h>

/* This program prints a message.
   Written for the C tutorial. */
int main(void) {
    printf("Learning C\n");
    return 0;
}

Output:

Output of the above program is

Learning C

Comment out a line of code

#include <stdio.h>

int main(void) {
    printf("First\n");
    // printf("Second\n");
    printf("Third\n");
    return 0;
}

Output:

Output of the above program is

First
Third

Note: You cannot put one /* */ comment inside another. Also, do not write comments which only repeat the code, like “add 1 to i”. Write why you did it.

Next: C Keywords.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.