Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A...

21
Day16 A Young W. Lim 2017-12-08 Fri Young W. Lim Day16 A 2017-12-08 Fri 1 / 21

Transcript of Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A...

Page 1: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Day16 A

Young W. Lim

2017-12-08 Fri

Young W. Lim Day16 A 2017-12-08 Fri 1 / 21

Page 2: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Outline

1 Based on

2 C Formatted IOFormatting OutputFormatting InputConversion SpecifiersField Widths, Precision and FlagsSummary Tables

Young W. Lim Day16 A 2017-12-08 Fri 2 / 21

Page 3: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Based on

"C How to Program",Paul Deitel and Harvey Deitel

I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted tocopy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts,and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.

CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short:you are free to share and make derivative works of the file under the conditions that you appropriately attribute it,and that you distribute it only under a license compatible with this one.

Young W. Lim Day16 A 2017-12-08 Fri 3 / 21

Page 4: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Format Control String

describes the formats in which the output values appear

conversion specifiersflagsfield widthsprecisionsliteral characters

Young W. Lim Day16 A 2017-12-08 Fri 4 / 21

Page 5: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Scan Set

a scan set scans the characters in the inputlooking only for those characters in the scan setwhen a character matches, it is stored in a character arraythe input operation stopswhen a character that is not in the scan set is encountered

Young W. Lim Day16 A 2017-12-08 Fri 5 / 21

Page 6: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Inverted Scan Set

inverted scan set : place ˆ (caret) in the square bracketsbefore the scan charactersstarts inputing characterswhich are not in the inverted scan setstoring them in a character arraystops when a character is encountered,which are in the inverted scan set

Young W. Lim Day16 A 2017-12-08 Fri 6 / 21

Page 7: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Conversion Specifier n (1)

the conversion specifier nstores the number of characterswhich have been read in the current scanf statementthe corresponding argument is the pointer to int

Young W. Lim Day16 A 2017-12-08 Fri 7 / 21

Page 8: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Conversion Specifier n (2)

#include <stdio.h>

int main(){

int val;

printf("abcd %n efgh\n", &val);printf("val = %d\n", val);

}

----abcd efghval = 5

from https://stackoverflow.com/questions/3401156/what-is-the-use-of-the-n-format-specifier-in-c

Young W. Lim Day16 A 2017-12-08 Fri 8 / 21

Page 9: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Conversion Specifier n (3)

int n;printf("%s: %nFoo\n", "hello", &n);printf("%*sBar\n", n, "");

-----hello: Foo

Bar

-----int n = printf("%s: ", "hello");printf("Foo\n");printf("%*sBar\n", n, "");

from https://stackoverflow.com/questions/3401156/what-is-the-use-of-the-n-format-specifier-in-c

Young W. Lim Day16 A 2017-12-08 Fri 9 / 21

Page 10: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Integer Conversion Specifiers

d display as a signed decimal integeri display as a signed decimal integero display as an unsigned octal integeru display as an unsigned decimal integerx, X display as an unsigned hexadecimal integer

place before any integer conversion specifierh shortl longll long long

Young W. Lim Day16 A 2017-12-08 Fri 10 / 21

Page 11: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Floating Point Number Conversion Specifiers

e, E display a floating-point value in exponential notationf, F display a floating-point value in fixed-point notationg, G either f, F or e, Eu display as an unsigned decimal integerx, X display as an unsigned hexadecimal integer

place before any floating-point conversion specifierl long double

Young W. Lim Day16 A 2017-12-08 Fri 11 / 21

Page 12: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Character and String Conversion Specifiers

c prints a characters pritns a string of characters ending in null characters

Young W. Lim Day16 A 2017-12-08 Fri 12 / 21

Page 13: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Other Conversion Specifiers

p prints an address (usually in hexadecimal)%% pritns % literally

Young W. Lim Day16 A 2017-12-08 Fri 13 / 21

Page 14: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Field Widths

if the field width is larger than the object being printed,the object is right justified by defaultfield width can be used with any conversion specifiers%{field width}.{precision}{conversion specifier}possible to specify the field width and the precisionby an integer expression in the argument list%*.*{conversion specifier}the matching argument in list is used in place of the astersk

Young W. Lim Day16 A 2017-12-08 Fri 14 / 21

Page 15: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Precisions

integer number precision:

indicates the minimu number of digits printedzeros are prefixed to the printed value

floating point number precsion:

indicates the number of digitsthat appear after the decimal point (f, F, e, E)indicates the number of significant digits (g, G)

string precision:

indicates the number of characters to be printed

Young W. Lim Day16 A 2017-12-08 Fri 15 / 21

Page 16: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Flags

– flag:left justifies its argument in a field

+ flag:prints + or – sign

space flag:

prints a space for a positive numberwhich are not displayed with the + flag

# flag:prefixes 0 to octal numbersprefixes 0x to hexadecimal numbersforces to print decimal points for floating point numbers

0 flag:prints leading zeros for a valuewhich does not occupy its entire field width

Young W. Lim Day16 A 2017-12-08 Fri 16 / 21

Page 17: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Escape Sequence

\’ single quote\" double quote\? question mark\\ back slash\a alert bell\b backspace\f new page or form feed\n newline\r carriage return\t horizontal tab\v vertical tab

Young W. Lim Day16 A 2017-12-08 Fri 17 / 21

Page 18: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Integer Conversion Specifier - Printing

d display as a signed decimal integeri display as a signed decimal integer

(i and d are different in scanf)o display as an unsigned octal integeru display as an unsigned decimal integerx,X display as an unsigned hexadecimal integerh, l, ll place before any integer conversion specifier

to indicate that a short, long, long longinteger is displayed : length modifiers

Young W. Lim Day16 A 2017-12-08 Fri 18 / 21

Page 19: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

Integer Conversion Specifier - Reading

d read an optionally signed decimal integer int *i read an optionally signed decimal integer int *

or hexadecimal integero read an octal integer unsigned int *u read an unsigned decimal integer unsigned int *x, X read a hexadecimal integer unsigned int *h,l,ll place before any integer converison

specifiers to indicate that a short,long, long long integer is to be input

Young W. Lim Day16 A 2017-12-08 Fri 19 / 21

Page 20: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

FP Number Conversion Specifier - Printing

e, E displays a floating point number in exponential notationf, F displays a floating point number in fixed-point notationg, G displays a floating point number in either fixed-point

form f or exponential form e based on the magnitude ofthe value

L place before any floating point conversion specifier toindicate that a long double floating point number isdisplayed

Young W. Lim Day16 A 2017-12-08 Fri 20 / 21

Page 21: Day16A - Wikimedia · 12/8/2017  · Day16A YoungW.Lim 2017-12-08Fri YoungW.Lim Day16A 2017-12-08Fri 1/21

FP Number Conversion Specifier - Reading

e, E read a floating point number double *f, Fg, Gl, ll place before any floating point conversion double *

specifiers to indicate that a double or long double *long double number is to be input

Young W. Lim Day16 A 2017-12-08 Fri 21 / 21