Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

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.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
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.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include #include int main ( ) { printf( "%d\n", 455 ); printf( "%d\n", 455 ); printf(
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 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.
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.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 4 Input and Output.
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.
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.
1 Pertemuan 4 Fungsi-Fungsi Bahasa C Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Dale Roberts Basic I/O (Chap. 9) CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
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.
 Integers and Characters  Character Strings  Input and Output.
CSE1301 Computer Programming: Lecture 6 Input/Output.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
CS 1704 Introduction to Data Structures and Software Engineering.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Chapter 9 C Formatted Input/Output
Input/output.
TMF1414 Introduction to Programming
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Plan of the Day: More on type conversions scanf printf format strings
OUTPUT STATEMENTS GC 201.
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.
Formatted Input/Output
C Formatted Input / Output Review and Lab Assignments
CSI 121 Structured Programming Language Lecture 7: Input/Output
توابع ورودي-خروجي.
Lecture 13 Input/Output Files.
Dale Roberts, Lecturer IUPUI
Chapter 9 - Formatted Input/Output
Department of Computer and Information Science
Programming Assignment #1 12-Month Calendar—
Formatted Input/Output
Formatted Input/Output
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of Computer and Information Science IUPUI

Dale Roberts Formatting Input with Scanf scanf Input formatting Capabilities Input all types of data Input specific characters Skip specific characters Format scanf (format-control-string, other-arguments); Format-control-string: describes formats of inputs %*wlx [*]: optional conversion only, result is not stored [w]: optional minimum width (wider if necessary). The padding character is blank normally and zero if the field width was specified with a leading zero [l]: long integer [x]: see the table next page Other-arguments Pointers to variables where input will be stored (address of variables) Can include field widths to read a specific number of characters from the stream

Dale Roberts Conversion specifier Description Integers diou x or X h or l Read an optionally signed decimal integer. The corresponding argument is a pointer to integer Read an optionally signed decimal, octal, or hexadecimal integer. The corresponding argument is a pointer to integer. Read an octal integer. The corresponding argument is a pointer to unsigned integer. Read an unsigned decimal integer. The corresponding argument is a pointer to unsigned integer. Read a hexadecimal integer. The corresponding argument is a a pointer to unsigned integer. Place before any of the integer conversion specifiers to indicate that a short or long integer is to be input.

Dale Roberts Floating-point Number e,E,f,g, G I or L Read a floating point value. The corresponding argument is a pointer to a floating point variable. Place before any of the floating point conversion specifiers to indicate that a double or long double value is to be input Characters and strings cs Read a character. The corresponding argument is a pointer to char no null (‘\0’) is added Read a string. The corresponding argument is a pointer to an array of type char that is large enough to hold the string and a terminating null (‘\0’) character which is automatically added.

Dale Roberts Scan set [ scan char Scan a string for a set of characters that are stored in an array. Miscellaneous pn% Read an address of the same form produced when an address is output with %p in a printf statement Store the number of characters input so far in this scanf. The corresponding argument is a pointer to integer Skip a percent sign ( % ) in the input

Dale Roberts Formatting Input with Scanf Scan sets Set of characters enclosed in square brackets [] Preceded by % sign Scans input stream, looking only for characters in scan set Whenever a match occurs, stores character in specified array Stops scanning once a character not in the scan set is found Inverted scan sets Use a caret ^ : [^aeiou] Causes characters not in the scan set to be stored Skipping characters Include character to skip in format control Or, use * (assignment suppression character) Skips any type of character without storing it

Dale Roberts 1/* Fig 9.20: fig09_20.c */ 2/* Reading characters and strings */ 3#include 4 5int main() 6{6{ 7 char x, y[ 9 ]; 8 9 printf( "Enter a string: " ); 10 scanf( "%c%s", &x, y ); printf( "The input was:\n" ); 13 printf( "the character \"%c\" ", x ); 14 printf( "and the string \"%s\"\n", y ); return 0; 17} Enter a string: Sunday The input was: the character "S" and the string "unday" /* initialize variables */ /* input */ /* print */ Example: Program Output:

Dale Roberts 1/* Fig 9.22: fig09_22.c */ 2/* Using an inverted scan set */ 3#include 4 5int main() 6{6{ 7 char z[ 9 ] = { '\0' }; 8 9 printf( "Enter a string: " ); 10 scanf( "%[^aeiou]", z ); 11 printf( "The input was \"%s\"\n", z ); return 0; 14} Enter a string: String The input was "Str" /* initialize variables */ /* input */ /* print */ Example: Program Output: Example: int i, float x; char name[50]; scanf(“%d %f %s”, &i, &x, name); With input: 25  54.32E-1  Thompson 25  i  x “Thompson”  name

Dale Roberts 1/* Fig 9.24: fig09_24.c */ 2/* Reading and discarding characters from the input stream */ 3#include 4 5int main() 6{6{ 7 int month1, day1, year1, month2, day2, year2; 8 9 printf( "Enter a date in the form mm-dd-yyyy: " ); 10 scanf( "%d%*c%d%*c%d", &month1, &day1, &year1 ); 11 printf( "month = %d day = %d year = %d\n\n", 12 month1, day1, year1 ); 13 printf( "Enter a date in the form mm/dd/yyyy: " ); 14 scanf( "%d%*c%d%*c%d", &month2, &day2, &year2 ); 15 printf( "month = %d day = %d year = %d\n", 16 month2, day2, year2 ); return 0; 19} Enter a date in the form mm-dd-yyyy: month = 11 day = 18 year = 2000 Enter a date in the form mm/dd/yyyy: 11/18/2000 month = 11 day = 18 year = 2000 Example: Program Output:

Dale Roberts Other Input / Output puts(line) Print a string to standard output and append a newline Example: puts(“12345”); putchar(c) Print a character to standard output Example:putchar(‘A’); gets(line) Read a string from standard input (until a newline is entered) Example : char buf[128]; gets(buf); /* space is OK, and the ‘\n’ won’t be read in */ Newline will be replaced by ‘\0’ getchar() Get a character from standard input Example: int c; c = getchar(); /* c must be int */ c = getchar(); /* c must be int */ In-memory Format Conversion sprintf(string, control, variables); sscanf(string, control, address-of-variables); Example: sprintf(buf, “project%03d%02d.dat”,year,month); Sscanf(buf, “project%03d%02d”,&year,&month);