Computer Programming: Variables and Data Types Furqan Aziz
Computer Programming: Variables and Data Types Furqan Aziz
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;
}