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

C Lecture-3-DataTypes

C++Lecture-3-DataTypes

Uploaded by

Alili Amar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C Lecture-3-DataTypes

C++Lecture-3-DataTypes

Uploaded by

Alili Amar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Spring 2021 - CS1428 Chapter 2 – Introduction to C++

Data Types

• Variables are classified according to their data type.


• The data type determines the kind of information that may be stored in the variable.
• A data type is a set of values.
• Generally two main (types of) data types:
a. Numeric ( integers such as 3, 157 , -47 and floating points such 23.7 , 0.94 )
b. Character
• Primary Consideration for selecting a numeric data type are :-
a. The largest and the smallest numbers that may be stored in the variable.
b. How much memory the variable uses.
c. Whether the variable stores signed or unsigned numbers
d. The number of decimal places of precision the variable has.

Data Types

Data Type Represents


int, short, long whole numbers ( integers )
float, double real numbers ( fractional , decimal )
bool logical values : true, false
char a single character
string sequence of chars.

Integer Data Types


int, short int , long int

• Whole numbers 2 , 1000 , -900


• May be signed or unsigned
• Typical sizes and ranges (may vary depending on the system)
• Literals ( are int by default)

Husain Gholoom – Senior Lecturer in Computer Science Page 1


Spring 2021 - CS1428 Chapter 2 – Introduction to C++

Integer Data Types

Data Type Size Range


Short int 2 bytes -32,768 to 32,767
unsigned short int 2 bytes 0 to 65,535
int 4 bytes -2,147,483,648 to
2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
Long int 4 bytes -2,147,483,648 to
2,147,483,647
unsigned long int 4 bytes 0 to 4,294,967,295
Long long int 8 bytes -9,223,372,036,854,775,808
to
9,223,372,036,854,775,808
unsigned long long int 8 bytes 0 to
18,446,744,073,709,551,615

Example of Variable Definitions:


short dayOfWeek;
unsigned long distance;
int xCoordinate = 10;
long deficit = 1500;

Husain Gholoom – Senior Lecturer in Computer Science Page 2


Spring 2021 - CS1428 Chapter 2 – Introduction to C++

Floating-Point Data Types

• Used to hold real numbers such as 2.5 , -7.8

• Typical sizes and ranges (may vary depending on the system):

Single Precision float 4 bytes +/- 3.4e +/- 38 (~7 digits)


Double Precision double 8 bytes +/- 1.7e +/- 308 (~15 digits)
Long Double Precision long double 8 bytes* +/- 1.7e +/- 308 (~15 digits)

*some compiler use 10 bytes for long double : the range is +/- 3.4E-4932 and
+/- 1.1E4832
• Floating-point literals can be represented in

– Fixed point (decimal) notation: 31.4159 0.0000625


– E (scientific) notation: 3.14159E1 6.25e-5

Note : there are no unsigned floating point data types. On all machines, variables of
the float , double, and long double data types can store positive or negative numbers.

Literals ( default type is double ) – can be expressed in a variety of ways :-

31.415E5 // equivalent to 3141500.0 - E or e will work - but printed as e.


-31.415e5 // equivalent to -3141500.0
3.1e-4 // equivalent to 0.00031

Floating-Point Data Types

float distance, time;


double mass;

distance = 1.495979E11; // how far away the sun is (in meters)


mass = 1.989E30; // how much the sun weighs (in kilograms)
time = 12.816; // hours of daylight in San Marcos today, 8/31

Husain Gholoom – Senior Lecturer in Computer Science Page 3


Spring 2021 - CS1428 Chapter 2 – Introduction to C++

Converting between floating-points and integers:

int i; float f;
f = 8.9;
i = 8.9; // stores 8 in i ( truncates, does not round )
i = 8;
f = 8; // stores 8.0 in f
f = 7.9;
i = f; // stores 7 in i

The bool Data Type

• Defined as bool
• Literals: the values are true or false

bool boolValue;
boolValue = true;
cout << boolValue << endl;
boolValue = false;
cout << boolValue << endl;

Output:

1
0

• bool is a numeric type:


• true is 1 and false is 0

The char Data Type

• char
• Literals: All the keyboard and printable symbols such as 'A' '3' '!' '\n' 'n'.
• Numeric value of character from the ASCII character set is stored in memory:

char letter;
letter = 'A'; // 65 is stored in memory
cout << letter << endl;
letter = '!';
cout << letter << endl;
Output:

A
!

Husain Gholoom – Senior Lecturer in Computer Science Page 4


Spring 2021 - CS1428 Chapter 2 – Introduction to C++

• char is really a numeric type also!


• Note: 65 is the ASCII code for 'A'

char letter;
letter = 65;
cout << letter << endl;
letter = 66;
cout << letter << endl;

Output:

A
B

The string Data Type


• A string is a sequence of characters.
• Requires the string header file: #include <string>
• Literals: “Hello again” “Over\nThere” “Y”
• A string is stored sequentially in memory, with the null character ('\0') at the end.
• The null character is not displayed.
• To define string variables in programs:

string firstName , lastName;

• To assign literals to variables :

firstName = "George";
lastName = "Washington";

• To display via cout :

cout << firstName << " " << lastName;

Husain Gholoom – Senior Lecturer in Computer Science Page 5


Spring 2021 - CS1428 Chapter 2 – Introduction to C++

sizeof
• sizeof function returns size of a data type in bytes in any system.
• The result is system-dependent.
• The argument may be a data type:
sizeof(int) // result is 4 on most systems
• The argument may be a variable:
double salary;
cout << sizeof(salary); // result is 8 on most systems

What is the output of the following ??


cout << "The size of a short is " << sizeof(short) << " bytes.\n";

cout << "The size of an integer is " << sizeof(int) << " bytes.\n";

Declaring Variables with the auto Key Word


The auto word key tells the compiler to determine the variable’s data type
from the initialization value.

auto amount = 100;


auto interestRate = 12.5;
auto stockCode = ‘X’;

The above statements uses auto instead of a data type

Scopes of a Variable

A variable's scope is the part of the program in which a variable can be


accessed.

Rule : A variable cannot be used before it is defined.

Husain Gholoom – Senior Lecturer in Computer Science Page 6


Spring 2021 - CS1428 Chapter 2 – Introduction to C++

Example :-

#include <iostream>
using namespace std;
int main () {
value = 150; //error, use of value before it is defined
int value;
cout << value; }

Named Constants

• Variable whose value cannot be changed during program


execution

Literals do not have “meaningful names”

cost = price + (price * .0825);

• what is the meaning of .0825?

Same literal may be used throughout a program, but may want to change it later.

• Maybe .0825 occurs in dozens of places in the code.


• Search and replace problem.

Literals may be given names to be used in their place.

General Form:

• const data_type VARIABLE = value;

Husain Gholoom – Senior Lecturer in Computer Science Page 7


Spring 2021 - CS1428 Chapter 2 – Introduction to C++

For Example

o const double SALES_TAX_RATE = .0825;

Then the equation will be

cost = price + (price * SALES_TAX_RATE);

• const makes the variable read-only

• Initialization required

• All-caps for the name of the constant is just a convention

Husain Gholoom – Senior Lecturer in Computer Science Page 8

You might also like