

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Constants and Expressions
Typology: Lecture notes
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















/ FtoC*
**- print a Fahrenheit-to-Celsius
conversion table */ #include <stdio.h> int main (void) { int fahr; int celsius; int lower = 0; int upper = 300; int step = 20; fahr = lower; while (fahr <= upper) { celsius = 5
/ 9; printf (“%d\t%d\n”, fahr, celsius); fahr = fahr
step; return} 0; }**
how wouldhow would how wouldhow would youyouyouyou do it? do it?do it?do it?
Say I told you to calculate this table
for each Fahrenheit temperature
0 to 300° F
in steps of 20 ° F
you have a calculator
you know that C = 5/9 (F - 32)
what might you do?what might you do? what might you do?what might you do?
the left column of the table
plug it into the equation
calculate a Celsius temperature
write it in the table to the right of the F temperature
so the program approachesso the program approachesso the program approachesso the program approaches it differentlyit differentlyit differentlyit differently
calculate a Celsius equivalent
print out the whole line
if we haven’t gone over our limit, go back to step 2
the program once morethe program once more the program once morethe program once more */
FtoC**
**- print a Fahrenheit-to-Celsius
conversion table */ #include <stdio.h> int main (void) { int fahr; int celsius; int lower = 0; int upper = 300; int step = 20; fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr
/ 9; printf (“%d\t%d\n”, fahr, celsius); fahr = fahr + step; return} 0; }**
signed/unsignedsigned/unsigned signed/unsignedsigned/unsigned
clarityclarity clarityclarity
unlike temperatures
number of things
dates
negative numbersnegative numbers negative numbersnegative numbers
let’s “steal” one bit to indicate +/-
say, 0 is positive, 1 is negative
that way, a positive number is the same as an unsigned
7 bits can represent a number from 0 to 2 7 -1 (which is 127)
so this way, we could have 0 – 127, either + or –
it doesn’t exactly workit doesn’t exactly workit doesn’t exactly workit doesn’t exactly work that waythat waythat waythat way
one bit is a “sign bit”
the others represent values
the details aren’t important
type conversionstype conversions type conversionstype conversions
often it’s just common sense
smaller to larger
less precise to more precise
what does this mean?what does this mean? what does this mean?what does this mean?
x = i;i = 1;double x; int i;
numerical constantsnumerical constants numerical constantsnumerical constants
usually implied
sometimes explicit
from “smaller” to “larger”
from less accurate to more accurate
using numerical constantsusing numerical constants using numerical constantsusing numerical constants
double radius = 4;double pi = 3.1415 int i = 34;
i.e., all those L and F suffixes