// This Article is contributed by Debarup Sarkar
Declaring a variable :
Whenever we will declare a variable, we should always know what type of value it should hold, whether it’s an integer (int), decimal number (float / double), character value (char).
In general, the variable is declared as follows :
Datatype variableName = VALUE;
● int : Integer value
● float, double : Decimal number
● char : Character values (including special characters)
● bool : Boolean values (true or false)
● long : Contains integer values but with larger size
● short : Contains integer values but with smaller size
Table for datatype and its size in C++:
(This can vary from compiler to compiler and system to system depending on the version you are using)
Datatype & their default size :
A) int — — 4 bytes
B) char — — 1 byte
C) short — — 2 bytes
D) bool — — 1 byte
E) long — — 8 bytes
F) float — — 4 bytes
G) double — — 8 bytes
Example: To declare an integer variable ‘n’ with a value of 6, the structure looks like: int n = 6; & Similarly, this way other types of variables can also be declared
Some Rules For Variable Names :
● Can’t begin with a number.
● Spaces and special characters except underscore( _ ) are not allowed.
● C++ keywords (reserved words) must not be used as a variable name.
● C++ is case-sensitive, meaning a variable with name ‘A’ is different from variable with name ‘a’. (Difference in the upper-case and lower-case holds true)
Printing/Providing output:
For printing statements in C++ programs, we use the ‘cout’ statement .
For example: If you want to print “Hello World!” (without parenthesis) in your code, we will write it in following way:
cout << “Hello World!”;
Full Code :
#include <iostream>
using namespace std;
int main() {
cout<<”Hello World!” ;
}
Output: Hello World!
Line separator:
For separating different lines in C++, we use endl or ‘\n’ .
code example :
#include <iostream>
using namespace std;
int main() {
cout<<"Hello World(1)”<<endl;
cout<<"Hello World(2)”<<’\n’;
}
Output:
Hello World(1)
Hello World(2)
Taking input from the user:
To take input from the user, we use the ‘cin’ statement.
For example: Taking Number Input from user :
int n;
cin >> n;
Lets try to code this : User wants to take input of 2 numbers and then print the sum of them:
Code:
#include <iostream>
using namespace std;
int main() {
int a, b, sum; // declaring data types & variables
cin >> a;
// taking input from user
cin >> b; // taking input from user
sum = a + b; // calculation
cout << “Sum of two numbers: ”; // printing output
cout << sum << endl; // printing output
}
// Input & Output discussion :
/* Input :
3
5
Output :
Sum of two numbers: 8 */
Operators in C++
There are 3 types of operators in C++
● Arithmetic operators
● Relational operators
● Logical operators
1) Arithmetic operators:
These are used in mathematical operations in the same way as that in algebra
OPERATOR | DESCRIPTION |
+ | Add two operators |
– | Subtracts second operand from the first |
* | Multiplies two operands |
/ | Divides numerator by denominator |
% | Calculates Remainder of division |
2) Relational operators:
C++ relational operators specify the relation between two variables by comparing them.
Following table shows the relational operators that are supported by C++
OPERATOR | DESCRIPTION |
== | Checks if two operands are equal |
!= | Checks if two operands are not equal |
> | Checks if operand on the left is greater than operand on the right |
< | Checks if operand on the left is lesser than operand on the right |
>= | Checks if operand on the left is greater than or equal to operand on the right |
<= | Checks if operand on the left is lesser than or equal to operand on the right |
Logical operators:
OPERATOR | DESCRIPTION |
&& | Logical AND |
| | | Logical OR |
! | Logical NOT |
Dada sotti eto upokar tomra korcho r kichu bolar nei ,, just take❤️❤️