printf() Documentation info:

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

ECE Application Programming Instructor: Dr. Michael Geiger Spring 2013 Lecture 6: printf() formatting.
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.
 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",
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.
C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer.
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.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Simple Data Type Representation and conversion of numbers
 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 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.
C-Language Keywords(C99)
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
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.
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 1704 Introduction to Data Structures and Software Engineering.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ 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.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
ECE Application Programming
Chapter 9 C Formatted Input/Output
ECE Application Programming
ECE Application Programming
Input/output.
TMF1414 Introduction to Programming
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.
Chapter 08- printf and scanf
Programming in C Input / Output.
Formatting 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.
Chapter 9 - Formatted Input/Output
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Programming in C Input / Output.
Conversion Check your class notes and given examples at class.
Introduction to Java Applications
EECE.2160 ECE Application Programming
Introduction to C EECS May 2019.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
EECE.2160 ECE Application Programming
Presentation transcript:

printf() Documentation info: ECE 160 02/07/2005 printf() Documentation info: int printf(const char *format [,argument] ...) Type of value returned Name of function ( ) indicate printf is a function First arg type and formal name (required, since no brackets) next argument type and name (in this case it may be any simple type) [ ] indicate optional arguments … indicates previous argument repeated zero or more times (c) 2005, P. H. Viall

printf() int printf(const char *format [,argument] ...) Type of value returned (int in this case) All functions return at most one value. The type void is used to indicate a function returns no value There is no requirement to use the value returned. The printf() function returns the number of characters printed (including spaces); returns negative value if error occurs.

printf() int printf(const char *format [,argument] ...) Name of function; printf( ) in this case A function name is ALWAYS followed by a set of (), even if the function takes no arguments

printf() int printf(const char *format [,argument] ...) Type (const char *) and name (format) of first argument For the moment, const char * can be thought of as a series of characters enclosed in double quotes The name format may be thought of as a code indicating how the arguments are to be interpreted, and how the output should look.

printf() int printf(const char *format [,argument] ...) zero of more optional arguments, each preceded by a comma zero because of the … optional because of the [ ]

examples - printf() float a=67.49,b=9.999925; printf("hello %f there %f\n",a,b); printf("%f%f%f%f\n",a,a,b,b); printf("a=%f, b=%f",a,b); printf("Cool huh?\n"); Printed: hello 67.490000 there 9.999925 67.49900067.4990009.9999259.999925 a=67.490000, b=9.999925Cool huh? Note: default %f prints as many digits as needed to represent the whole part of the value and exactly 6 digits to the right of the decimal point.

examples - printf() float a=67.49,b=9.999925; printf("Price:█%10f█%10f\n",a,b); printf("%8.2f█%8.4f█%5.1f█%7.5f\n",a,a,b,b); printf("a=%5.5f,█b=%0.2f\n",a,b); Printed: 00000000011111111112222222222333333 print 12345678901234567890123456789012345 position Price:██67.499000███9.999925 columns ███67.49██67.4900██10.0█9.99993 1 to 35 a=67.49000,█b=10.00 Note: On last line of output, the actual value was output; the printf routine overrode the specified width.

examples - printf() float a=67.49,b=9.999925; int i=184,j=-51; double x=123.456, y=-22.33; printf("%7.1lf%5d%8.2f\n",y,i,a); printf("%13.2f%4d%9.2lf\n",b,j,x); //changed Printed: 00000000011111111112222222222333333 print 12345678901234567890123456789012345 position -22.3 184 67.49 columns 10.00 -51 -22.33 1 to 35 Notes: d indicates decimal integer f indicates float lf (that’s lower case L and f) indicates double (for double output, f OK too; not OK for input)

printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type flag meaning default - Left align the result within the given field width Right align + Prefix the output value with a sign (+ or -) if the output value is of a signed type. Sign appears only for negative signed values (-) If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and - appear, the 0 is ignored. No padding (actually space padding) blank Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. No blank appears # When used with the o, x, or X format, the # flag prefixes any nonzero output with 0, 0x, or 0X, respectively. Ignored when used with c, d, i, u, or s. No prefix When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases. Decimal point appears only if digits follow it. When used with g or G format, forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros. Decimal point appears only if digits follow it. Trailing zeros are truncated. This slide adapted from information in MSDN Library

printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. Notes: If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers). The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification). This slide adapted from information in MSDN Library

printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type typ meaning default c The precision has no effect character is printed d,i, u,o, x,X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision Default precision is 1. e,E The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default precision is 6; if precision is 0 or the period f The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. (.) appears without a number following it, no decimal point is printed. g,G The precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated. s,S The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Characters are printed until a null character is encountered. This slide adapted from information in MSDN Library

printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type to specify use prefix with type specifier long, long int l d, i, o, x, X long unsigned int u short int h short unsigned int __int64 I64 Note: this is not a complete list. Note: these prefixes are Microsoft extensions and not ANSI compatible This slide adapted from information in MSDN Library

printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type type what c Single byte char d or i Signed decimal integer X or x Unsigned hexadecimal integer (using ABCDEF or abcdef) o Unsigned octal integer u Unsigned decimal integer f Signed real value having the form [-]xxxx.yyyy where yyyy is dependant upon the requested precision, and xxxx is dependent upon the requested width and/or magnitude of value e or E Signed real value of form [-]xxxx.yyyye±zzz, where xxxx and yyyy are as described for f; zzz is a three digit exponent. E is the same as e except a capital E is printed g or G Automatically selects f or e/E based on the magnitude of the value s String - assumes start address given; terminates at \0 (null) Note: not a complete list This slide adapted from information in MSDN Library

printf() examples printf("vv% dww%-dxx%+dyy%dzz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= ruler vv-12ww-34xx-56yy-78zz printf("vv% dww%-dxx%+dyy%dzz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= ruler vv 12ww34xx+56yy78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= ruler vv -12ww-34 xx -56yy -78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= ruler vv 12ww34 xx +56yy 78zz

printf() examples printf("v% 05dw%-05dx%+05dy%05dz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= ruler v-0012w-34 x-0056y-0078z printf("v% 05dw%-05dx%+05dy%05dz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= ruler v 0012w34 x+0056y00078z printf("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= ruler v -012w -034x-056 y -078z print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78); ..../....1..../....2..../....3..../ <= ruler v 012w 034x056 y +078z

printf() examples printf("w%7.2fx%7.2fy%7.2fz\n",-1.234,-3.456,-5.6); ..../....1..../....2..../....3..../ <= ruler w -1.23x -3.46y -5.60z printf("w%7.2fx%7.2fy%7.2fz\n", 1.234, 3.456, 5.6); ..../....1..../....2..../....3..../ <= ruler w 1.23w 3.46y 5.60z printf("w%7.2fx%7.2f\n",-1234567.8,1234567.8); ..../....1..../....2..../....3..../ <= ruler w-1234567.8x1234567.8z