The C Programming Language

Comments in C 18


Single Line Comments

 

Single line comments span over one line only and are ignored by the compiler.
In C they can be done with a double Slash.
In this case we do NOT need a semicolon at the end of the line.

They can either be above a part of the code...


Example:

#include <stdio.h>

int main()
{
    // This is a single line comment

    printf("%s", "Hello, World!");

    return 0;
}

Or they can be in the same line of the code...

Example:

#include <stdio.h>

int main()
{
    printf("%s", "Hello, World!");     // This is a single line comment

    return 0;
}

 

Multi Line Comments

 

Multi line comments span over two or more lines and are ignored by the compiler.
In C they start with a slash and an asterisk and end with an asterisk and a slash.
In this case we do NOT need a semicolon at the end of the line.


Example:

#include <stdio.h>

int main()
{
    /*

    This is

    a multi

    line comment

    */

    printf("%s", "Hello, World!");

    return 0;
}

Single Line comments and Multi Line Comments can also be used together.

 

Example:

#include <stdio.h>

int main()
{
    /*

    This is

    a multi

    line comment

    */

    printf("%s", "Hello, World!"); // And this is a single line comment

    return 0;
}

How do you rate this article?


2

0

Cheetah Faith
Cheetah Faith

Crypto enthusiast, programming enthusiast, music lover and animal lover from Germany


Programming Tutorials
Programming Tutorials

I will share short and easy lessons, exercises and examples about programming languages here

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.