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.

Slides:



Advertisements
Similar presentations
printf() Documentation info:
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
Types and Variables. Computer Programming 2 C++ in one page!
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Data types and variables
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 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",
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.
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(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Objectives You should be able to describe: Data Types
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 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.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
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.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Input, Output, and Processing
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
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.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© 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.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
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.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
OUTPUT STATEMENTS GC 201.
Lecture 13 Input/Output Files.
Chapter 9 - Formatted Input/Output
Introduction to Java Applications
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

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 a file with names and phone numbers. I would like to print it out with vertically aligned columns. (so my printout is not in the same format as the input file) File contents: Bob 4929 Chuck 4882 Desired display: Bob 4929 Chuck 4882

printf is a command from the C programming language to control printing. Shell script #!/bin/bash printf "Hello world.\n" a=`echo Hello world. | wc | nawk '{print $2}' ` printf "This phrase contains %d words\n" $a Run it alpaca.ceri.memphis.edu512:> printex.sh Hello world. This phrase contains 2 words alpaca.ceri.memphis.edu513:>

#!/bin/bash printf "Hello world.\n" a=`echo Hello world. | wc | nawk '{print $2}' ` printf "This phrase contains %d words\n" $a We need the double quotes “... “ to define an argument (the stuff inside the quotes) for printf.

#!/bin/bash printf "Hello world.\n" a=`echo Hello world. | wc | nawk '{print $2}' ` printf "This phrase contains %d words\n" $a The argument in double quotes has - Regular text (“Hello world”, “This text contains “) - Some funny new thing - %d – a format specifier. - The already known specification for a new line - \n

#!/bin/bash printf "Hello world.\n" a=`echo Hello world. | wc | nawk '{print $2}' ` printf "This phrase contains %d words\n" $a We also have another argument, the $a, which is a shell variable, at the end. Note that the items are delimited with spaces, not commas.

#!/bin/bash printf "Hello world.\n" a=`echo Hello world. | wc | nawk '{print $2}' ` printf "This phrase contains %d words\n" $a We also have an example of nawk (which can be understood from the quick introduction given previously to allow nawk use in the homework). Ignoring the details, this line assigns the value of the shell variable a to be the number of words in the string “Hello world.” The variable a therefore contains an integer value (as a character string).

printf: format specifiers How to specify the format for printing various types of things printf "This phrase contains %d words\n" $a We are going to print out what is in the double quotes.

printf: format specifiers printf "This phrase contains %d words\n" $a No problem for everything but the %d. And what is that shell variable $a at the end?

printf: format specifiers printf "This phrase contains %d words\n" $a The shell variable a contains the number of words. We want this (number) information where the %d is located in the format specification. The %d and the $a are “paired”.

printf: format specifiers printf "This phrase contains %d words\n" $a The %d format specifier is used to control how contents of the shell variable, a, are printed.

printf: format specifiers Specify how to print various types of things %d signed decimal integer (The word decimal means base 10, as opposed to octal – base 8, or hexadecimal – base 16, or a number with a decimal point. The word integer means a whole number, no decimal point and fraction).

printf: format specifiers Modifying how decimal integers are printed. %. d says use a field N characters wide, with DIGITS digits (uses leading zeros, DIGITS can be > N (N gets overridden), or DIGITS can be left out).

printf: format specifiers Specify how to print various types of things printf "This phrase contains %d words\n" $a This phrase contains 2 words printf "This phrase contains %3d words\n" $a This phrase contains 002 words printf "This phrase contains %3.0d words\n" $a This phrase contains 2 words printf "This phrase contains %.4d words\n" $a This phrase contains 0002 words printf "This phrase contains %3.4d words\n" $a This phrase contains 0002 words

printf: format specifiers Specify how to print various types of things %d Print the associated argument as signed decimal number %i Same as %d %o Print the associated argument as unsigned octal number %u Print the associated argument as unsigned decimal number %x Print the associated argument as unsigned hexadecimal number with lower- case hex-digits (a-f) %X Same as %x, but with upper-case hex-digits (A-F) %f Interpret and print the associated argument as floating point number %e Interpret the associated argument as double, and print it in ±e format %E Same as %e, but with an upper-case E in the printed format

printf: format specifiers Specify how to print various types of things %g Interprets the associated argument as double, but prints it like %f or %e %G Same as %g, but print it like %E %c Interprets the associated argument as character: only the first character of a given argument is printed %s Interprets the associated argument literally as string %b Interprets the associated argument as a string and interpreting escape sequences in it %q Prints the associated argument in a format, that it can be re-used as shell-input (escaped spaces etc..)

printf: format specifiers Modifiers are specified between the introducting % and the character that specifies/identifies the format: Any number: specifies a minimum field width, if the text to print is smaller, it's padded with spaces * The asterisk: the width is given as argument before the string. Usage (the ”*” corresponds to the ”20”): printf ”%*s\n” 20 “test string” # “Alternative format” for numbers: see table below - Left-bound text printing into the field (standard is right-bound) 0 Pads numbers with zeros, not spaces Pad a positive number with a space, where a minus (-) is for negative numbers + Prints all numbers signed (+ for positive, - for negative)

printf: format specifiers Precision for a floating- or double – precision number can be specified by using., where is the number of digits for precision after the decimal point. malleys-imac-2:ESCI7205 smalley$ printf "%.10f\n" Combine with (total # characters, “-”, decimal point, e) smalleys-imac-2:ESCI7205 smalley$ printf "%15.10f\n"

printf: format specifiers If or is an asterisk (*), the precision is read from the argument that precedes the number to print. smalleys-imac-2:ESCI7205 smalley$ printf "%.*f\n" smalleys-imac-2:ESCI7205 smalley$ printf "%.*f\n" smalleys-imac-2:ESCI7205 smalley$ printf "%*.*f\n"

printf: format specifiers Scientific notation (seems to ignore the field if too small) smalleys-imac-2:~ smalley$ printf "%*.*e\n" e+11 smalleys-imac-2:~ smalley$ printf "%*.*e\n" e+02 smalleys-imac-2:~ smalley$ printf "%*.*e\n" e+02 smalleys-imac-2:~ smalley$ printf "%.*e\n" e+02 smalleys-imac-2:~ smalley$printf "%*.*e\n" e+02 smalleys-imac-2:~ smalley$

Later on we will talk about how computers represent numbers Integer format (integers, counting numbers) Floating point format (numbers with decimal point) Floating point numbers can be Real or Complex

printf: format specifiers For strings, the precision specifies the maximum number of characters to print (i.e. the maximum field width). (We already saw) For integers, it specifies the number of characters/digits to print (with leading zero- or blank-padding!).

Escape codes \\Prints the character \ (backslash) \aPrints the alert character (ASCII code 7 decimal) \bPrints a backspace \fPrints a form-feed \nPrints a newline \rPrints a carriage-return \tPrints a horizontal tabulator \vPrints a vertical tabulator \ Interprets as octal number and prints the corresponding character from the character set \0 same as \ * \x Interprets as hexadecimal number and prints the corresponding character from the character set (3 digits)* \u same as \x, but 4 digits * \U same as \x, but 8 digits * (* - indicates nonstandard, may not work)

A few of the most useful format specifiers %s String %c ASCII character. Print the first character of the corresponding argument %d Decimal integer %f Floating-point format %E Scientific notation floating-point format