Lecture03 - Copy
Lecture03 - Copy
University of Ottawa
Winter 2025
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
In-Class Exercise: Go To Brightspace
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Conversion Specifiers
Outline
1 Conversion Specifiers
2 More on Types
3 Arithmetics
4 Math Library
5 Cast Operator
6 Symbolic Constant
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Conversion Specifiers
Conversion specifiers for scanf and for printf are not always the
same (check type double).
“%e” and “%E” are for scientific notations: 1.3e3 and 1.3E3
You do not need to remember this entire table. Just the common
ones (those for int, float, double) would be good enough.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Conversion Specifiers
By running this code, can you figure out what %x.yf does?
#include <stdio.h>
int main()
{
printf("%5.3f\n", 3.14);
printf("%7.3f\n", 3.14);
printf("%7.3f\n", 3.14345);
printf("%7.3f\n", 3.14365);
printf("%5.3f\n", 321.14);
return 0;
}
Outline
1 Conversion Specifiers
2 More on Types
3 Arithmetics
4 Math Library
5 Cast Operator
6 Symbolic Constant
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Types
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Types
Binary Representation
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Types
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Types
Fixed-Point Encoding
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Types
Fixed-Point Encoding
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Types
Fixed-Point Encoding
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Types
#include <stdio.h>
int main()
{
printf("Size of char is %ld bytes\n", sizeof(char));
printf("Size of short is %ld bytes\n", sizeof(short));
printf("Size of int is %ld bytes\n", sizeof(int));
printf("Size of long is %ld bytes\n", sizeof(long));
printf("Sizes of unsigned long is %ld bytes\n",
sizeof(unsigned long));
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Types
Floating-Point Encoding
Outline
1 Conversion Specifiers
2 More on Types
3 Arithmetics
4 Math Library
5 Cast Operator
6 Symbolic Constant
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Arithmetics
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Arithmetics
Operator Precedence
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Arithmetics
#include <stdio.h>
int main()
{
int x=1;
printf("1 is represented by %ld bytes\n", sizeof(1));
printf("5.0 is represented by %ld bytes\n", sizeof(5.0));
printf("1+5.0 is represented by %ld bytes\n", sizeof(1+5.0));
printf("x is represented by %ld bytes\n", sizeof(x));
printf("(1+5.0)*x is represented by %ld bytes\n", sizeof((1+5.0)*x));
return 0;
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Arithmetics
Examples
Expression Value
3/4 0
3.0/4 0.75
0.3+3/4 0.3
0.3+3/4.0 1.05
(0.3+3)/4 0.825
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Math Library
Outline
1 Conversion Specifiers
2 More on Types
3 Arithmetics
4 Math Library
5 Cast Operator
6 Symbolic Constant
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Math Library
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Math Library
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Cast Operator
Outline
1 Conversion Specifiers
2 More on Types
3 Arithmetics
4 Math Library
5 Cast Operator
6 Symbolic Constant
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Cast Operator
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Cast Operator
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Symbolic Constant
Outline
1 Conversion Specifiers
2 More on Types
3 Arithmetics
4 Math Library
5 Cast Operator
6 Symbolic Constant
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Symbolic Constant
Symbolic Constant
Highlight
The use of symbolic constants can eliminate “magic numbers” and
makes the program more readable and easier to maintain.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Symbolic Constant
Programming Example
Write a program that asks the user to enter two integer values
representing the radii of two circles in unit of centimeter, and prints
the perimeters of the two circles and their ratio. Use a symbolic
constant for PI.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Symbolic Constant
Coding Demonstration
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .