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.
2024-2025 ESII ALBACETE
CONTENTS
Data types
Pointers
2
DATA AND IDENTIFIERS
What is Data?
The raw information that a program processes and stores in memory.
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
Data representation in memory.
Different views.
1 or 0
4
DATA AND IDENTIFIERS
Data
Data representation in memory
5
DATA AND IDENTIFIERS
Data
Example:
Memory
Identifier: age
0001 Type: integer
0002 00000022 Address: 0002
age 0003 Value : 22
Scope: global
0004
6
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.
7
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.
I P
T
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.
8
DATA AND IDENTIFIERS
Data types
Pointers
10
DATA TYPES
11
DATA TYPES
12
DATA TYPES
Basic :: Character
Keyword: char
Stores a single symbol that will be represented internally by its ASCII code.
Size in memory: 1 byte 8 bits. Range: -128 to 127 (signed) 0 to 255 (unsigned).
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 "".
13
DATA TYPES
Basic :: Character
Keyword: char • Characters are internally represented as numbers in C.
• The computer maps each integer with the corresponding
Stores a single symbol that will be represented internally by its ASCII code.
character using a numerical code.
1 byte. • When we store a character in a char variable, we are actually
storing its number in the ASCII table.
Example: The character a will be represented as the
• Think about symbol
the ASCII as theand
code'a' the
order numeric
in which the
character 1 as '1'. symbols are stored.
• 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…
14
DATA TYPES
Basic :: Character
Another example:
The character 'A' is assigned the value 65 in the ASCII table.
In memory, the character 'A' is stored as an 8-bit sequence, with the values 01000001.
The character '1' is assigned the value 49 in the ASCII table.
In memory, the character '1' is stored as a sequence of 8 bits, with the values 00110001.
Therefore, since they are stored as the index in the table (a number), you can
perform arithmetic operations with them that are, in principle, not very intuitive:
'A' + 1 = 'B' 65 + 1 = 66
15
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.
16
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.
17
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.
4 bytes for the storage the range of values are from -2,147,483,648 to 2,147,483,647.
Relational operators and arithmetic operators can be applied to these data.
18
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.
19
DATA TYPES
Basic :: Floating-point:
CHAR_MAX : 127
CHAR_MIN : -128
Store Real numbers, with decimals.
UCHAR_MAX : 255
21
DATA TYPES
22
DATA TYPES
23
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.
24
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…
25
DATA TYPES
26
DATA TYPES
27
DATA TYPES
28
DATA TYPES
struct address
{
char name[30];
char street[40];
char city[20];
int zip;
};
30
DATA TYPES
31
DATA TYPES
32
CONTENTS
Data types
Pointers
33
OPERATORS AND EXPRESSIONS
Expressions
Valid combination of variables, constants, literals and operators.
Generates a value.
Operators:
Assignment.
Arithmetic
Relational
Logical
34
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;
Operators :: Assignment
Multiple assignments
x = y = z = 0;
36
OPERATORS AND EXPRESSIONS
x + y;
result = x + y;
37
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
40
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
41
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
43
OPERATORS AND EXPRESSIONS
Operators :: Logical
44
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
46
OPERATORS AND EXPRESSIONS
Operators :: Relational
Examples: int i, j, k;
i = 1;
j = 2;
k = 3;
47
OPERATORS AND EXPRESSIONS
Operators :: Relational
Examples:
48
OPERATORS AND EXPRESSIONS
Operators :: Logical
int i;
Examples: char c;
double f;
i = 7;
c = 'w';
f = 5.5;
49
OPERATORS AND EXPRESSIONS
51
OPERATORS AND EXPRESSIONS
52
CONTENTS
Data types
Pointers
53
POINTERS
54
POINTERS
Operators :: Pointers
Once the variable is defined, then we have two operators we can use:
57
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’.
58
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
59
0001:223D
POINTERS
61
POINTERS
62
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.
63