26
LECTURE 38: C PROGRAMMING & I/O CSC 107 – Programming For Science

CSC 107 – Programming For Science. Today’s Goal Learn C functions to input and output data Using printf to print out variables and strings Read

Embed Size (px)

Citation preview

LECTURE 38:C PROGRAMMING & I/O

CSC 107 – Programming For Science

Today’s Goal

Learn C functions to input and output data Using printf to print out variables and

strings Read in values typed in using scanf

Variables

Variable gives name to location to store data Memory location's initial value is unknown Assignments update memory location with

new value Memory location updated by assignment

ONLY When variable is used in program…

…uses current value at that memory location

Just about everything (interesting) uses variables

Print Results w/ printf

Cannot use cin & cout with standard C code Must instead use the C functions for these

actions printf used to print out data in C

program Like cout, prints results in window where it

is run printf's arguments determine what is

printed Arguments to printf extremely

esoteric This is why manuals exist; do not memorize

cout vs. printf

cout printf

Simple Easy-to-use Clear Limited need to

memorize Fun Elegant

Complex Lots of knobs &

buttons Obscure Lots to look up Hateful Ugly

cout vs. printf

cout printf

printf

#include <stdio.h>#include <stdlib.h>

printf("Hi, Mom\n"); First argument must be in quotes Prints out what you specify, with few

exceptions\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

No error on wrong specifier, just printed wrong

Variable Type Specifierint %i, %dshort %hi, %hdlong %li, %ldfloat %f, %e, %E, %g, %Gdouble %lf, %le, %lE, %lg, %lGlong double %Lf, %Le, %LE, %Lg, %LGchar %ccString %s

Printing Out Data

1st argument includes all specifiers Actual value replaces specifier when printed out The first argument also can (should) include

text

Values supplied in next arguments to the function Values may include literals, expressions, or

variables Must appear in order of specifiers from the first

argument

Printing Out Data

Beware when using function There will be NO checks on identifiers or

argument Problems later if too many arguments Printing shows really odd results if too few

arguments

Nothing forces types to match specifier

printf Examples

#include <stdio.h>#include <stdlib.h>

int main() {

double d = 1.22;int i = -2;unsigned int u = 7;printf(“This is a boring example.\n”);printf(“Double -- %lf\n”, d);printf(“2 digits after -- %.2lf\n”, d);printf(“Int -- %d\n”, i);printf("%d", i);printf(“Unsigned int -- %u\n”, u);printf(“%d & %u”, i, u);printf(“%u\t %d\n”, i + u, i + d);printf(“100%% proof”);

scanf

scanf gets user’s input when working in C Function that resembles usage of printf Behaves like cin in C++, but works very

differently

cin vs. scanf

cin scanf

Arguments to scanf

Must use quotes for 1st argument Only for first argument & not the others,

however Use identifiers to state type of data to

be read printf's identifiers used also in scanf

State variables to assign in next arguments Data types must match, but no error if they

do not Uses pass-by-pointer, so must pass

variable's address

scanf Examples

#include <stdio.h>#include <stdlib.h>

int main() {float fl;int i;unsigned int u;char str[80];scanf(“%u”, &u);scanf(“%f %d\n”, &fl, &i);scanf(“%f\n%d”, &fl, &i);scanf(“Bob: %f\n”, &fl);scanf(“%f%%\n”, &fl);scanf(“%s\n”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]); scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]); scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]); scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]); scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]);scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]);scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]);scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]);scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Will It Compile & Work?

double d;int i;int arr[10];char str[100];double *pD = &d;scanf(“%d”, &i);scanf(“%lf %d\n”, &d, &i);scanf(“%lf”, &pD);scanf(“%d”, arr[5]);scanf(“%d”, &arr[1]);scanf(“%d %d %d %d %d %d %d %d %d %d”,arr);scanf(“%lf %lf”, pD, &d);scanf(“%s”, str);

Your Turn

Get into your groups and try this assignment

For Next Lecture

Read Section 16.4 on macros for Monday How can we write simple, fast little

functions? What is use of #define statements anyway? What is the purpose of writing a macro

function?

Angel has Weekly Assignment #14 due Tuesday

Last programming assignment due in 1 week