Data and I/O
03603111 Programming Fundamentals I
Department of Computer Engineering, Faculty of Engineering at Sriracha
Course outline and schedule
2
Overview
3
Previous lesson recaps
4
Let’s make it clear once again
5
Basic computer architecture
6
Image by Amila Ruwan 20 - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=95966225
Program execution
7
Fetch
Decode
Execute
Program translation
8
Compiler
Linker
Source file(s) (.c)
Object file(s)
Executable file
Included libraries
Building process
Relationship between base-2, -10, and -16 numbers
9
If we keep going up, we’ll have:
… and so on …
Working with data
What we’re going to cover today
#include <stdio.h>
int main()
{
// Variable declarations
int height;
float weight;
float bmi;
int cm2_to_m2 = 10000;
// Read body measurement
printf("Enter height (cm): ");
scanf("%d", &height);
printf("Enter weight (kg): ");
scanf("%f", &weight);
// Compute body mass index (BMI)
bmi = weight / (height * height) * cm2_to_m2;
printf("BMI = %f\n", bmi);
return 0;
}
Types of the variables
A literal or “written value” – used here to initialize the cm2_to_m2 variable
An expression – something that can be evaluated to a value
Variables – used for storing values
Another integer literal – a literal is by itself also an expression, as it can be evaluated to a value
Statements specify actions to be carried out – a program is a sequence of statements
Programs and data
12
Basic C data types
13
Type | Description |
char | 1-byte character or integer |
int | Basic integer type |
float | 32-bit (4-byte) “single-precision” floating-point number |
double | 64-bit (8-byte) “double-precision” floating-point number |
Types define a set of possible values and operations
14
Values and types
* Literals mean “written values”
15
Variables
16
Declaring variables
int height; // single variable
float weight, bmi; // declare two variables at once
int cm2_to_m2 = 10000; // declare and initialize its value
17
Default values and initialization
int count;
int height;
printf("%d, %d\n", count, height);
~> 4156548, -538089401
18
C identifiers
19
C naming conventions
20
INTERESTING BITS
Expressions
21
Arithmetic operators, precedence, and grouping
22
Operator | Description |
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
% | Modulo |
Operator | Associativity | Precedence |
( ) | left to right | highest |
+ - (unary) | right to left | … |
* / % | left to right | … |
+ - (binary) | left to right | lowest |
Statements
printf("Enter weight (kg): "); // output statement
scanf("%f", &weight); // input statement
/* Below is an assignment statement */
bmi = weight / (height * height) * cm2_to_m2;
return 0; // return statement
23
Assignment operators
24
Operator | Description |
= | Assign |
+= | Add to |
-= | Subtract from |
*= | Multiply assign |
/= | Divide assign |
%= | Modulo assign |
Note that = is for assignment, not equality
Inputs and outputs
Formatted printing
printf("BMI = %f\n", bmi);
printf(format-string, arg1, arg2, …);
26
Format strings and format specifiers
printf("%d is equal to 0x%x (%X)\n", 1234, 1234, 1234);
~> 1234 is equal to 0x4d2 (4D2)
printf("%d is also the ASCII code of '%c'\n", 70, 70);
~> 70 is also the ASCII code of 'F’
printf("%f + %.1f = [%8.2f]\n", 12.5f, 21.3f, 12.5f + 21.3f);
~> 12.500000 + 21.3 = [ 33.80]
// '0' flag -> prefixed with 0s, '-' flag -> left-aligned
printf("Mr.%s, %06d is billed at [%-4d]PM.\n", "Kim", 25, 12);
~> Mr.Kim, 000025 is billed at [12 ]PM.
// Note the inaccuracy in the output
printf("A large float: %f\n", 2.7182818284e20);
~> A large float: 271828182839999987712.000000
printf("There's a %u%% OFF SALE right now.\n", 50);
~> There's a 50% OFF SALE right now.
27
%d | int (decimal) |
%u | unsigned int (decimal) |
%x, %X | int (hexadecimal) |
%c | char (character) |
%f | float |
%lf | double |
%s | char[] (string) |
%% | Printing % |
| flag | field width | precision | type |
% | -, 0 | 8 | .2 | f |
Reading inputs
scanf("%d %f", &height, &weight);
scanf(format-string, &var1, &var2, …);
28
Understanding scanf() format specifiers
These are 3 simplified rules for using scanf() (assuming the user enters input correctly):
29
scanf() examples (red is user inputs)
int a; char c, d, e; float x; double y;
scanf("%d %f %c", &a, &x, &c);
123 4.5 x
~> 123, 4.5, 'x'
scanf("%d%lf%c", &a, &y, &c);
123 4.5 x
~> 123, 4.5, ' '
25⏎
3.9f
~> 25, 3.9, 'f'
scanf("%c%c%c", &c, &d, &e);
A1$2
~> 'A', '1', '$'
A 1 $ 2 // Note the leading space
~> ' ', 'A', ' '
scanf(" %c %c %c", &c, &d, &e);
A1$2
~> 'A', '1', '$'
A 1 $ 2 // note the leading space
~> 'A', '1', '$'
scanf("%d", &a);
scanf("%c", &c);
42⏎
x
~> 42, '⏎' // it won’t even wait for 'x'
scanf("%d", &a);
scanf(" %c", &c);
42⏎
x
~> 42, 'x'
30
Skip any leading whitespace
Use %lf for double
BMI example revisited
#include <stdio.h>
int main()
{
int height;
float weight;
float bmi;
int cm2_to_m2 = 10000;
printf("Enter height (cm): ");
scanf("%d", &height);
printf("Enter weight (kg): ");
scanf("%f", &weight);
bmi = weight / (height * height) * cm2_to_m2;
printf("BMI = %.2f\n", bmi);
return 0;
}
31
Enter height (cm): 168
Enter weight (kg): 62
BMI = 21.97
Result:
More on types
Integer types with type modifiers
33
Modifier(s) | Type | Minimum Size (bytes) | Range |
signed | char | 1 | -128 to 127 |
unsigned | 1 | 0 to 255 | |
short | int | 2 | -32,768 to 32,767 |
unsigned short | 2 | 0 to 65535 | |
signed | 2 (normally 4) | Refer to either above or below | |
unsigned | 2 (normally 4) | ||
long | 4 | -2,147,483,648 to 2,147,483,647 | |
unsigned long | 4 | 0 to 4,294,967,295 | |
long long | 8 | -263 to 263-1 | |
unsigned long long | 8 | 0 to 264-1 |
IMPORTANT: Assignments or operations that produce values beyond the range of the type will cause numerical overflow or wrapping
What data type should be used for the followings?
34
char numeric character code usually follows the ASCII standard
35
By Shabaz1000 - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=150153452
Two sides of a char
char c, d, e, f;
c = 'A';
d = 65;
e = 'a';
f = 97;
printf("%c %c %c %c\n", c, d, e, f);
~> A A a a
printf("%d %d %d %d\n", c, d, e, f);
~> 65 65 97 97
// Move up 3 characters
c += 3;
printf("'%c' = %d\n", c, c);
~> 'D' = 68
// Character distance
printf("'G'-'%c' = %d\n", c, 'G'-c);
~> 'G'-'D' = 3
// Numeric characters aren't numbers!
c = '8';
d = '1';
printf("c - d = %d\n", c - d);
~> c - d = 7
printf("c + d = %d\n", c + d);
~> c + d = 105
36
Literal value suffixes
37
INTERESTING BITS
Abstraction
What’s wrong with this code?
#include <stdio.h>
int main()
{
float r = 5.0;
printf("Radius for the circle = %f\n", r);
printf("Area = %f\n", 3.14159 * r * r);
printf("Circumference = %f\n", 2 * 3.14159 * r);
return 0;
}
39
The value 3.14159 is used in many (two) places
Why does it matter?
40
#include <stdio.h>
#define PI 3.14159
int main()
{
float r = 5.0;
printf("Radius for the circle = %f\n", r);
printf("Area = %f\n", PI * r * r);
printf("Circumference = %f\n", 2 * PI * r);
return 0;
}
41
Benefits of using constant values
Macro (constant value)
Naming a value is a kind of abstraction
42
More naming conventions
43
INTERESTING BITS
Types and limitations
44
What follows is an advanced topic –
not required, but may deepen your understanding
45
Representing integers
46
Integer overflow
47
Representing real numbers
48
Representing real numbers
49
Floating-point types
50
That’s all for today!
51