0% found this document useful (0 votes)
19 views

CPP 2

This document discusses various topics related to variables and data types in C++ including: - Variables are portions of memory used to store values and have naming constraints like only allowing letters, digits, and underscores. - Common data types include bool, char, int, long, float, and double which can be modified with type modifiers. - Variables are declared with a data type and can be initialized at declaration. - Constants are declared with const and have a specific type and value. - Arithmetic operators like +, -, *, /, % are used to perform math operations on variables and constants.

Uploaded by

M.Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CPP 2

This document discusses various topics related to variables and data types in C++ including: - Variables are portions of memory used to store values and have naming constraints like only allowing letters, digits, and underscores. - Common data types include bool, char, int, long, float, and double which can be modified with type modifiers. - Variables are declared with a data type and can be initialized at declaration. - Constants are declared with const and have a specific type and value. - Arithmetic operators like +, -, *, /, % are used to perform math operations on variables and constants.

Uploaded by

M.Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture two

 Variables and data types


 Constant s
 Operators
 Input command
 Variables and data types
 Constant s
 Operators
 Input command
 What is Variable?
A portion of memory to store a value.
 What are variable
name constraints?
1. Only Alphabets, Digits and Underscores are
permitted.
2. Variable name cannot start with a digit.
3. Reserved words cannot be used as a name.
4. Upper case and lower case letters are distinct.
5. Special Characters are not allowed.
6. Global Identifier cannot be used as Variable.
 Variable name examples

Variable name
Name valid
name valid
name_1 valid
Int valid
_SUM valid
sum_of_the_numbers valid
num^2 Not valid
cin Valid // iostream not included
 Variable name example

Variable name
Int valid
$sum Not valid
num^2 Not valid
num 1 Not valid
2num Not valid
INT valid
firstName valid
 What are variable
data types ?
Boolean bool
Character char
Integer int
Long int long
Floating point float
Double floating point double
Variable types can be modified using one or more of these type
modifiers:

 signed
 unsigned
 short
 long
Declaration of variables

Data type variable name ;

int a ;

Integer a,b Decimal c

int a,b;
float c;
Declare and initialize variable

int a=5;
 Variables and data types
 Constants
 Operators
 Input command
You can use const prefix to declare constants
with a specific type as follows:

const type variable = value;


#include <iostream.h>

int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
Result
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE; 50
return 0;
}
 Variables and data types
 Constants
 Operators
 Input command
 Arithmetic operators

operator description
+ addition
- subtraction
* multiplication
/ division
% modulo
X= 11%3 Result is 2
Expression Equivalent to...
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
price *= units + 1; price = price * (units+1);
// compound assignment operators
#include <iostream.h>
int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << a;
}

Result
5

You might also like