Download presentation
Presentation is loading. Please wait.
Published byWinfred Gallagher Modified over 8 years ago
2
CSE1301 Computer Programming: Lecture 6 Input/Output
3
Topics Streams Formatted input Formatted output Reading: Deitel and Deitel: Section 9.1 to 9.5, Section 9.11
4
Recall: scanf() Example: scanf(“%d”, &x); printf() Example: printf(“The value of x is %d\n”, x); #include
5
Input/Output Program
6
Streams Text input or output is dealt with as streams of characters. A stream serves as a channel to convey characters between I/O and programs.
7
Streams: Input 135 25.5 _ 135 25.5\n Example 1: int item; float cost; scanf(“%d %f”, &item, &cost); input buffer
8
Streams: Input 135 25.5 _ 135 25.5\n Example 1: int item; float cost; scanf(“%d %f”, &item, &cost); itemcost
9
Streams: Input 135 25.5 _ 25.5\n Example 1: int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost
10
Streams: Input 135 25.5 _ \n Example 1: int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost 25.5
11
Streams: Output Hello!\n Example 2: printf(“Hello!\n”); output buffer
12
Hello!\n Example 2: printf(“Hello!\n”); Streams: Output
13
Example 2: Hello! _ printf(“Hello!\n”); Streams: Output
14
Streams From the program's point of view, the characters are queued in a pipe. The sequence of characters are organised into lines. Each line: –can have zero or more characters; –ends with the "newline" character '\n'.
15
"Standard" Streams stdin - standard input –usually from keyboard stdout - standard output –usually to screen stderr - standard error (unbufferd) –usually to screen must have at the top of your program #include these streams can be redirected (more on this later)
16
stdin: Input Data is read in from stdin (into a variable) using the scanf() function. When input ends, the scanf() function returns a special value: EOF.
17
Example: ReadData input name, age, gender, idNumber
18
#include /*************************************\ Read in important info about a lecturer \**************************************/ main() { char name[100] ; float age ; char gender ; int idNumber ; scanf("%s %f %c %d", name, &age, &gender, &idNumber); } Input: Joey 19.5 M 666 Joey 19.5 M 666
19
stdout: Output Data is written out to stdout (eg. from a variable) using the printf() function.
20
Example: WriteData set name to ”Joey" set age to 19.5 set gender to ’M' set idNumber to 666 output name, age, gender, idNumber
21
#include /*****************************************\ Write out important info about a lecturer \*****************************************/ main() { char *name = ”Joey" ; float age = 19.5; char gender = ’M'; int idNumber = 666 ; printf("%s\n%f\n%c\n%d\n", name, age, gender, idNumber); } Joey 19.5 M 666 _
22
Formatted Input and Output General form: printf( format-control-string, other-arguments ); scanf( format-control-string, other-arguments ); Examples: printf(s\n%f\n%c\n%d\n", name, age, gender,idNumber); scanf("%s %f %c %d", name, &age, &gender, &idNumber);
23
Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age); Format-Control-String
24
Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age); conversion specifiers Format-Control-String
25
Example: printf(“%s is %d years old.\n”, name, age); literal characters Format-Control-String Describes the format of the data for output Contains “conversion specifiers” and “literal characters”
26
For printf: variables containing data for output. Example: (“%s%dnameage printf(“%s is %d years old.\n”, name, age); Other-Arguments: printf
27
For scanf: “pointers” to variables in which input will be stored. scanf("%s %f %c %d", name, &age, &gender,&id); Other-Arguments: scanf
28
Variables of type int, float or char need ‘&’ scanf("%s %f %c %d", name, &age, &gender,&id); Other-Arguments: scanf Do not use ‘&’ with strings! ‘&’ is for scanf only! For scanf: “pointers” to variables in which input will be stored.
29
Common Conversion Specifiers string: %s printf(“Name: %s\n”, name); scanf(“%s”, name); char: %c printf(“What letter follows %c?\n”, ch); scanf(“%c”, &nextchar); decimal integers: %d printf(“What is %d plus %d?\n”, x, y); scanf(“%d”, &sum);
30
Common Conversion Specifiers float: %f printf(“%f squared is...? ”, x); scanf(“%f”, &ans); double: printf(“%f squared is...? ”, x); scanf(“%lf”, &ans);
31
scanf: Conversion specifiers d : read an optionally signed decimal integer. i : read an optionally signed decimal, octal, or hexadecimal integer. i and d : The corresponding argument is a “pointer” to integer. int idNumber; scanf("%d", &idNumber) ;
32
scanf: Conversion specifiers h or l : place before any of the integer conversion specifiers to indicate that a short or long integer is to be input. long int idNumber; scanf("%ld", &idNumber) ; Also: i, o, u, x, e, f, g... [Fig 9.17 D&D]
33
Conversion example input octal integer output integer as decimal
34
Conversion example #include main() { int i ; scanf("%o", &i); printf("%d\n", i); }
35
Conversion example #include main() { int i ; scanf("%o", &i); printf("%d\n", i); } 70 56 _ i 56
36
Skipping characters in input stream Skipping blank spaces scanf("%d %d %d", &day, &month, &year); An alternative –Enter data as dd-mm-yyyy: 16-3-1999 –Store each number in date scanf("%d-%d-%d", &day, &month, &year);
37
Summary Input from keyboard is via the stdin stream Output to the screen is via the stdout stream Streams carry characters –divided into lines with ‘\n’ character –input ends with special value: EOF To use the C library functions available, you must include the stdio.h header file. Input and output can be formatted and converted between data types.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.