C Comments

C Tutorial

1. C Comments

  • In C Comments Comments are notes written in the code that are ignored by the compiler.

  • They are used to explain code, make it readable, or temporarily disable code.

  • Comments do not affect the program execution.


2. Types of Comments

Two types of comments:

(a) Single-line Comment

  • Starts with //

  • Everything after // on that line is ignored by the compiler.

Example:


 

Output:

Age: 25

The comment // This is the age of the user is ignored by the compiler.


(b) Multi-line Comment

  • Starts with /* and ends with */

  • Can span multiple lines.

Example:


 

Output:

Age: 25

3. Uses of Comments

  1. Explain code for yourself or others.

  2. Temporarily disable code for testing:

// printf("This line is commented and won't run\n");
  1. Document complex logic or algorithms.


4. Best Practices

  • Write clear, concise comments.

  • Avoid obvious comments like int x = 10; // assign 10 to x (the code is self-explanatory).

  • Use comments to explain why, not what.


5. Quick Example Combining Both


 

Output:

Sum: 15

You may also like...