Skip to main content

Comments in C++ Programming

Comments are annotations in the source code that help explain what the code is doing. They are crucial for making the code more readable and maintainable. In C++, there are two types of comments: single-line and multi-line.

Types of Comments in C++

  1. Single-Line Comments:
    • Syntax: //
    • Extends from the // to the end of the line.
    • Example:

// This is a single-line comment

int x = 10; // Initialize x with 10

 

  1. Multi-Line Comments:
    • Syntax: /* comment */
    • Can span multiple lines.
    • Example:

/* This is a multi-line comment

   which spans over multiple lines. */

int y = 20;

 

Key Characteristics

  • Ignored by Compiler: Both types of comments are ignored by the C++ compiler and have no effect on the execution of the program.
  • Versatile Placement: Comments can be placed anywhere in the code to provide explanations or to disable certain lines of code temporarily.

Examples Using Dev C++

Here are some examples demonstrating the use of comments in a C++ program, with the assumption that you're using Dev C++ as your IDE.

 

  • Single-Line Comment:

#include <iostream>

using namespace std;

 

int main() {

    cout << "Hello, Dev C++!"; // This line prints a greeting message

    return 0;

}

In this example, the comment // This line prints a greeting message explains what the preceding line of code does.

 

  • Multi-Line Comment:

#include <iostream>

using namespace std;

 

int main() {

    /* The following code demonstrates

       the use of multi-line comments

       in C++ using Dev C++ IDE. */

    cout << "Learning C++ is fun!";

    return 0;

}

Here, the multi-line comment explains the purpose of the code block.

 

  • Nested Comments:

/* This is a multi-line comment that

   temporarily disables the code below:

 

cout << "This won't be printed"; // This line is commented out

 

*/

In this example, the multi-line comment /* ... */ is used to disable a block of code, which includes a single-line comment.

 

Practical Usage

Comments play a crucial role in software development by:

  • Describing Code Functionality: Helping others understand what a specific block of code does.
  • Documenting Assumptions: Clarifying important details or assumptions within the code.
  • Temporary Disabling: Allowing developers to comment out code during debugging or while developing new features.

Using Dev C++, you can easily add comments to your code by typing // for single-line comments or /* ... */ for multi-line comments. Comments are an essential tool for writing clear, understandable, and maintainable code.

 

Comments

Popular posts from this blog

How to Make Automatic Room Light Controller Without Microcontroller

You must have noticed in some offices or hotels, when nobody is in gallery or washroom, the light remains OFF but when somebody enters the place, light switches ON automatically. In this post I am going to teach you how to make this circuit. Before going ahead I would like to tell you that this is VERY EASY circuit. For this circuit the material we need is… PIR Motion sensor General Purpose PCB - 5x5 cm. Transistor 2222N – 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Relay Circuit Concept : We can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed). So I am using 5V relay. PIR sensor has three terminals, One for 5Vdc Second for Gnd (0V). Third for

How to drive high voltage/current load by small voltage signal from a microcontroller?

Sometimes we need to control or drive a high voltage and heavy current load by a small voltage signal. For example, if you want to control water motor with your microcontroller output. We know that microcontroller gives only 5v output which is not sufficient to drive a heavy motor. This circuit, about which this post is, is very-very useful for electronics engineer and hobbyist. So pay attention! For this circuit the material we need is… General Purpose PCB - 5x5 cm. Transistor KN 2222A (TO-92) - 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Tools. Concept: Weak signal triggers the transistor and transistor acts as a switch for the relay. You can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed).

How to Read Analog Input & Use PWM pin as Analog Output

  Analog Input: An analog signal can take on any number of values. To measure the value of analog signals, Arduino has a built-in analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. There is an inbuilt function to read Analog value; analogRead(pin_number). This function converts the value of the voltage on the analog input pin and returns a digital value ranges from 0 to 1023, relative to the reference value. The default reference voltage is 5 V (for 5 V Arduino boards) or 3.3 V (for 3.3 V Arduino boards). This function has only one parameter, which is the pin number.     Analog Output: The Arduino does not have any built-in digital-to-analog converter (DAC), but it can do pulse-width modulation (PWM); a digital signal to achieve some of the functions of an analog output. The function analogWrite(pin, value) is used to output a PWM signal. In the function ‘pin’ is the pin number used for the PWM output. ‘value’ is a number proportiona