introduction to c++ - 國立中興大學 · outline printf and scanf streams (input and output)...

19
INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT Dept. of Electronic Engineering, NCHU Original slides are from http://sites.google.com/site/progntut/

Upload: trinhtruc

Post on 10-Apr-2018

244 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

INTRODUCTION TO C++

C FORMATTED INPUT/OUTPUT

Dept. of Electronic Engineering, NCHU

Original slides are from http://sites.google.com/site/progntut/

Page 2: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Outline

printf and scanf

Streams (input and output) gets, puts

getchar, putchar

2010/11/15

2

Introduction to C++, NCHU

Page 3: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Formatting output with printf

Syntax printf("format control string", other arguments);

Format control string could include Conversion specifications (begin with %)

Flags

Filed widths

Precisions

Literal characters

Other arguments: Each one corresponds to a conversion specification

optional

2010/11/15

4

Introduction to C++, NCHU

Page 4: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Example of printf

int numDogs = 2, numCats = 1;

printf("I have %d dogs and %d cats\n", numDogs, numCats);

format control string argument

conversion specification

2010/11/15

5

Introduction to C++, NCHU

Page 5: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Printing integers

Positive, negative, and zero

Integer conversion specification: %d: signed decimal integer

%i: signed decimal integer (different from %d in scanf)

%o: unsigned octal integer

%u: unsigned decimal integer

%x: unsigned hex integer (0-9, a-f)

%X: unsigned hex integer (0-9, A-F)

%h or %l: placed before any integer conversion specifier to indicate a short or long integer

2010/11/15

6

Introduction to C++, NCHU

Page 6: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Example of printing integer

01 /* Fig 9.2: fig09_02.c*/

02 /* Using the integer conversion specifiers */

03 #include <stdio.h>

04

05 int main(void)

06 {

07 printf("%d\n", 455);

08 printf("%i\n", 455);

09 printf("%d\n", +455);

10 printf("%d\n", -455);

11 printf("%hd\n", 32000);

12 printf("%ld\n", 2000000000L);

13 printf("%o\n", 455);

14 printf("%u\n", 455);

15 printf("%u\n", -455);

16 printf("%x\n", 455);

17 printf("%X\n", 455);

18 return 0;

19 } /* end main */

455

455

455

-455

32000

2000000000L

707

455

4294955841

1c7

1C7

Output:

2010/11/15

7

Introduction to C++, NCHU

Page 7: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Printing floating-point number

Floating point or exponential notation 153.2 = 1.532 x 102 = 1.532E2

Floating point conversion specification: %e or %E: Display floating-point in exponential notation

%f: Display floating-point in fixed-point notation

%g or %G: Display floating-point in either exponential or fixed-point based on the magnitude of the value

%L: Placed before any floating-point conversion specifier to indicate a long double

2010/11/15

8

Introduction to C++, NCHU

Page 8: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Example of printing floating-point

01 /* Fig 9.4: fig09_04.c*/

02 /* Printing with floating-point conversion specifiers */

03 #include <stdio.h>

04

05 int main(void)

06 {

07 printf("%e\n", 1234567.89); 08 printf("%e\n", +1234567.89); 09 printf("%e\n", -1234567.89); 10 printf("%E\n", 1234567.89); 11 printf("%f\n", 1234567.89);

12 printf("%g\n", 1234567.89); 13 printf("%G\n", 1234567.89); 14

15 return 0;

16 } /* end main */

1.2345678e+006

1.2345678e+006

-1.2345678e+006

1.2345678E+006

1234567.890000

1.234567e+006

1.234567E+006

Output:

2010/11/15

9

Introduction to C++, NCHU

Page 9: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Printing Strings and Characters

%c: Print character

Single quotes for character constant (‘z’, ‘a’, or ‘\n’)

Print the first character of a string

printf("%c“, “string”); // output: s

%s: Print string

Double quotes for strings (“z”, “string”)

Can not print characters

s

s \0

„s‟

“s”

2010/11/15

10

Introduction to C++, NCHU

Page 10: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Example of printing strings

01 /* Fig 9.5: fig09_05.c*/

02 /* Printing string and characters */

03 #include <stdio.h>

04

05 int main(void)

06 {

07 char ch = 'a'; /* char */

08 char string[] = "This is a string."; /* char array */

09 const char *stringptr = "Also a string"; /* char pointer */

10

11 printf("%c\n", ch);

12 printf("%s\n", "This is a string."); 13 printf("%s\n", string); 14 printf("%s\n", stringptr); 15

16 return 0;

17 } /* end main */

a

This is a string.

This is a string.

Also a string

Output

2010/11/15

11

Introduction to C++, NCHU

Page 11: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Other conversion specifiers

%p Display the address of a pointer value

%% Print a percent sign (%)

2010/11/15

12

Introduction to C++, NCHU

Page 12: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Printing with field widths

Size of field in which data is printed

If the width is larger than data, default right justified If the field width is too small, it is increased to fit data

Minus sign occupies one character in the field

Integer width inserted between % and conversion specifier %4d: field width is 4

printf(“print %4d”, 23);

p r i n t 2 3

2010/11/15

13

Introduction to C++, NCHU

Page 13: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Example of field widths

printf(“%4d\n”, 1); printf(“%4d\n”, 12); printf(“%4d\n”, 123); printf(“%4d\n”, 1234); printf(“%4d\n”, 12345);

printf(“%4d\n”, -1); printf(“%4d\n”, -12); printf(“%4d\n”, -123); printf(“%4d\n”, -1234); printf(“%4d\n”, -12345);

1111 1112 1123 1234 12345 11-1 1-12 -123 -1234 -12345

2010/11/15

14

Introduction to C++, NCHU

Page 14: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Printing with precision

Usage format % + dot (.) + precision number + conversion specifies Example: %.4d, %.4f

Definition varies with data types For integer Minimal number of digits to print If data is too small, prefixed with zeros Example: printf(“%.4d\n”, 12); => ourput: 0012

For floating point Number of digits to appear after decimal Example: printf(“%.4f\n”, 12.34) => output: 12.3400

For string Maximum number of characters to be written from string

2010/11/15

15

Introduction to C++, NCHU

Page 15: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Example of precision

int i = 123;

printf(“%.4d\n%.9d\n”, i, i);

double f = 1234.5678;

printf(“%.3f\n%.3e\n%.4g\n”, f, f, f,);

char s*+ = “happy birthday”;

printf(“%.11s\n”, s);

0123 000000123 1234.567 1.234e+003 1235 happy birth

2010/11/15

16

Introduction to C++, NCHU

Page 16: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Flags

Placed right after (%)

Several flags may be combined

Flag Description

- Left justify the output

+ Display + in front of a positive value

(space) Display a space „ „ in front of a positive value

# Display 0 in front of an octal integer

Display 0x or 0X in front of a hex integer

0 Pad a field with leading zeros

2010/11/15

17

Introduction to C++, NCHU

Page 17: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Example of flags

printf(“%5d students\n”, 100); printf(“%-5d students\n”, 100);

printf(“%05d students\n”, 100); printf(“%+d\n%d\n”, 100, 100);

printf(“% d\n%d\n”, 100, -100);

printf(“%#o\n”, 1427); printf(“%#x\n%#X\n”, 1427, 1427);

00100 students 10000 students 00100 students +100 100 100 -100 02623 0x593 0X593

2010/11/15

18

Introduction to C++, NCHU

Page 18: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Escape sequences

Escape sequence Output

\‟ Single quote („)

\” Double quote (“)

\? Question mark (?)

\\ Backslash (\)

\a Bell

\b Backspace (Move the cursor back one position)

\f Move the cursor to the start of the next page

\n Move the cursor to the next line

\r Move the cursor to the beginning of the current line

\t Move the cursor to the next horizontal tab position

\v Move the cursor to the next vertical tab position

2010/11/15

19

Introduction to C++, NCHU

Page 19: INTRODUCTION TO C++ - 國立中興大學 · Outline printf and scanf Streams (input and output) gets, puts getchar, putchar 2010/11/15 2 Introduction to C++, NCHU

Formatting output with scanf

Syntax scanf("format control string", &var1, &var2, …);

int a; scanf(“%d”, &a); // read integer

double f; scanf(“%lf”, &f); // read double floating point

char ch; scanf(“%c”, &ch); // read character

char s[10]; scanf(“%s”, s); // read string (should notice the size of string )

2010/11/15

20

Introduction to C++, NCHU