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

Lecture 3+4 Data Types

The document discusses various C++ data types including integers, characters, floating-point numbers, booleans, enums, and void. It also covers declaring and initializing variables, constants, arithmetic operators, and relational operators. Key concepts covered include variable scope, type conversions, and the sizes of different data types in memory.

Uploaded by

brm1shubha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture 3+4 Data Types

The document discusses various C++ data types including integers, characters, floating-point numbers, booleans, enums, and void. It also covers declaring and initializing variables, constants, arithmetic operators, and relational operators. Key concepts covered include variable scope, type conversions, and the sizes of different data types in memory.

Uploaded by

brm1shubha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Lecture 3 + 4

,Data types
Variables and Constant
Variable Declaration
Assignment Statement
Reading and writing variables
First C program

Instructor : Haya Samma’aneh


Data Types : 1- Integer

• An integer type is a number without a


fractional part.
• Designed to hold whole numbers
• Can be signed or unsigned:
– 12 -6 +3
• Available in different sizes (number of bytes):
short int, int, and long int
• Size of short int  size of int  size of long
int
Declaration of Integer Variables
• Declarations tell the compiler what variable names
will be used and what type of data each can handle
(store).

• Variables of integer type can be defined


- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
Data Type: 2- character
• Used to hold characters like ‘d’
• Numeric value of character is stored in
memory:

CODE: MEMORY:
char letter; letter
letter = 'C'; 67
Declaration of character Variables

• Variables of character type can be


defined:
- On separate lines:
char x;
- On the same line:
char x, y;
Data Types: 3- Floating-Point
• A floating-point type is a number with a
fractional part
• Designed to hold real numbers
12.45 -3.8
• All numbers are signed
• Available in different sizes (number of bytes):
float, double, and long double
• Size of float  size of double
 size of long double
Declaration of floating point
Variables
• Variables of floating point type can be
defined:
- On separate lines:
double x;
float y;
long double z;
- On the same line:
double x, y;
float y , e;
long double z , r;
Data Types: 4- The bool

• Represents values that are true or false


• bool variables are stored as small integers
• false is represented by 0, true by 1:
• bool declarations: allDone finished

bool allDone = true; 1 0

bool finished = false;


Data Type:5- enum
C++ introduces enumerations (‫ت<<<دد‬
<‫) ع‬to hold a set of values specified by
the user

enum colour {RED, WHITE, YELLOW}; // definition of type colour


colour a; // definition of var. of type colour
colour b = RED; // definition and initialization
enumeration variables would typically be used in switch clauses:

switch (a) {
case RED:
...
case WHITE:
...
}
Data Type: 6-void

The void type has no values and no operations.


Sizes of Data-Types
Bytes Digits of Precision Type

1 - char

2 - short

4 - int

4 - long

4 7 float

8 15 double

10 19 long
double
Type Conversions
• C++ is a hard-typed language, meaning that each variable
has a fixed type that does not change.
double pi=3.14; // ok
double x=”Hello”; // fails
• Some type conversions occur automatically for example int to
float or float to double
int i = 17;
float x = i; // assigns 17 to x
int j = 2;
float y = i/j;
Type conversions can be forced by a programmer through
a type cast
float z = static_cast<float>(i)/j; // casts i into float before division
Keywords and Identifier
Some Keywords in C and C++ :

asm double new switch


auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile
do long struct while
Identifiers

• Programmer-chosen names to represent parts


of the program: variables, functions, etc.
• Cannot use C++ key words as identifiers
• Must begin with alpha character or _, followed
by alpha, numeric, or _
• Upper- and lower-case characters are
important (case-sensitive)
Valid and Invalid Identifiers
REASON IF INVALID ?VALID IDENTIFIER
Yes totalSales

Yes total_Sales

. Cannot contain No total.Sales

Cannot begin with digit No 4thQtrSales

$ Cannot contain No $totalSale


Declarations
• Constants and variables must be declared before they
can be used.
• A constant declaration specifies the type, the name and
the value of the constant.
• any attempt to alter the value of a variable defined
as constant results in an error message by the compiler
• A variable declaration specifies the type, the name and
possibly the initial value of the variable.

• When you declare a constant or a variable, the compiler:


1. Reserves a memory location in which to store the
value of the constant or variable.
2. Associates the name of the constant or variable with
the memory location.
Constant Variables
(C++ style constants)
•Example:
const double PI = 3.14159;

(old C style constants)


• constants can also be specified using the preprocessor
directive #define example:
#define PI 3.14159
the preprocessor replaces the identifier PI by the text
3.14159 throughout the program
• the major drawback of #define is that the data type of
the constant is not specified
Constant declarations
• Constants are used to store values that never change during the
program execution.
• Using constants makes programs more readable and maintainable.

Syntax:
const <type> <identifier> = <expression>;
Examples:
const double x = 7.8;

const double r = x * 2;
Variable declarations
• Variables are used to store values that can be changed during the
program execution.

Syntax:
< type > < identifier >;
< type > < identifier > = < expression >;
Examples:
int sum;
int total = 3445;
char answer = 'y';
double temperature = -3.14;
.Variable declarations,cont
• A variable has a type and it can contain only values of that type.
• For example, a variable of the type int can only hold integer
values.
• Variables are not automatically initialized. For example, after
declaration
int sum;
the value of the variable sum can be anything (garbage).
• Thus, it is good practice to initialize variables when they are
declared.
• Once a value has been placed in a variable it stays there until the
program alters it.
Character data
• A variable or a constant of char type can hold an ASCII character.

• When initializing a constant or a variable of char type, or when


changing the value of a variable of char type, the value is enclosed in
single quotation marks.

Examples:
const char star = '*';
char letter, one = '1';
Variable Assignments and
Initialization
Assignment:
• Uses the = operator
• Has a single variable on the left side and a
value (constant, variable, or expression)
on the right side
• Copies the value on the right into the
variable on the left:
item = 12;
Variable Assignments and
Initialization
• Initialize a variable: assign it a value
when it is defined:
int length = 12;
• Can initialize some or all variables:
int length = 12, width = 5, area;
Scope
• The scope ‫)مجا<<ل‬of
( a variable: where the
program can access the variable
• A variable cannot be used before it is
defined
Arithmetic Operators

• Used for performing numeric calculations


• C++ has unary, binary, and trinary
operators:
– unary (1 operand) -5
– binary (2 operands) 13 - 7
– ternary (3 operands) exp1 ? exp2 : exp3
Arithmetic Operations
• multiplication, summation, subtraction, division
int i = 1/3; // integer division result 0
float x = 1.0/3; // floating point division result 0.3333
int j = 7 % 3; // modulo operator remainder of 7/3
• prefix and postfix-increment operator ++
int i=3;
int j=7;
cout << 10 * i++; // outputs 30, i has value 4 afterwards
cout << 10 * ++j; // outputs 80, j has value 8 afterwards
• arithmetic assignment operators
float x=6.0;
x+=3.5;
• is equivalent to
x=x+3.5;
Binary Arithmetic Operators
VALUE OF EXAMPLE OPERATION SYMBOL
ans
10 ;ans = 7 + 3 addition +

4 ;ans = 7 - 3 subtraction -

21 ;ans = 7 * 3 multiplication *

2 ;ans = 7 / 3 division /

1 ;ans = 7 % 3 modulus %
Operator /

• / (division) operator performs integer


division if both operands are integers
cout << 13 / 5; // displays 2
cout << 91 / 7; // displays 13
• If either operand is floating point, the result
is floating point
cout << 13 / 5.0; // displays 2.6
cout << 91.0 / 7; // displays 13.0
Operator %

• % (modulus) operator computes the


remainder resulting from integer division
cout << 13 % 5; // displays 3
• % requires integers for both operands
cout << 13 % 5.0; // error
Relational Operators
• a relational operator compares two values of C++ built
in data types such as char, int, float
• typical relationships are equal to, less than and greater than
• the result of a comparison is either true or false, where 0 is
false and any value unequal to 0 is true
• C++ provides the following relational operators
<, >, ==, !=, <=, >=
• example:
int x=44;
int y=12;
(x == y) // false
(x >= y) // true
(x != y) // true

You might also like