02 - Unit 2. Data Types
02 - Unit 2. Data Types
MEMORY
ARTURO S. GARCÍA PROGRAMMING FUNDAMENTALS 1
UNIVERSITY OF CASTILLA-LA MANCHA
DEGREE IN COMPUTER SCIENCE AND ENGINEERING.
2022-2023 ESII ALBACETE
CONTENTS
Data types
Pointers
2
DATA AND IDENTIFIERS
Data
Memory area in which the information processed by a program is stored.
Characteristics defined by the programmer:
Identifier.
Type.
Value.
Characteristics assigned at declaration:
Memory address in which data is stored.
Scope.
3
DATA AND IDENTIFIERS
Data
Example:
Memory
Identifier: age
0001 Type: integer
0002 00000022 Address: 0002
age 0003 Value : 22
Scope: global
0004
4
DATA AND IDENTIFIERS
Data :: Identifiers
Label or name that the programmer assigns to each individual data in order
to reference them later.
General rules:
Only letters, numbers and the underlined character (_) are allowed.
The first character cannot be a number.
Spaces or characters such as . ; * : - + etc. are not allowed.
The identifiers have a maximum length of characters.
They can’t coincide with the keywords of the language.
Case sensitive: The upper- and lower-case letters are different.
5
DATA AND IDENTIFIERS
Data :: Identifiers
Label or name that the programmer assigns to each individual data in order
to reference them later.
General rules:
Only letters, numbers and the underlined character (_) are allowed.
The first character cannot be a number.
Spaces or characters such as . ; * : - + etc. are not allowed.
The identifiers have a maximum length of characters.
They can’t coincide with the keywords of the language.
Case sensitive: The upper- and lower-case letters are different.
6
DATA AND IDENTIFIERS
Data types
Pointers
8
DATA TYPES
9
DATA TYPES
10
DATA TYPES
Basic :: Character
Keyword: char
Stores a single symbol that will be represented internally by its ASCII code.
1 byte.
Example: The character a will be represented as the symbol 'a' and the numeric
character 1 as '1'.
Operators:
Relational operators, also called comparison operators.
Addition and subtraction of an integer.
Several characters must be enclosed in double quotes "".
11
DATA TYPES
Basic :: Character
Keyword: char
• Characters are internally represented as numbers in C.
Stores a single symbol that will be represented internally by its ASCII code.
• The computer maps each integer with the corresponding
1 byte. character using a numerical code.
• Think about the ASCII code as the order in which the
Example: The character a will be represented as the
symbols are symbol
stored. 'a' and the numeric
character 1 as '1'. • The 48th symbol is ‘0’, which is not a number!
• You can use ‘0’ and the index 48 to refer to the
same symbol.
• Do not think ‘0’ is a number or you will get
confused…
12
DATA TYPES
Basic :: Character
Keyword: char
Stores a single symbol that will be represented internally by its ASCII code.
1 byte.
Example: The character a will be represented by the symbol 'a' and the numeric
character 1 as '1'.
Operators:
Relational operators, also called comparison operators.
Addition and subtraction of an integer.
Several characters must be enclosed in double quotes "".
13
DATA TYPES
Basic :: Character
Sometimes, it is necessary to use characters that cannot be typed or that
have a special meaning in the C language.
14
DATA TYPES
Escape sequence Description
Basic :: Character
\' (single quote) Prints the single quote character (').
Sometimes, it is necessary
\" (double quote) toPrints
use the
characters
double quotethat cannot
character ("). be typed or that
have a special meaning
\? (question mark) in the Prints
C language.
the question mark character (?).
\\ (backslash) Prints the backslash character (\).
\a (alert or bell) Cause an audible (bell) or visual alert.
\b (backspace) Move the cursor back one position on the current line.
\f (new page or form feed) Move the cursor to the start of the next logical page.
\n (newline)
Use Move the cursor to the beginning of the next line.
escape sequences
\r (carriage return) Move the cursor to the beginning of the current line.
\t (horizontal tab) Move the cursor to the next horizontal tab position.
\v (vertical tab) Move the cursor to the next vertical tab position.
15
DATA TYPES
Basic :: Integer
Keyword: int
Stores integer values: numbers without decimals.
The range of values allowed for this type will depend on the compiler being used,
normally:
2 bytes for the storage the range of values are from -32768 to 32767.
Relational operators and arithmetic operators can be applied to these data.
16
DATA TYPES
Basic :: Floating-point:
Store Real numbers, with decimals.
4 bytes are usually used for storage.
Relational and arithmetic operations can be applied (except the remainder
of the division operator).
Two data types:
Keyword: float: 4 bytes. -3.4e-38 to 3.4e+38.
Keyword: double: 8 bytes. -1.7e-308 to 1.7e+308.
17
DATA TYPES
Basic :: Floating-point:
CHAR_MAX : 127
CHAR_MIN : -128
Store Real numbers, with decimals.
UCHAR_MAX : 255
19
DATA TYPES
20
DATA TYPES
21
DATA TYPES
Basic :: Boolean:
The allowed values for data of this type are only two, TRUE and FALSE.
Only Boolean operators (AND(&&), OR(||) and NOT(!)) can be applied to
this type of data.
Not completely “built-in”. Alternatives:
Traditional:
int: Use integers: zero means false, nonzero means true.
Only with compilers that support C99:
_Bool. ‘0’ means false, ‘1’ means true.
Including <stdbool.h>. ‘bool’, ‘true’ and ‘false’ can be used.
22
DATA TYPES
Basic :: Pointer:
A pointer is the memory address where the data is stored.
A pointer variable is declared to hold a pointer to an object of a specified
type.
Pointers are one of C's most powerful features, and they are used for a
wide variety of purposes.
And one of the most complex features to master…
23
DATA TYPES
24
DATA TYPES
25
DATA TYPES
26
DATA TYPES
struct address
{
char name[30];
char street[40];
char city[20];
int zip;
};
28
DATA TYPES
30
CONTENTS
Data types
Pointers
31
OPERATORS AND EXPRESSIONS
Expressions
Valid combination of variables, constants, literals and operators.
Generates a value.
Operators:
Assignment.
Arithmetic
Relational
Logical
32
OPERATORS AND EXPRESSIONS
Operators :: Assignment
The assignment operator can be used within any valid expression.
The general form of the assignment operator is:
variable_name = expression;
Example: var a b
const int var = 43; 43 8 43
int a = 8; 39
int b = var;
a = b - 4;
33
OPERATORS AND EXPRESSIONS
Operators :: Assignment
Multiple assignments
x = y = z = 0;
34
OPERATORS AND EXPRESSIONS
x + y;
result = x + y;
35
OPERATORS AND EXPRESSIONS
Examples:
double result; double result;
int a; int a;
a = 45; a = 45;
result = a / 2; result = ((double) a / 2);
Examples:
double result; double result;
int a; int a;
a = 45; a = 45;
result = a / 2; result = ((double) a / 2);
Operators :: Arithmetic
38
OPERATORS AND EXPRESSIONS
Operators :: Arithmetic
‘++’ and ‘--’
x = x + 1; is the same as x++; or x += 1;
x = 10; x = 11 x = 10; x = 11
y = ++x; y = 11 y = x++; y = 10
Prefix Postfix
39
y = (x++) (y = x)++
OPERATORS AND EXPRESSIONS
Operators :: Arithmetic
‘/’ and ‘%’. Example:
int x, y, result;
x = 5;
y = 2;
result = x / y result = x % y
2 1
Operators :: Relational
41
OPERATORS AND EXPRESSIONS
Operators :: Logical
42
OPERATORS AND EXPRESSIONS
Operators :: Logical
Operators :: Logical
P Q P && Q P || Q !P
false false false false true
false true false true true
true false false true false
true true true true false
44
OPERATORS AND EXPRESSIONS
Operators :: Relational
Examples: int i, j, k;
i = 1;
j = 2;
k = 3;
45
OPERATORS AND EXPRESSIONS
Operators :: Relational
Examples:
46
OPERATORS AND EXPRESSIONS
Operators :: Logical
int i;
Examples: char c;
double f;
i = 7;
c = 'w';
f = 5.5;
47
OPERATORS AND EXPRESSIONS
: Use brackets! 48
OPERATORS AND EXPRESSIONS
49
OPERATORS AND EXPRESSIONS
50
CONTENTS
Data types
Pointers
51
POINTERS
Operators :: Pointers
Once the variable is defined, then we have two operators we can use:
53
POINTERS
Operators :: Pointers
Example: int count = 0;
int *m;
m = &count;
Places into ‘m’ the memory address of the variable ‘count’.
int q;
q = *m;
Places the value of ‘count’ into the variable ‘q’.
54
POINTERS
Identifier: ptr_temperature
memory Type: pointer
0001:2000 0001:223A Storage address: 0001:2000
0001:2001 Scope: global
0001:2002
Value: 0001:223A
0001:2003
Identifier: val_temperature
0001:223A 00000022 Type: integer
0001:223B Storage address: 0001:223A
0001:223C
Scope: global
Value: 22
55
0001:223D
POINTERS
57
POINTERS
58
POINTER RULES SUMMARY
No matter how complex a pointer structure gets, the list of rules remains
short.
A pointer stores a reference to its pointee (variable the pointer refers to).
The pointee, in turn, stores something useful.
The * operation on a pointer accesses its pointee. A pointer may only be accessed
after it has been assigned to refer to a pointee.
Most pointer bugs involve violating this one rule.
Allocating a pointer does not automatically assign it to refer to a pointee.
Assigning the pointer to refer to a specific pointee is a separate operation which is easy to
forget.
Assignment between two pointers makes them refer to the same pointee, which
introduces sharing.
59