- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Comments in C
Using comments in a C program increases the readability of the code. You must intersperse the code with comments at appropriate places. As far as the compiler is concerned, the comments are ignored. In C, the comments are one or more lines of text, that the compiler skips while building the machine code.
The comments in C play an important part when the program needs to be modified, especially by somebody else other than those who have written it originally. Putting comments is often not given importance by the programmer, but using them effectively is important to improve the quality of the code.
Why to Use Comments in C Programming?
Any programming language, including C, is less verbose as compared to any human language like English. It has far less number of keywords, C being one of the smallest languages with only 32 keywords. Hence, the instructions in a C program can be difficult to understand, especially for someone with non-programming background.
The C language syntax also varies and is also complicated. Usually, the programmer tries to add complexity to optimize the code. However, it makes the code hard to understand. Comments provide a useful explanation and convey the intention behind the use of a particular approach.
For example, take the case of the ?: operator in C, which is a shortcut for the if-else statement.
So, instead of the following code −
if (a % 2 == 0){ printf("%d is Even\n", a); } else { printf("%d is Odd\n", a); }
One can use the following statement −
(a % 2 == 0) ? printf("%d is Even\n", a) : printf("%d is Odd\n", a);
Obviously, the second method is more complicated than the first. If useful comments are added, it makes easier to understand the intention and logic of the statement used.
Types of Comments in C
In C, there are two types of comments −
- Single-line comments
- Multi-line comments
Single-line Comment in C
The C++-style single-line comments were incorporated in C compilers with C99 standards. If any text begins with the // symbol, the rest of the line is treated as a comment.
The text followed by a double oblique or forward slash [//] in a code is treated as a single-line comment. All that text after // is ignored by the C compiler during compilation. Unlike the multi-line or block comment, it need not be closed.
Syntax of Single-line C Comment
//comment text
The // symbol can appear anywhere. It indicates that all the text following it till the end of the line is a comment. The subsequent line in the editor is again a place to write a valid C statement.
Example: Single-line Comment in C
Take a look at the following program and observe how we have used single-line comments inside its main function −
/* Online C Compiler and Editor */ #include <stdio.h> #include <math.h> /*forward declaration of function*/ float area_of_square(float); float area_of_square(float side){ float area = pow(side,2); return area; } // main function - entire line is a comment int main(){ // variable declaration (this comment is after the C statement) float side = 5.50; float area = area_of_square(side); // calling a function printf ("Side = %5.2f Area = %5.2f", side, area); return 0; }
Output
When you run this code, it will produce the following output −
Side = 5.50 Area = 30.25
Multi-line Comment in C
In early versions of C (ANSI C), any length of text put in between the symbols /* and */ is treated as a comment. The text may be spread across multiple lines in the code file. You may call it a multi-line comment. A block of consecutive lines is treated as a comment.
Syntax of Multi-line C Comment
The general structure of a multi-line comment is as follows −
/* The comment starts here Line 1 Line 2 ….. ….. Comment ends here*/
For example −
/* Example of a multi-line comment program to print Hello World using printf() function */
Obviously, since the comments are ignored by the compiler, the syntax rules of C language don't apply to the comment text.
Comments can appear anywhere in a program, at the top, in between the code, or at the beginning of a function or a struct declaration, etc.
Example: Multi-line Comment in C
In this example, we have a multi-line comment that explains the role of a particular user-defined function used in the given code −
/* program to calculate area of square */ /* headers */ #include <stdio.h> #include <math.h> /* forward declaration of function */ float area_of_square(float); /* main function */ int main(){ /* variable declaration */ float side = 5.50; /* calling function */ float area = area_of_square(side); printf("Side = %5.2f Area = %5.2f", side, area); return 0; } /* User-defined function to calculate the area of square. It takes side as the argument and returns the area */ float area_of_square(float side){ float area = pow(side, 2); return area; }
Output
When you execute the code, it will produce the following output −
Side = 5.50 Area = 30.25
While inserting a comment, you must make sure that for every comment starting with /*, there must be a corresponding */ symbol. If you start a comment with /* and fail to close it, then the compiler will throw an error.
Note: A blocked comment or multi-line comment must be put inside /* and */ symbols, whereas a single-line comment starts with the // symbol and is effective till the end of the line.
Placing comments in a program is always encouraged. Programmers usually avoid the practice of adding comments. However, they can sometimes find it difficult to debug and modify their code if it is not properly commented. Comments are especially vital when the development is done in collaboration. Using comments effectively can be of help to all the members of a team.
Even though comments are ignored by the compiler, they should be clear in meaning and concise. Whenever the code needs modification, the reason, the timestamp, and the author should be mentioned in comments.