CO1003_Chapter 3_Variables and Basic Data Types
CO1003_Chapter 3_Variables and Basic Data Types
3
Content
🞐 Introduction
🞐 Data and Data Types
🞐 enum Data Type
🞐 struct Data Type
🞐 Variables and Variable Declaration
🞐 Constant Definition
🞐 Expressions
🞐 Operators
🞐 Summary 4
Introduction
Algorithm findMinNumber
🞐 Given a Input: positiveNumber[n] which is an array of n positive double values
Output: minNumber which is the smallest one whose type is double
set of n Purpose: find the smallest number in a collection
Precondition: n data inputs are positive.
positive Begin Algorithm
Check positiveNumber[n] contains only positive values
numbers, minNumber = positiveNumber[1]
iteration = 2
find the While (iteration <= n)
Begin While
smallest If (minNumber <= positiveNumber[iteration]) Then
iteration = iteration + 1
one.
Else
Begin
(Chapter 1 –
minNumber = positiveNumber[iteration]
iteration = iteration + 1
Pseudo code)
End
End While 5
Introduction
🞐 Given a void main() {
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
set of n
int n = 10;
positive double minNumber = positiveNumber[0];
int iteration = 1;
numbers,
while (iteration < n) {
find the if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
smallest
else {
one. minNumber = positiveNumber[iteration];
iteration = iteration + 1;
(Chapter 1 –
}
Real code in C) }
}
6
Introduction
🞐 Given a void main() {
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
set of n
int n = 10;
positive double minNumber = positiveNumber[0];
int iteration = 1; /* Variable declarations */
numbers,
while (iteration < n) {
find the if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
smallest
else {
one. minNumber = positiveNumber[iteration];
iteration = iteration + 1;
(Chapter 1 –
}
Real code in C) }
}
7
Introduction
🞐 Given a void main() {
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
set of n INPUT
int n = 10;
positive OUTPUT
double minNumber = positiveNumber[0];
int iteration = 1; ONES
SUPPORTING /* Variable declarations */
numbers,
while (iteration < n) {
find the if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
smallest
else {
one. minNumber = positiveNumber[iteration];
iteration = iteration + 1;
(Chapter 1 –
}
Real code in C) }
}
8
Introduction
🞐 Handle data in a C program
■ Input
■ Output What?
■ Supporting ones
🞐 Who declares?
■ Output What?
Values + Types
(Our real world)
■ Supporting ones
🞐 Who declares? - We
Where?
🞐 Who stores? - Computer Memory
(Computer’s world)
20
ASCII = American Standard Code for Information Interchange
Built-in Data Types in C
char
21
Built-in Data Types in C
int
23
Built-in Data Types in C
float
Variable definition:
31
enum Data Type
32
struct Data Type
🞐 A structure is a collection of one or more
variables grouped together under a single
name for convenient handling.
🞐 The keyword struct introduces a structure
declaration, which is a list of declarations
enclosed in braces, to define a derived type.
🞐 An optional name called a structure tag may
follow the word struct.
🞐 The variables named in a structure are called
members.
🞐 Structures can be nested. 33
struct Data Type
🞐 A structure declaration that is not followed by
a list of variables reserves no storage; merely
describes a template or shape of a structure.
🞐 Size of a struct data type is a total sum of all
the sizes of the types of its members.
🞐 An automatic structure may also be initialized
by assignment or by calling a function that
returns a structure of the right type.
🞐 A member of a particular structure is referred
to in an expression by a member operator:
structure-name.member 34
struct Data Type
struct { struct point { struct student {
int x; int x; char IdStudent[10];
int y; int y; char Name[50];
}; }; int IdMajor;
int EntranceYear;
struct point Location;
};
Structure declaration
37
New Type Name Definition
🞐 A mechanism for creating synonyms (or
aliases) for previously defined data types
typedef old_type_name new_type_name;
39
Variables and Variable Declaration
🞐 A variable is a location in memory where a
value can be stored for use by a program.
🞐 All variables must be defined with a name
and a data type before they are used in a
program.
🞐 Each variable has a value processed in a
program within a scope to be referred.
🞐 Variable classification
■ Global variables
■ Local variables
40
Variables and Variable Declaration
🞐 Variable names actually correspond to
locations in the computer’s memory.
🞐 A variable name in C is any valid identifier.
■ a series of characters consisting of letters, digits
and underscores (_) that does not begin with a digit
🞐 •: _minNumber, global_counter, i1, i2
🞐 X: min#, 123Iteration, ThisVar., @g_Variable
■ of any length, but only the first 31 characters are
required to be recognized by C standard compilers
■ not a keyword in C
🞐 C is case sensitive.
■ Global_counter is different from global_counter. 41
Variables and Variable Declaration
🞐 A data type of a variable is specified in its
declaration.
type_name variable_name_1 [= initial_value_1]
[, variable_name_2 [= initial_value_2]]
… [, variable_name_n [= initial_value_n]];
🞐 A compiler allocates memory for declared
variables up to the data type and its storage
class at run time.
🞐 A compiler associates variable_name to the
allocated memory.
🞐 A compiler sets initial_value to the content of
the allocated memory if initial_value exists. 42
Variable
Declaration
000000000022FE36
0
i
memory
43
Variables and Variable Declaration
🞐 Global variables
■ Declared outside of all the functions
■ Globally accessed inside of any functions
■ Hold values throughout the lifetime of a program
■ Initialized by the system once defined
🞐 Local variables
■ Declared and locally accessed inside a function
(main, others) or block between the brackets
■ Should be defined immediately after the left
bracket that begins the body of a function/block
■ Exist only as long as the function or block where
the variables are declared is still executing
■ Not initialized by the system once defined 44
A value of each local variable
Variables and Variable Declaration
can be set in its declaration.
Otherwise, local variables
start with random values in
their memory at run time.
45
Initialized values for global variables:
- char ‘\0’ (i.e. NULL)
- int 0
?
- float 0
- double 0
- pointer NULL
All the bytes in memory are filled with zeros.
46
Variables and Variable Declaration
🞐 The scope of a name is the part of the program within
which the name can be used.
🞐 A scope of a variable name is a region of the program
(function() {…}, block {..}) where a variable can have
its existence. Beyond that, it cannot be accessed.
🞐 For a variable declared at the beginning of a function,
the scope is the function where the name is declared.
■ Local variables of the same name in different functions are
unrelated.
■ The same is true for the parameters of the function, which are
in fact local variables.
🞐 The scope of an external variable or a function lasts
from the point at which it is declared to the end of the
file being compiled. 47
Variables and
Variable Declaration
gChar2 is unable to be accessed
in the main function due to its
improper declaration place!
48
gChar
bChar
cChar
49
The most “local”
variables will take
precedence over the
others.
#define MAX 50
61
Increment and Decrement
Operators
🞐 Increment and decrement operators: ++, --
preincrement
postincrement
predecrement
postdecrement
1 && 2 • 1 1 || 2 • 1 !1 • 0
1 && 1 • 1 1 || 1 • 1 !2 • 0
1 && 0 • 0 1 || 0 • 1 !-2 • 0
0 && 0 • 0 0 || 0 • 0 !0 • 1 64
Bitwise Operators
The binary bitwise operators are used to manipulate the bits of integral
operands (char, short, int and long; both signed and unsigned).
Unsigned integers are normally used with the bitwise operators.
Bitwise data manipulations are machine dependent. 65
Assignment Operators
🞐 Assignment operator: =
🞐 Assignment shorthand operators:
66
RHS = right hand side
Assignment Operators
🞐 Assignment operator: =
■ copies the value from its right hand side to the
variable on its left hand side
■ also acts as an expression which returns the
newly assigned value
1. Copy:
variable = RHS; int x=4, y=2;
x = y + 1;
2. return: RHS y = (x = x + 10);
• Data type of the variable and data type of RHS x=?y=?
must be the same.
Result:
• Otherwise, data type of RHS will be casted to
data type of the variable. x = 13, y = 13 67
Assignment Operators
🞐 Assignment operator: =
int x = 2, y = 3; int x = 2, y = 3;
char c; char c;
float f = 1.5;
char c;
y = f + x*2;
c = y*20 + x + 8*f;
f = (y = c - x*3);
x = f/5.0;
x=?y=?f=?c=?
70
Assignment Operators
🞐 Assignment shorthand operators:
x = x * (y-2); NOT: x = 71
RHS = right hand side
Comma Operators
🞐 Comma operator: ,
charVar3 = ‘C’;
intVar2 = 2 + charVar3 = 2 + ‘C’ = 2 + 67 = 69;
intVar1 = 2 + charVar3 = 69;
charVar1 = ‘A’;
charVar2 = ‘B’; 74
Comma Operators
🞐 Comma operator: ,
charVar3 = ‘C’;
intVar2 = 2 + charVar3 = 2 + ‘C’ = 2 + 67 = 69;
intVar1 = 2 + charVar3 = 69;
charVar1 = ‘A’;
charVar2 = ‘B’; 75
Conditional Operators
🞐 Conditional operator
<expression1> ? <expression2> : <expression3>
■ Evaluate <expression1>
■ If the value of <expression1> is true (non-zero),
evaluate and return <expression2>.
■ Otherwise, evaluate and return <expression3>.
min = ? min = 1; 76
Type Casting Operators
🞐 Type casting operator: (type) expression
■ Produces the value of expression in the type
char < short < int < long < float < double < long double
promotion
truncation
77
Operators and Expressions
78
Other Operators
Name Operator Description Example
sizeof sizeof(type), Returns the size (bytes) of sizeof(char)
sizeof(variable) a type or a variable
int anInt = 0;
sizeof(anInt);
address &Variable Returns the address of the char aChar;
memory named Variable char* ptrChar;
ptrChar = &aChar;
Dereferencing *Pointer Returns the value of the
aChar = *ptrChar + 1;
memory Pointer points to
Index Variable[..] Returns the element at the int intArray[3];
index
intArray[0] = 0;
intArray[1] = 1;
intArray[2] = 2;
anInt = intArray[1];
Structure Structure_ Refers to a member of a struct point pt;
member name.member particular structure pt.x = 10; 79
Higher
precedence
Lower
precedence
[2], pp. 53 80
Put them altogether
y
🞐 Write a program to describe a
rectangle by two opposite Vertex v2
Vertex v2
Vertex v1
v1.x, v1.y ?
v2.x, v2.y ?
82
Summary
🞐 How to handle data in a C program
■ Data types
🞐 Built-in: char, int, float, double, …, void
🞐 Derived: array, pointer, structure, union, enum
🞐 enum and structure for abstract data
🞐 More advanced types (array, pointer) come later.
■ Variables: declaration vs. definition
🞐 Naming
🞐 Storage
🞐 Type
🞐 Value
🞐 Scope 83
Summary
🞐 Constants
■ No change during the execution of the program
■ Known at the compile time
■ Defined with: #define, enum, const
🞐 Expressions: value, type
🞐 Operators
■ Assignment
■ Arithmetic
■ Bitwise
■ Logic
■ Relational and others
🞐 Type casting: explicit vs. implicit conversion 84
Summary
🞐 Data
■ Under control!
85
Chapter 3: Variables and
Basic Data Types
86