Formatted Input and Output

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

1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
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.
 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",
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
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.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
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.
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.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
 Integers and Characters  Character Strings  Input and Output.
CSE1301 Computer Programming: Lecture 6 Input/Output.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
© 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.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
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
ECE Application Programming
Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
TMF1414 Introduction to Programming
Chapter 2 Overview of C.
Chapter 3: I/O Management
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Formatted Input/Output
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
Programming in C Input / Output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lecture 13 Input/Output Files.
Formatted Input/Output
Chapter 9 - Formatted Input/Output
Differences between Java and C
Chapter 4 Managing Input and Output Operations
Programming in C Input / Output.
Formatted Input/Output
Conversion Check your class notes and given examples at class.
Formatted Input/Output
Formatted Input/Output
Introduction to C EECS May 2019.
DATA TYPES There are four basic data types associated with variables:
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

Formatted Input and Output CSE 220 – C Programming Formatted Input and Output

MSU IEEE (Institute of Electrical and Electronics Engineers) Invitation from Kyle MacLean First meeting: Thursday 9/15 at 6-7pm in EB 1145

Summary Printf General Syntax Scanf General Syntax Common Mistakes http://grade5eisnor.weebly.com/uploads/2/5/1/5/25151059/8233546_orig.gif

What do you think the following code outputs? int a = 220; float b = 3.5f; printf("Your grade in %d is %f\n", a, b); Your grade in %d is %f\n Your grade in 220 is 3.5 Your grade in a is b Your grade in 220 is 4.0

printf(format_string, expr1, expr2, expr3, …); Printing output printf: used to print output to screen Defined in stdio.h Usage: printf(format_string, expr1, expr2, expr3, …); No limit on the number of expressions

Expressions printf(format_string, expr1, expr2, expr3, …); expr1, expr2, …: constants, variables, complicated expressions printf(“The value of %d multiplied by %f is %f\n”, 2, PI, (2*PI) );

Format String printf(format_string, expr1, expr2, expr3, …); Contains: ordinary characters and conversion characters: Conversion characters: placeholder for a value to be filled specifies how to convert the value into printed form printf(“The value of %d multiplied by %f is %f\n”, 2, PI, (2*PI) );

Conversion Specification General format: Conversion type Start of the specifier % - + 0 m .p X Minimum width of the field Flags Precision printf(“Your score is: %-4d”, x); printf(“%-6.3d”, x);

Conversion Specification Conversion type: c: a single character s: string d: integer f: floating point notation E,e: scientific notation u: unsigned integer X,x: hexadecimal number % - + 0 m .p X Start of the specifier Conversion type Flags Minimum width of the field Precision

Conversion Specification int x = 20; float y = 74.0231f; char z = 'd'; printf("%d %f %c\n", x, y, z); printf("%e\n", y); printf("%E\n", y); printf("%d %c\n", x); printf("%d\n", x, z); 20 74.023100 d 7.402310e+01 7.402310E+01 20 ? 20 2nd value is unpredictable z is not printed since no placeholder for it

Conversion Specification % - + 0 m .p X Start of the specifier Conversion type Flags Minimum width of the field Precision Flags: - : Left justify +: always print sign (+/-) 0: pad with leading zeros instead of spaces Can multiple flags in one specifier int x = 20, y = -20; printf(“%d %d\n”, x, y); 20 -20 printf(“%+d %+d\n”, x, y); +20 -20

Conversion Specification % - + 0 m .p X Start of the specifier Conversion type Minimum width: The minimum characters to print Pads with spaces if not enough characters Precision: depends on the conversion specifier with e and f: number of decimal digits with d: minimum number of digits Flags Minimum width of the field Precision

Example float x = 5.123456f; printf(“%f\n”, x); 5.123456 printf(“%+.3f\n”, x); +5.123 printf(“%+10.3f\n”, x); printf(“%-10.3f is my lucky number!\n”, x); 5.123 is my lucky number! 4 leading spaces to make total count 10

Escape Sequence \a: alert (bell sound) \n: new line \t: horizontal tab \b: backspace \": quotation mark Want: printf(""Hello""); Interpreted: printf(""Hello""); Use: printf("\"Hello\""); \\: single \ character printf("\\Hello\\"); Will print: \Hello\

What do you think the following code outputs? int a = 220; float b = 3.5f; printf("\"grade\" in %d \\\t %f", a, b); "grade" in 220 \\t 3.5 \grade\ in 220 \ 3.5 "grade" in 220 \ 3.5 None of the above

Error Checking printf("%d %d", x, y, z); printf("%d %d", x); C compilers are not required to check that the number of conversion specifications matches the number of output items: printf("%d %d", x, y, z); printf("%d %d", x); C compilers are not required to check that the type of conversion specification is appropriate int myInt; float myFloat; printf("%f %d", myInt, myFloat);

Summary Printf General Syntax Scanf General Syntax Common Mistakes http://grade5eisnor.weebly.com/uploads/2/5/1/5/25151059/8233546_orig.gif

Reading input scanf: used to read input according to given format Defined in stdio.h Usage: scanf(format_string, var1, var2, var3, ….); No limit on the number of variables

Format String Contains: ordinary characters and conversion characters: Conversion characters: same as printf scanf("%d%f", &i, &j); Convert first value to an integer Convert second value to a float %e, %f: are interchangeable for scanf

Maximum-Length If you don't want to consume an entire number from input, you can specify a maximum length for a conversion. Example: I want to only store the first two digits of the input 8492 (i.e. 84) scanf("%2d", &var); The next scanf starts at the 9 (the unconsumed input).

If the input is "480274", what does the following code output? int a, b, c; scanf("%1d", &a); scanf("%2d%d", &b, &c); printf("%d %d %d", c, b, a); 274 80 4 3 2 1 4 80 274 None of the above

Error Checking C compilers are not required to check that the number of conversion specifications matches the number of output items C compilers are not required to check that the type of conversion specification is appropriate C compilers are not required to not check for the (usually) required & in scanf program crash, value not read, warning

How scanf works Reads input data from left Skips blanks Reads the item until it reads a character that cannot belong to the item according to the conversion specification %d: ----10.4---------5 In this case, the %d matches the 10 (integers don't have decimal points). If successful: continues processing the format string If not: returns immediately If more input, belongs to next scanf call

Ordinary Characters Pattern matching If white space in format string: keeps reading, matching with whitespaces in input One white space character in format string matches any number of white spaces in input If other character: If matching: discards input, continue processing Otherwise: aborts

Example ~ : one space Format String: %d/%d Input: ~10/~35 Skip white space, match %d with 10, match / with /, skip white space, match %d with 35 Input: ~10~/~35 Skip white space, match %d with 10, fail to match ~ with /, abort How to allow whitespaces around /? Format string: %d%f Input: 20.3~5.0

Summary Printf General Syntax Scanf General Syntax Common Mistakes http://grade5eisnor.weebly.com/uploads/2/5/1/5/25151059/8233546_orig.gif

Common Mistakes Using & in printf Forgetting & in scanf Using format string in scanf similar to printf Adding \n to scanf