CSI 121 Structured Programming Language Lecture 7: Input/Output

Slides:



Advertisements
Similar presentations
1 CSE1303 Part A Data Structures and Algorithms Semester 2, 2006 Lecture A1 – Welcome & Revision.
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
Chapter 18 I/O in C.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
C Programming Lecture 5 : Basic standard I/O Lecture notes : courtesy of Ohio Supercomputing Center, science and technolgy support.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
1 Data Structures and Algorithms Programs. Different problems. Operations. Size of data. Resources available.
GE 211 Programming in C Dr. Ahmed Telba Room :Ac -134
CSE1301 Computer Programming: Lecture 6 Input/Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CS 1704 Introduction to Data Structures and Software Engineering.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
Chapter 18 I/O in C Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, Y. Malaiya Colorado State University.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
CSCE 206 Structured Programming in C
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Chapter 9 C Formatted Input/Output
Formatted Input/Output
Input/output.
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Plan for the Day: I/O (beyond scanf and printf)
Formatted Input/Output
Plan of the Day: More on type conversions scanf printf format strings
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Programming in C Input / Output.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 9 - Formatted Input/Output
Chapter 18 I/O in C.
Department of Computer and Information Science
Chapter 18 I/O in C.
Chapter 4 Managing Input and Output Operations
Programming in C Input / Output.
Introduction to C Programming
Module 12 Input and Output
Introduction to C EECS May 2019.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 18 I/O in C.
Presentation transcript:

CSI 121 Structured Programming Language Lecture 7: Input/Output

Topics Streams Formatted input Formatted output Lecture 7: Input/Output

Recall scanf() Example: scanf(“%d”, &x); printf() Example: printf(“The value of x is %d\n”, x); #include <stdio.h>

Input/Output Program Lecture 7: Input/Output

Streams Text input or output is dealt with as a sequence of characters A stream serves as a channel to convey characters between I/O and programs Lecture 7: Input/Output

Streams: Input -- Example 135 25.5 _ int item; float cost; scanf(“%d %f”, &item, &cost); 1 3 5 2 5 . 5 \n input buffer Lecture 7: Input/Output

Streams: Input -- Example (cont) 135 25.5 _ int item; float cost; scanf(“%d %f”, &item, &cost); 1 3 5 2 5 . 5 \n item cost

Streams: Input – Example (cont) int item; float cost; scanf(“%d %f”, &item, &cost); 135 25.5 _ 2 5 . \n item cost 135 Lecture 7: Input/Output

Streams: Input – Example (cont) int item; float cost; scanf(“%d %f”, &item, &cost); 135 25.5 _ \n item cost 135 25.5

Streams: Output -- Example printf(“Hello!\n”); H e l l o ! \n output buffer

Streams: Output – Example (cont) printf(“Hello!\n”); H e l l o ! \n

Streams: Output – Example (cont) printf(“Hello!\n”); e l l o ! \n H

Streams: Output – Example (cont) printf(“Hello!\n”); l l o ! \n He

Streams: Output – Example (cont) printf(“Hello!\n”); l o ! \n Hel

Streams: Output – Example (cont) printf(“Hello!\n”); o ! \n Hell

Streams: Output – Example (cont) ! \n Hello printf(“Hello!\n”);

Streams: Output – Example (cont) printf(“Hello!\n”); \n Hello!

Streams: Output – Example (cont) printf(“Hello!\n”); Hello! _

Streams From the program's point of view, the characters are queued in a pipe The sequence of characters is organized into lines Each line: can have zero or more characters ends with the "newline" character '\n' Lecture 7: Input/Output

"Standard" Streams Standard streams: stdin - standard input usually from keyboard stdout - standard output usually to screen stderr - standard error must have at the top of your program #include <stdio.h> can be redirected Lecture 7: Input/Output

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

Example: ReadData Input name, age, gender, idNumber

#include <stdio.h>

#include <stdio.h> /*************************************\ Read in important info about a student \**************************************/

#include <stdio.h> /*************************************\ Read in important info about a student \**************************************/ int main() { return 0; }

#include <stdio.h> /*************************************\ Read in important info about a student \**************************************/ int main() { char name[100] ; float age ; char gender ; int idNumber ; return 0; }

#include <stdio.h> /*************************************\ Read in important info about a student \**************************************/ int main() { char name[100] ; float age ; char gender ; int idNumber ; scanf("%s %f %c %d", name, &age, &gender, &idNumber); return 0; }

#include <stdio.h> /*************************************\ Read in important info about a student \**************************************/ int main() { char name[100] ; float age ; char gender ; int idNumber ; scanf("%s %f %c %d", name, &age, &gender, &idNumber); return 0; } Ashley 19.2 M 3825 Input: Ashley 19.2 M 3825

stdout:Output Data (e.g., from a variable) is written out to stdout using the printf() function.

Example: WriteData Set name to “Ashley” Set age to 18.2 Set gender to ‘M’ Set idNumber to 3825 Output name, age, gender, idNumber

Ashley 18.2 M 3825 _ #include <stdio.h> /*****************************************\ Write out important info about a student \*****************************************/ int main() { char *name = ”Ashley" ; float age = 18.2; char gender = ’M'; int idNumber = 3825 ; printf("%s\n%f\n%c\n%d\n", name, age, gender, idNumber); return 0; } Ashley 18.2 M 3825 _ Lecture 7: Input/Output

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); Lecture 7: Input/Output

printf -- Format-Control-String Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age);

printf -- Format-Control-String (cont) 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 Lecture 7: Input/Output

printf -- Format-Control-String (cont) Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age); literal characters Lecture 7: Input/Output

printf -- Other-Arguments For printf: variables containing data for output Example: printf(“%s is %d years old.\n”, name, age);

scanf -- Format-Control-String Describes the format of the data given as input Contains “conversion specifiers” Example: scanf("%s %f %c %d", name, &age, &gender,&id); conversion specifiers Lecture 7: Input/Output

scanf -- Other-Arguments For scanf: “pointers” to variables where the input will be stored Example: scanf("%s %f %c %d", name, &age, &gender, &id);

scanf -- Other-Arguments (cont) For scanf: “pointers” to variables in which the input will be stored Example: scanf("%s %f %c %d", name, &age, &gender, &id); Do NOT use ‘&’ with strings! Variables of type int, float or char need ‘&’ ‘&’ is for scanf only!

Common Conversion Specifiers for Numerical Information decimal integer: %d printf(“What is %d plus %d?\n”, x, y); scanf(“%d”, &sum); float: %f printf(“%f squared is...? ”, x); scanf(“%f”, &ans); double: scanf(“%lf”, &ans); Lecture 7: Input/Output

Conversion Specifiers for Alphanumeric Information char: %c printf(“What letter follows %c?\n”,ch); scanf(“%c”, &nextchar); string: %s printf(“Name: %s\n”, name); scanf(“%s”, name); Lecture 7: Input/Output

printf: Conversion Specifiers i or d: display a signed decimal integer f: display a floating point value e or E: display a floating point value in exponential notation g or G: display a floating point value in either f form or e form L: placed before any float conversion specifier to indicate that a long double is displayed Lecture 7: Input/Output

scanf: Conversion Specifiers d: read an optionally signed decimal integer i: read an optionally signed decimal, octal, or hexadecimal integer i and d: the argument is a “pointer” to an integer int idNumber; scanf("%d", &idNumber); Lecture 7: Input/Output

scanf: Conversion Specifiers (cont) h or l: placed before any integer conversion specifiers to indicate that a short or long integer is to be input long int idNumber; scanf("%ld", &idNumber); l or L: placed before any float conversion specifiers to indicate that a double or long double is to be input Lecture 7: Input/Output

Conversion Example Input octal integer Output integer as decimal

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; }

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _ i

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _ i

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i 56

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i 56

Conversion Example (cont) #include <stdio.h> int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 56 _ i 56

Skipping Characters in Input Stream Skipping blank spaces scanf("%d %d %d", &day, &month, &year); Skipping dashes Enter data as dd-mm-yyyy: 16-3-1999 Store each number in date variables scanf("%d-%d-%d", &day, &month, &year); Lecture 7: Input/Output

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 I/O functions, you must include the stdio.h header file Input and output can be formatted and converted between data types

Reading King: Chapters 3 and 7, Section 22.3 Deitel & Deitel: Sections 9.1 to 9.6, 9.11