Variables, Data Types, and Arithmetic Expressions: Dept. of Computer Science Faculty of Science and Technology
Variables, Data Types, and Arithmetic Expressions: Dept. of Computer Science Faculty of Science and Technology
Expressions
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)
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 */
Storage sizes and ranges
Every type has a range of values associated with it.
}
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 or bit operator => 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
Declaring variables
Some older languages (FORTRAN, BASIC) allow you to use
variables without declaring them.
Other languages (C, Pascal) impose to declare variables
Advantages of languages with variable declarations:
Putting all the variables in one place makes it easier for a reader to
understand the program
Thinking about which variables to declare encourages the programmer
to do some planning before writing a program ( What information does
the program need? What must the program to produce as output?
What is the best way to represent the data?)
The obligation to declare all variables helps prevent bugs of misspelled
variable names.
Compiler knows the amount of statically allocated memory needed
Compiler can verify that operations done on a variable are allowed by
its type (strongly typed languages)
Declaration vs Definition
Variable declaration: [Type, Identifier]
Variable definition: a declaration which does also
reserve storage space (memory) !
Not all declarations are definitions
In the examples seen so far, all declarations are as well
definitions
Declarations which are not definitions: later in this semester !
Executing a program
Statement1
Program = list of statements
Statement2
Entrypoint: the point where the Statement3
execution starts Statement4
Control flow: the order in which Statement5
Statement6
the individual statements are
Statement7
executed Statement8
Structure of a program
Entry point of a
program