CSC 107 -Programming for Science
Lecture 5:
Actual Programming
Today’s Goal
At the end of today’s lecture, you should be able to write a reasonable C programThis includes input & output!We will have a real lab this week!
Variables
Variables name memory location for program to store dataVariable’s initial value is unknownAssignments update data stored at memory
locationVariable’s value used whenever program uses
variable
Data Types
Each variable also has data type Specifies how program treats variable’s value
C defines 6 numeric data types Integer types: short, int, long Decimal types: float, double, long double Does NOT specify ranges for each type
char data type holds a character C will only allow certain assignments
Assign an integer to a decimal variable --- easy Assign decimal to integer variable --- not easy
More Data Types
Each numeric data type is actually 2 typessigned (default) and unsignedint i, j, k;unsigned double l, m, n;
Unsigned data must be non-negativeUpper range of variable is doubledUseful for some data: degreesKelvin,
timeObservation, weight, homeworkScore C compiler emits warning when mixing
signed & unsigned data
Printing Out Results
C defines function to print information outFunction is called printf Information is printed out in window where
program is runArguments to printf determine what is
printed printf’s arguments can be very esoteric
Do not memorize; this is why manuals exist
printf
printf(“Hi, Mom\n”); First argument must be in quotes Except for a few magic commands, it
prints out what you specify\n print out newline\t print out tab\\ print out “\” character\b sound error bell
Even Better Magic
Must tell printf how to format variables
C will not complain for wrong specifier, but results are bad
Variable Type Specifierint %i, %dshort %hi, %hdlong int %li, %ldunsigned int %uunsigned short %huunsigned long %lufloat %f, %e, %E, %g, %Gdouble %lf, %le, %lE, %lg, %lGlong double %Lf, %Le, %LE, %Lg, %LGchar %c
Printing Out Data
Identifier placed in first argument Identifier replaced with value of variable, symbolic
constant, or literal
Values supplied in next arguments Must be included in order identifiers appear
caveat emptor when using function Compiler does NOT check identifiers or argument Too many can cause later problems Too few arguments results in odd printouts
printf Examples
double d;int i;unsigned int u;printf(“This is a boring example.\n”);
printf(“Prints a double -- %f\n”, d);
printf(“Prints 2 digits beyond decimal -- %.2f\n”, d);
printf(“Prints an int -- %d\n”, i);
printf(“Prints an unsigned int %u\n”, u);
printf(“Prints i’s (%d) & u’s (%u) value”, i, u);
printf(“i + u = %u\t i + d = %d\n”, i+u, i+d);
printf(“Printing a percent sign is 100%% hard”);
scanf
C defines scanf to get user’s inputVery similar in usage to printfSets variables equal to values input on
keyboard scanf waits until it has input
Wait could be years if nothing is inputLines cannot be read until enter is pressed
Arguments to scanf
First argument must be in quotes Must state type of data to be read
Uses same identifiers as printf Afterward specify variables to assign
Data types should match (otherwise program results will be odd)
Need to list “&” before each variable name
scanf Examples
double d;int i;unsigned int u;scanf(“%u”, &u);
scanf(“%f %d\n”, &d, &i);
scan(“%f\n%d”, &d, &i);
scanf(“%f%%\n”, &d); Ampersand (&) before each variable name is vital
Needed to assign variable a new value
Your Turn
Get into groups and complete daily activity
For Next Lecture
Read through Section 2.6 of bookDo not need to understand all the detailsBut important knowing what is not understood
Start homework assignment for week 3Covers material from this week’s lectures