C Comments

In the previous tutorial you learned to write your first C program. Now, let's learn about C comments.

Tip: We are introducing comments early in this tutorial series because, from now on, we will be using them to explain our code.

Comments are hints that we add to our code, making it easier to understand.

Comments are completely ignored by C compilers.

For example,

#include <stdio.h>

int main() {

  // print Hello World to the screen
  printf("Hello World");
  return 0;
}

Output

Hello World

Here, // print Hello World to the screen is a comment in C programming. The C compiler ignores everything after the // symbol.

Note: You can ignore the programming concepts and simply focus on the comments. We will revisit these concepts in later tutorials.


Single-line Comments in C

In C, a single line comment starts with // symbol. It starts and ends in the same line. For example,

#include <stdio.h>

int main() {

  // create integer variable    
  int age = 25; 
 
  // print the age variable
  printf("Age: %d", age);

  return 0;
}

Output

Age: 25

In the above example, we have used two single-line comments:

  • // create integer variable
  • // print the age variable

We can also use the single line comment along with the code.

int age = 25;  // create integer variable

Here, code before // are executed and code after // are ignored by the compiler.


Multi-line Comments in C

In C programming, there is another type of comment that allows us to comment on multiple lines at once, they are multi-line comments.

To write multi-line comments, we use the /*....*/ symbol. For example,

/* This program takes age input from the user
It stores it in the age variable
And, print the value using printf() */

#include <stdio.h>

int main() {

  // create integer variable    
  int age = 25; 
 
  // print the age variable
  printf("Age: %d", age);

  return 0;
}

Output

Age: 25

In this type of comment, the C compiler ignores everything from /* to */.

Note: Remember the keyboard shortcut to use comments:

  • Single Line comment: ctrl + / (windows) and cmd + / (mac)
  • Multi line comment: ctrl + shift + / (windows) and cmd + shift + / (mac)

Prevent Executing Code Using Comments

While debugging there might be situations where we don't want some part of the code. For example,

In the program below, suppose we don't need data related to height. So, instead of removing the code related to height, we can simply convert them into comments.

#include <stdio.h>

int main() {
    int number1 = 10;
    int number2 = 15;
    int sum = number1 + number2;

    printf("The sum is: %d\n", sum);
    printf("The product is: %d\n", product); 
    return 0;
}

Here, the code throws an error because we have not defined a product variable.

We can comment out the code that's causing the error.

For example,

#include <stdio.h>

int main() {
    int number1 = 10;
    int number2 = 15;
    int sum = number1 + number2;

    printf("The sum is: %d\n", sum);
// printf("The product is: %d\n", product);
return 0; }

Now, the code runs without any errors.

Here, we have resolved the error by commenting out the code related to the product.

If we need to calculate the product in the near future, we can uncomment it.


Why use Comments?

We should use comments for the following reasons:

  • Comments make our code readable for future reference.
  • Comments are used for debugging purposes.
  • We can use comments for code collaboration as it helps peer developers to understand our code.

Note: Comments are not and should not be used as a substitute to explain poorly written code. Always try to write clean, understandable code, and then use comments as an addition.

In most cases, always use comments to explain 'why' rather than 'how' and you are good to go.

Next, we will be learning in detail about C Variables, constants and literals.

Video: Comments in C Programming

Did you find this article helpful?