IP Lecture 2 DataTypesOperators
IP Lecture 2 DataTypesOperators
Expressions
Course Code: CSC1102 &1103 Course Title: Introduction to Programming
L-value R-value
Data type int: can be used to store integer numbers (values with no decimal
places)
Data type type float: can be used for storing floating-point numbers (values
containing decimal places).
Data type double: the same as type float, only with roughly twice the
precision.
Data type char: can be used to store a single character, such as the letter a,
the digit character 6, or a semicolon.
Data type bool: can be used to store just the values 0 or 1 (used for indicating
a true/false situation). This type has been added by the C99 standard (was not
in ANSI C)
Data types
Data Type Size Range Description
-2,147,483,648 Stores whole numbers, without decimals
int 4 bytes to
2,147,483,647
float 4 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 7 decimal
digits
double 8 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 15 decimal
digits
bool 1 byte true or false Stores true or false values
Stores a single character/letter/number, or
char 1 bytes 0 to 255
ASCII values
Assigning values to char
letter = ‘A'; /* OK */
letter = A; /* NO! Compiler thinks A is a variable */
letter = “A"; /* NO! Compiler thinks “A" is a string */
letter = 65; /* ok because characters are really
stored as numeric values (ASCII code),
but poor style */
Precedence
!, ++, --, (type)
*, /, %
+, -
=
Integer and Floating-Point
Conversions
Assign an integer value to a floating variable: does
not cause any change in the value of the number;
the value is simply converted by the system and
stored in the floating
Assign a floating-point value to an integer variable:
the decimal portion of the number gets truncated.
Integer arithmetic (division):
int divided to int => result is integer division
int divided to float or float divided to int => result is real
division (floating-point)
The Type Cast Operator
f2 = (float) i2 / 100; // type cast operator
The type cast operator has the effect of converting the value of
the variable i2 to type float for purposes of evaluation of the
expression.
This operator does NOT permanently affect the value of the
variable i2;
The type cast operator has a higher precedence than all the
arithmetic operators except the unary minus and unary plus.
Examples of the use of the type cast operator:
(int) 29.55 + (int) 21.99 results in 29 + 21
(float) 6 / (float) 4 results in 1.5
(float) 6 / 4 results in 1.5
The assignment operators
The C++ language permits you to join the arithmetic operators
with the assignment operator using the following general
format: op=, where op is an arithmetic operator, including +,
–, ×, /, and %.
op can also be a logical later in this course
Example:
count += 10;
Equivalent with:
count=count+10;
Example: precedence of op=:
a /= b + c
Equivalent with:
a = a / (b + c)
addition is performed first because the addition operator has higher
precedence than the assignment operator
Structure of a C++ program
Entry point of a C+
+ program
int main ()
{
int value1, value2, sum; Sequential
value1 = 50; flow of control
value2 = 25;
sum = value1 + value2;
cout<<"The sum = “ <<sum<<endl;
return 0;
}
Controlling the program flow