04 constants and expressions, Lecture notes of C programming

Constants and Expressions

Typology: Lecture notes

2013/2014

Uploaded on 12/29/2014

wsabren1..
wsabren1.. 🇮🇶

4

(5)

11 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Constants &
Expressions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download 04 constants and expressions and more Lecture notes C programming in PDF only on Docsity!

ExpressionsConstants &

let’s look at thatlet’s look at thatlet’s look at thatlet’s look at that

temperature example againtemperature example againtemperature example againtemperature example again

/ 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; }**

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?

Write the list of Fahrenheit temperatures

the left column of the table

For each temperature in 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

Start with the first F temperature (0)

Do each line fully

calculate a Celsius equivalent

print out the whole line

Fahrenheit and Celsius temperatures

by 20Now increment the last Fahrenheit temperature

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

Why bother?

clarity

increased range

clarityclarity clarityclarity

Some data is inherently unsigned

unlike temperatures

number of things

you can’t have –3 oranges

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

  • number we’re left with 7 bits for the number

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

But it’s similar

one bit is a “sign bit”

the others represent values

the details aren’t important

type conversionstype conversions type conversionstype conversions

C can sometimes convert between different types

often it’s just common sense

smaller to larger

char to int

int to long

less precise to more precise

float to double

int to double

what does this mean?what does this mean? what does this mean?what does this mean?

We can do things like this:

x = i;i = 1;double x; int i;

numerical constantsnumerical constants numerical constantsnumerical constants

Always has a data type

usually implied

sometimes explicit

The computer can sometimes convert data types

from “smaller” to “larger”

short to int

int to long

float to double

from less accurate to more accurate

int to double

using numerical constantsusing numerical constants using numerical constantsusing numerical constants

what type constant isSo you don’t usually have to worry about

double radius = 4;double pi = 3.1415 int i = 34;

we won’t bother with explicit types for constants

i.e., all those L and F suffixes