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.

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.
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.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
 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.
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.
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.
Chapter 18 I/O in C.
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.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
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.
CS 1704 Introduction to Data Structures and Software Engineering.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
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.
Chapter 9 - Formatted Input/Output
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.
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.
Variables, Types and Expressions
توابع ورودي-خروجي.
Variables, Types and Expressions
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.
Programming in C Input / Output.
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Introduction to C EECS May 2019.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Chapter 18 I/O in C.
Presentation transcript:

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 ECE 103 material by prof. Phillip PSU

Syllabus Console I/O Output Functions Input Examples

2 Common Console I/O Functions The C++ standard and streamio libraries contain functions to print output to and read input from the console “Console” refers to:  Output device (e.g., monitor or output stream)  Input device (e.g., keyboard or input stream) The header file contains prototypes for the console I/O functions #include

3 Output Functions int putchar( int c );  putchar() writes a character c to the console  If successful, the character written is returned. If not successful, the EOF value is returned Note: EOF is a pre-defined macro  The expected argument c is of type integer. If c is type char, it is converted to an integer first  Make sure no integer of value > 255 is passed!  Best to pass a char literal or char type object

4 Example: #include // old fashioned C int main( void ) { // main int x = 'e'; char y = 's'; putchar( 'Y' ); // output: ‘Y’ putchar( x );// ouptut: ‘e’ putchar( y );// output: ‘s’ putchar( 33 );// 33 is '!' in ASCII return 0; } //end main Output: !

5 int printf( const char * format, … );  printf() writes formatted output to the console  You have already learned cin and cout  If successful, the number of characters written is returned; else a negative value is returned, indicating error!  format is a string that contains any combination of literal text and conversion specifiers  … is an optional argument list of expressions whose values are to be printed  For each item in the list, there must be a corresponding conversion specifier in format

6  A conversion specifier inside printf() string: Begins with % Ends with a conversion character Unix has pretty incomplete printf man-page  Between the % and the conversion character are optional formatting values (in this order): – to left-justify output (default is right-justify) + to force display of + or – sign Number that specifies a minimum field width. (period), which separates field width from precision Number that specifies the precision (# of digits printed after a decimal point)

7 Variable Typeprintf() SpecifierComment Integer Values short, int% d, % i long% ld, % li int%u%u unsigned int% x, % X hexadecimal Floating-Point Values float, double% f % e, % E % g, % G fixed decimal format scientific notation shorter of %f or %e long double% Lf, % Le, % LE % Lg, % LG Character & String Values char%c%c char *%s%s string Miscellaneous address%p%p memory address To print % symbol%

8  An escape sequence embedded in the format string performs special actions  A sequence starts with the '\' character SequenceDescriptionSequenceDescription \a alert (bell) character \v vertical tab \b backspace \\ backslash \f formfeed \' single quote \n newline \" double quote \r carriage return \t horizontal tab \xhh hexadecimal number

9 Example: #include int main( void ) { // main printf("Wake up!\n"); printf("\n"); /* Blank */ printf("Summer "); printf("is coming.\n"); printf(" See \"you\"\nlater.\n"); return 0; } //end main Wake up! Summer is coming. See "you" later.

10 Example: #include int main( void ) { // main int c = 65; // an ’A’ char ch = 'B'; // obvious printf( "c = %c\n", c ); printf( "c = %d\n", c ); printf( "ch = %c\n", ch ); printf( "ch = %d\n", ch ); return 0; } //end main c = A c = 65 ch = B ch = 66

11 Example: #include int main( void ) { // main int x = 5, y = 9; printf( "%d ", 12 ); printf( "[%d]\n", 3*x+y ); printf( "x equals %d\n", x ); printf( "x=%d y=%d\n", x, y ); printf( "x=%3d y=%3d\n", x, y ); printf( "x=%3d y=%3d\n", 12, 7 ); return 0; } //end main 12 [24] x equals 5 x=5 y=9 x= 12 y= 7

12 Example: #include int main( void ) { // main float u = 5.0; double v = 1.75; const char * str = "PSU rocks!"; printf( "%c %d %f\n", 65, 65, (float)65 ); printf( "%f\n", 2/3.0 ); printf( "u=[%f] v=[%f]\n", u, v ); printf( "%.1f %.2f %.3f\n", v, v, v ); printf( "%12.3f\n", v ); printf( "%12.3e\n", 254*v ); printf( "%s\n", str ); printf( "[%15s]\n", str ); printf( "[%-15s]\n", str ); return 0; } //end main A u=[ ] v=[ ] e+02 PSU rocks! [ PSU rocks!]

13 Example: #include #define V1 1.0 #define V2 1.5e-7 #define V3 1.5e7 int main( void ) { // main printf( "V1 (%.8f) = %.8f\n", V1 ); printf( "V1 (%.8g) = %.8g\n", V1 ); // %g is shorter of: %f and %e printf( "V1 (%.8e) = %.8e\n\n", V1 ); printf( "V2 (%.8f) = %.8f\n", V2 ); printf( "V2 (%.8g) = %.8g\n", V2 ); // %g is shorter of: %f and %e printf( "V2 (%.8e) = %.8e\n\n", V2 ); printf( "V3 (%.8f) = %.8f\n", V3 ); printf( "V3 (%.8g) = %.8g\n", V3 ); // %g is shorter of: %f and %e printf( "V3 (%.8e) = %.8e\n", V3 ); return 0; } //end main V1 (%.8f) = V1 (%.8g) = 1 V1 (%.8e) = e+00 V2 (%.8f) = V2 (%.8g) = 1.5e-07 V2 (%.8e) = e-07 V3 (%.8f) = V3 (%.8g) = V3 (%.8e) = e+07

14 Input Functions int getchar( void );  getchar() reads a single character from the console  If successful, the next character from the console is read and returned, converted to int  If not successful, the EOF value is returned

15 Example: #include int main( void ) { // main int ch; printf( "Enter character: ” ); ch = getchar(); printf( "ch = %c\n", ch ); return 0; } //end main Enter character: A ch = A

16 Example: #include int main( void ) { // main int ch1, ch2; printf( "Enter ch1: ” ); ch1 = getchar(); printf( "Enter ch2: ” ); ch2 = getchar(); printf( "ch1 = %c ch2 = %c\n", ch1, ch2 ); return 0; } //end main Enter ch1: A Enter ch2: ch1 = A ch2 = Why does this look wrong?

17 getchar() works with buffered line input: Buffer contents after pressing ' A ' and 'Enter': ch1=getchar() gets the ' A ' for ch1 After reading the first character in the buffer, the "next character" arrow is updated: ch2=getchar() gets the ' \n ' for ch2 'A''\n''A''\n' Arrow indicates "next character" to read.

18 A workaround for the buffer problem: #include int main( void ) { // main int ch1, ch2; printf( "Enter ch1: " ); ch1 = getchar(); getchar(); // Remove pending '\n’ printf( "Enter ch2: " ); ch2 = getchar(); printf( "ch1 = %c ch2 = %c\n", ch1, ch2 ); return 0; } //end main Enter ch1: A Enter ch2: B ch1 = A ch2 = B

19 int scanf( const char * format, … );  scanf() reads formatted input from the console  If successful, the number of items read is returned  If not successful, EOF is returned, -1 on Unix  format is a string that contains any combination of conversion specifiers  … is an optional argument list of variable addresses where the input values are to be stored  For each item in the list, there must be a corresponding conversion specifier in format

20 The “address” of a variable is the memory location that holds the variable's value The & character is C’s “address-of” operator When storing the value read by scanf(), you must specify the address of the variable that will receive the value Example: int x;... scanf( "%d", &x ); 1. Read character string of digits ‘0’..‘9’ into the input buffer; only ‘0’..’9’ due to %d 2. Convert the string value to an integer 3. Store the value at the address of int variable x

21 Variable Typescanf() Specifier Integer Values int% d, % i short% hd, % hi long int% ld, % li unsigned int%u%u Floating-Point Values float% f, % e, % E, % g, % G double% lf, % le, % lE, %lg, % lG long double% Lf, % Le, % LE, %Lg, % LG Character Values char%c%c Character String (whitespace delimited – not full text) char *%s%s

22 Example: #include int main( void ) { // main char cvar; int ivar; float fvar, fv2; double dvar; scanf( "%c", &cvar ); scanf( "%f", &fvar ); /* Use %f for floats */ scanf( "%lf", &dvar ); /* Use %lf for doubles */ scanf( "%d %f %f", &ivar, &fvar, &fv2 ); return 0; } //end main

23 Example: Common error – missing & in scanf() // source file: test.c #include int main( void ) { // main int x; // OK to leave uninitialized printf( "Enter x: ” ); scanf( "%d", x ); printf( "%d\n", x ); return 0; } //end main Notice x instead of &x $ gcc -ansi -Wall -pedantic test.c test.c: In function 'main': test.c:7: warning: format argument is not a pointer (arg 2) $./a.out Enter x: 45 Segmentation fault (core dumped)

24 Reminder for printf and scanf Specifiers Output → If value to display with printf is of type:  int: % d  long int: % ld  float, double: % f % e % g  long double: % Lf % Le % Lg Input → If value to input with scanf is of type:  int: % d  long int: % ld  float: % f % e % g  double: % lf % le % lg  long double: % Lf % Le % Lg printf uses the same specifiers for both float and double types. scanf uses separate specifiers for the float and double types.