Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CSC 107 – Programming For Science

2 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

3 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

4 printf Print Results w/ printf  Cannot use cin & cout with standard C code  Must instead use the C functions for these actions  printf  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

5 cout vs. printf coutprintf  Simple  Easy-to-use  Clear  Limited need to memorize  Fun  Elegant  Complex  Lots of knobs & buttons  Obscure  Lots to look up  Hateful  Ugly

6 cout vs. printf coutprintf

7

8 Even Better Magic  Must tell printf how to format variables  No error on wrong specifier, just printed wrong Variable TypeSpecifier int%i, %d short%hi, %hd long%li, %ld float%f, %e, %E, %g, %G double%lf, %le, %lE, %lg, %lG long double%Lf, %Le, %LE, %Lg, %LG char%c cString%s

9 Printing Out Data  1 st 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

10 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

11 printf Examples #include 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”);

12 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

13 cin vs. scanf cinscanf

14 Arguments to scanf  Must use quotes for 1 st 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

15 scanf Examples

16 Will It Compile & Work?

17

18

19

20

21

22

23

24

25 Your Turn  Get into your groups and try this assignment

26 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  Last programming assignment due in 1 week


Download ppt "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."

Similar presentations


Ads by Google