Comments

Lesson 10
Author : Afrixi
Last Updated : January, 2023
C - Programming Language
This course covers the basics of programming in C.

Comments in C programming language are used to add human-readable explanations to the code that do not affect the program’s execution. These comments can help other developers understand the code or remind the developer of the code’s purpose when revisiting it in the future.

In C, there are two ways to add comments to the code: single-line comments and multi-line comments.

Single-line comments start with // and continue to the end of the line. For example:

// This is a single-line comment
int x = 5; // This is a single-line comment at the end of a line of code

Multi-line comments start with /* and end with */. Anything between the opening and closing comment symbols is considered a comment. For example:

/* This is a multi-line comment
   that can span multiple lines of code
   and even include code examples:
   
   int x = 5;
   int y = 10;
   printf("The sum of x and y is %d", x + y);
*/

It is good practice to use comments to explain what the code is doing and why it is doing it. However, it is important not to overuse comments or add comments that are redundant or unnecessary. The code should be self-explanatory whenever possible, and comments should only be used when additional explanation is needed.