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

ITC C++ Programming LAB7

nm

Uploaded by

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

ITC C++ Programming LAB7

nm

Uploaded by

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

Department of By: Engr.

Farhan Memon
Electrical Engineering Introduction to Computers & C++
Mehran University of Engineering Year 1ST Semester 1st
& Technology Batch 17 Duration 03
Jamshoro Hours

Lab 7: Variables, Constants & Data types

Outlines

· Variables
· Fundamental Data Types
· Escape Sequences
· Constants
· Lab tasks

Requirements
· Code::Block IDE

1
VARIABLES
A variable as a named portion of memory to store a value.

- The values of variables are stored somewhere in an unspecified location in the


computer memory as zeros and ones. Our program does not need to know the exact
location where a variable is stored; it can simply refer to it by its name.

a = 5;

b = 2;

result = a - b;

Each variable (sometimes identifiers) needs a name that identifies it and distinguishes it
from the others.

-The name of a variable can be composed of letters, digits, and the underscore
character.

-It must begin with either a letter or an underscore.

Variable declaration
Each variable must be declared with data type:

int a;

int b;

float result;

OR

int a,b;

float result

Variable Initialization
We initialize the variables with assigning some values to them:

int a=5; int b=3;

float result=a/b;

2
Example

FUNDAMENTAL DATA TYPES

Fundamental data types are basic types, they can mainly be classified into:

Character types: They can represent a single character, such as 'A' or '$'. The most basic
type is char, which is a one-byte character.

Numerical integer types: They can store a whole number value, such as 7 or 1024.

Floating-point types: They can represent real values, such as 3.14 or 0.01.

Boolean type: The boolean type, known in C++ as bool, can only represent one of two states,
true or false.

3
The table shows the variable type, how much memory it takes to store the value in memory,
and what is maximum and minimum value which can be stored in such type of variables.

4
5
ESCAPE SEQUENCES

Example

6
CONSTANTS
Constants refer to fixed values that the program may not alter and they are called literals.
Constants can be of any of the basic data types : Integer, Numerals, Floating-Point Numerals,
Characters, Strings and Boolean Values.

There are two simple ways in C++ to define constants:

- Using #define preprocessor.


#define identifier value

- Using const keyword.


const type variable = value

Examples

7
LAB TASKS
1. Write a program to calculate Circumference and area of a circle. Where:
Value of radius it will take during run time.
Use the value pi = 3.141593, as a constant.
A = pi *r*r;
C = 2 * pi * r;

2. Write a program to calculate the area of rectangle and take width and height as
input.

3. Write down a program that adds two numbers/variables of type float and stores the
result in variable of type integer.

SUBMISSION:
In Hard-Format (manual along with exercise), will be checked in the next Lab

The End

You might also like