0% found this document useful (0 votes)
21 views

Week 1, Continued: 1. Imprecision

This document summarizes key points from a CS50 lecture on programming in C. It discusses numerical imprecision in computers due to limited memory. It also announces upcoming problem sets and office hours, and provides an overview of basic C syntax and data types like integers, floats, characters, and strings. Functions like printf are demonstrated using volunteers to represent computation.

Uploaded by

Kathryn Cotton
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Week 1, Continued: 1. Imprecision

This document summarizes key points from a CS50 lecture on programming in C. It discusses numerical imprecision in computers due to limited memory. It also announces upcoming problem sets and office hours, and provides an overview of basic C syntax and data types like integers, floats, characters, and strings. Functions like printf are demonstrated using volunteers to represent computation.

Uploaded by

Kathryn Cotton
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Week 1, continued

This is CS50. Harvard University. Fall 2015.

Anna Whitney

Table of Contents
1. Imprecision................................................................................................................... 1
2. Announcements........................................................................................................... 2
3. C................................................................................................................................... 2
4. Types............................................................................................................................ 3
4.1. Conditions.......................................................................................................... 4
4.2. Loops................................................................................................................. 6
4.3. Integer Overflow................................................................................................ 7
4.4. Loops, continued............................................................................................... 8
4.5. Variables............................................................................................................ 8
4.6. Functions and Arguments.................................................................................. 9
5. Problem Set 1............................................................................................................. 11

1. Imprecision

Last week, we saw in imprecision.c1 that 1.0/10.0 does not in fact equal 0.1
as expected. Well, actually it does equal 0.1 , of course - but the computer gets it a
little bit wrong.

Computers only have a finite amount of memory, so they have to pick and choose what
values theyre going to support.

If we only have 8 bits, we can only represent 256 values (and since we use one of those
values for zero, the greatest number we can represent is 255).

Floating point values are stored a little differently, but the computer still only uses
typically either 32 or 64 bits to store a floating point numberso it cant possibly
represent infinitely many values.

1
http://cdn.cs50.net/2015/fall/lectures/1/f/src1f/imprecision.c

1
Week 1, continued

When we get these inaccuracies after many decimal places, were running
up against the hardware limitations of the computer.

Paying attention to how numbers are stored in your code can be critical
this clip from
Modern Marvels2 shows how disasters can result when numerical
imprecision isnt
taken into account in high-precision systems.

2. Announcements
Supersections this weekend; they will be filmed and streamed live for
those unable to attend.
Problem Set 1 is live on the course website, and due next Thursday.
Office hours will take place Monday through Thursday this week.

3. C
Lets return to our canonical program from last time:

#include <stdio.h>

int
main
(voi
d)
{
printf("hello,
world"\n);
}

Recall that #include <stdio.h> allows us to use the functions of the


standard I/O library, written by other programmers in the past, and declared in
a file called stdio.h elsewhere in our system.

We introduced main last week as the analog of Scratchs [ when


[green flag]
clicked ] block. In C and several other languages, your first function must
be called
main . Well explain later what int and void are doing here.
printf is a function that prints out a formatted string. It takes one or more
arguments
(also known as parameters or simply inputs). The first is a string - a word or
phrase or
even a whole essay - which you usually want to terminate with a \n to
ensure that
the output ends with a newline. Subsequent arguments tell printf what
values to

2
http://youtu.be/7yFh7v6XMTo?t=4m1s

2
Week 1, continued

fill in for any format strings (such as %f for a floating-point number) that
you included in the first string argument.
# \n in the above is whats known as an escape character - rather
than being interpreted literally as a backslash and the letter n, it tells
the compiler to do something else. In this case, that "something else" is
starting a new line.
Semicolons indicate the end of statements, and curly braces delineate
blocks of code (like the structures of control puzzle pieces in Scratch).
Volunteer Kopal acts as the printf function, accepting input from
David (acting as the hello program that calls printf ) written on a piece
of paper and writing the input given on the touchscreen, simulating the
effect of printf .
Another volunteer, Ife, represents the GetString function. Kopal, as
printf , writes "State your name" on the touchscreen. Ife then gets a name
from the audience and brings it back. David stores the returned name,
"Nik", in a variable called s (by writing it on a sheet of paper labeled s ).
David now gives Kopal/ printf a sheet of paper that says hello,
%s\n and the sheet of paper containing the value of s . He fills in the
name stored in the variable s , "Nik", in place of the placeholder %s .
This same model of message passing underlies all the code we write, as
outputs of functions are passed as inputs to other functions and so on.

4. Types
Weve been talking mostly about strings thus far, but values in C can have
a few other
types:
# char , a single character (like a or 7 or % ), which takes up one
byte, or 8 bits; uses a printf format code of %c
# float , a floating-point value (a number with a decimal point, like

10.0 or
3.14159 ), which takes up 32 bits (four bytes); %f
# double , a floating-point number that takes up twice as much space
as a float (so 64 bits/8 bytes); also %f
# int , an integer, also 32 bits/4 bytes, meaning that the largest
integer we can represent is roughly 4 billion; %i or %d
# longlong , a 64 bit integer (which can represent much larger values!);
'%lld`

3
ATTENTION!
TRIAL LIMITATION - ONLY 3 SELECTED PAGE(S) MAY BE CONVERTED PER CONVERSION.
PURCHASING A LICENSE REMOVES THIS LIMITATION. TO DO SO, PLEASE USE THE FOLLOWING LINK:
http://www.pdfconverter.com/purchase/

You might also like