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

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.

Dec 24, 2015

Download

Documents

Felix Farmer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

LECTURE 38:C PROGRAMMING & I/O

CSC 107 – Programming For Science

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

cout vs. printf

cout printf

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

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”);

Page 12: 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.

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

Page 13: 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.

cin vs. scanf

cin scanf

Page 14: 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.

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

Page 15: 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.

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);

Page 16: 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.

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);

Page 17: 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.

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);

Page 18: 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.

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);

Page 19: 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.

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);

Page 20: 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.

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);

Page 21: 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.

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);

Page 22: 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.

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);

Page 23: 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.

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);

Page 24: 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.

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);

Page 25: 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.

Your Turn

Get into your groups and try this assignment

Page 26: 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.

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