- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Comments
![]() Share with a Friend |
C Programming - C Comments
Comments in C are used to explain the code and make it more readable for developers. They are ignored by the compiler and do not affect the program's execution.
Types of Comments in C
- Single-line Comments
- Denoted by //.
- Used for brief explanations or notes.
- The compiler ignores everything on the same line after //.
Example:
#include <stdio.h> int main() {// This is a single-line comment
printf("Hello, World! \n"); // Print statement return 0; // Exit the program }
- Multi-line Comments
- Denoted by /* to start and */ to end.
- Used for longer explanations, block comments, or to temporarily disable sections of code.
- The compiler ignores everything between /* and */.
Example:
#include <stdio.h> int main() {/* This is a multi-line comment. It can span multiple lines. */
printf("Hello, World! \n"); return 0; }
Best Practices for Using Comments
- Keep Comments Relevant:
- Write comments that explain why the code exists, not what it does (the code should make the "what" clear).
- Avoid Over-Commenting:
- Do not comment every line unnecessarily. Only comment complex logic or assumptions.
- Update Comments:
- Ensure comments are updated when the corresponding code is changed.
- Use Multi-line Comments Sparingly:
- Overuse of multi-line comments can clutter the code. Use them only when necessary.
Uses of Comments
- Code Explanation:
- Helps other developers (and your future self) understand the logic and purpose of the code.
Example:
// Calculate the area of a rectangleint area = length * width;
- Disabling Code:
- Temporarily remove sections of code from execution during debugging or testing.
Example:
/*
printf("This line is disabled and won't execute.\n");
*/
- Documentation:
- Comments can serve as documentation for functions, variables, and logic.
Example:
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
*/
Common Mistakes with Comments
- Commented-Out Code Left Behind:
- Avoid leaving unused, commented-out code in the final version.
Example:
// Old version of the function
// int multiply(int a, int b) {
// return a * b;
// }
- Misleading Comments:
- Ensure comments match the actual functionality of the code.
- Overloading Code with Comments:
- Excessive comments can make the code harder to read.
Conclusion
Comments are essential for writing maintainable and readable code. When used judiciously, they make the codebase easier to understand and maintain. However, poorly written or excessive comments can reduce code clarity. Always aim for clear, concise, and relevant comments.
