You are on page 1of 18

Computer

Programming
Lecture 3
Variables and Data Types
Furqan Aziz
C++ uses Variables to name and
store data
 Each variable has a name and is assigned a
memory location.
 Variable Names
 Variable declaration
 Variable definition
 Type
 Size
Data Types of variable
 Char
 Short
 Int
 Float
 Double
Characters
 Character Sets
 Character Encoding
 Escape Sequence (for control characters).
E.g., \n,
Integer Arithmetic
 The basic integer arithmetic
operators are +, -, *, /, %.
+ Addition
 Examples

cout<<10+3;
- Subtraction

cout<<10-3;

cout<<10-13; / Division

cout<<10/2;

cout<<10/3; * Multiplication

cout<<10%3;

cout<<10%2; % Remainder

cout<<2+3;

cout<<2+3+6;
Precedence of Operators
 What will be the output if the following
statement is executed.
cout<<3+5*2;
13 or 16
Precedence and Associativity of
Operators
Operator Associativity
*/% Left
+- Left
 What will be the output if the following
statement is executed.
cout<<3+5*2;
cout<< 2*5+3*7
cout<< 2*3+3*7/3
cout<< 2*3+3*(7/3)
Variable
 Used to store data in memory
 It has a name

Rules for naming variables
 It has a data type
 It has a value
Integer Variables
 Variable that can store integers.
 Declaring: int num1;
Assignment Operator =
 Used to assign values to a variable
 Has less priority than the arithmetic
operators
 Right associative.
 Examples

Num1 = 10;

Num1 = Num2 = 10+20;
Using Integer Variables
# include <iostream>
using namespace std;
int main()
{
int num1 = 10;
Int num2 = 20;
int sum = 0;
sum = num1 + num2;
cout<<“Sum is ”<<sum<<endl;
return 0;
}

Out put: Sum is 30


Integer Variable Types
Type Size (byte) Range
char 1 -128 to 127
unsigned char 1 0U to 255U
short 2 Guess What?
unsigned short 2 Guess What?
int 4 Guess What?
unsigned int 4 Guess What?
long 8 Guess What?
unsigned long 8 Guess What?
The arithmetic Assignment Operator
+=, *=, -=, /=, %=
 So called because the compose of two operators,
the assignment operator and one of the arithmetic
operator.
 Right Associative
 All the arithmetic assignments operators, and the
assignment operator have equal precedence.
 Examples

int x = 10;

x *= 2; // equivalent to x = x * 2.

x += 10; // add 10 to x.
Multiple arithmetic Assignment Operator

 Use multiple assignments and arithmetic


assignment operators in same statement.
 Some examples

int x = 10, y = 5, z = 0;

x = y += 10;

x += y -= 5;

x *= y+= 3;

z = x *= y+= 3;
Incrementing and Decrementing Integers
 increment operator ++. Increments one.
 Decrement operator --. Decrements one.
 Higher precedence that the arithmetic operators, the
assignment operators, and the arithmetic assignment
operators.
 Examples

int x=5;

x++;
 Postfix and Prefix

int x=5, y=0;

y = x++;

y = ++x;
The Constant keyword
 const int x = 10;
 The value of a constant variable cannot be
change.
Real variable types
 float: typically provide 7 digits precession.
 double: typically provide 15 digits
precession.
 long double: may provide 19 digits
precession.
 Examples

float pi = 3.1416f;

long val = 23.43245;
Working with characters

You might also like