Introduction :
Welcome to my first series on #Hashnode where I will be documenting my journey of learning C++ from scratch to advance to develop my programming skills and later make some interesting projects. In this blog, I will be sharing what I learnt in C++ since I wrote my previous blog . In this tutorial, we can study the variables and comments withinside the C++ language. In our last lesson, I mentioned the primary shape of a C++ program, in which we understood the operation of the C++ code line through the line. Do check it out to be up to date with what I will be sharing in this one.
Variables in C++ :
Variables are containers for storing data values.
In C++, there are different types of variables (defined with different keywords), for example:
int
- stores integers (whole numbers), without decimals, such as 123 or -123double
- stores floating point numbers, with decimals, such as 19.99 or -19.99char
- stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotesstring
- stores text, such as "Hello World". String values are surrounded by double quotesbool
- stores values with two states: true or false
Syntax for Declaring Variables in C++
Data_type Variable_name = Value;
Ex: int a=10; char ch = p;
Ex: float a= 1.1 , b=6.5;
Rules For Declaring Variables in C++
Variable names in C++ can range from 1 to 255 characters.
All variable names must begin with a letter of the alphabet or an underscore(_).
After the first initial letter, variable names can also contain letters and numbers.
Variable names are case-sensitive i.e it is treated differently depending on whether it is in capital or lower-case text.
No spaces or special characters are allowed.
You cannot use a C++ keyword (a reserved word) as a variable name.
Scope of Variables in C++
In general, the scope is defined as the extent to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with. There are mainly two types of variable scopes:
Local Variables: Variables defined within a function or block are said to be local to those functions.
Global Variables: As the name suggests, Global Variables can be accessed from any part of the program.
For example, consider this code snippet --- it shows an example of a variable " sum" taken as an integer variable, which will store a value 5, and writing sum after the " cout" statement will show us the value of "sum" on the output window i.e 5.
#include<iostream>
using namespace std;
int global_variable = 2;
int main(){
int local_variable = 3;
int sum = 5;
cout<< "The value of variable int sum is "<< sum;
return 0;
}
Comments in C++ :
A well-documented program is a good habit for a programmer. Programs are easier to read and easier to debug. Comments are an important part of good documentation.
In computer programming, comments are programmer-readable explanations or annotations in the source code of a computer program.
Comments are statements that are not executed by the compiler and interpreter.
There are two ways to write comments :
Single-Line Comments: 1st way is to use" //" before a single line of text to make it unparsable by the compiler.
Multi-Line Comments: 2nd way is to use "/*" as the opening and "*/" as the closing of the comment. We then write text in between them. It includes multiple statements in the form of non-executable texts for better readability.
For example :
This program consists of both single-line and multi-line comments that do not affect the outcome of the code. Compiling
and executing
this program will just print the statement - "Comments are easy".
#include<iostream>
using namespace std;
// This is a single line comment
/* this
is
a
multi
line
comment */
int main(){
cout<< "Comments are easy"<< sum;
return 0;
}
Congratulations!
You just learnt about variables and comments in C++ !!!
Thank you for joining me in my quest to conquer C++. In the next blog, well be talking more about the different Data Types in a C++ program, see you there, till then keep coding and keep learning.
Top comments (0)