Output

Chapter 4 Simple Input/Output 4.1 Formatted Input/Output 4.2 The printf() Function 4.3 The scanf() Function 4.4 Character Input/Output Executing Pro...
Author: Lenard Harper
7 downloads 2 Views 140KB Size
Chapter 4 Simple Input/Output 4.1 Formatted Input/Output 4.2 The printf() Function 4.3 The scanf() Function 4.4 Character Input/Output

Executing Programs Items in memory Machine code instructions (in program) Input Data

CPU

Data

Output Results

Results INPUT

OUTPUT

4.1 Formatted Input/Output • Input/output is the way a program communicates with the user. For C, the I/O operations are carried out by the I/O functions in the I/O libraries. • Input from the keyboard/output to the monitor screen is referred to as standard input/output. scanf() gets() stream keyboard

Data

printf() puts()

Program

stream Data

Display

Example: Computing Area and Circumference of a Circle

Output

Input

radius radius(r) (r)

r

Area, Area, Circumference Circumference

Area = π*r*r, Circumference=2*π*r

• A function is a piece of code to perform a specific task. • A library contains a group of functions, usually for related tasks. e.g. standard I/O functions in the library e.g. maths functions in the library • To use the I/O functions in , the line #include • needs to be included as the preprocessor instructions in a program. • Two I/O functions are most frequently used: – printf() : output function – scanf() : input function

4.2 The printf() Function • A printf() statement has the form printf(control-string, argument-list); • Example #include main(void) { int num1 = 1, num2 = 2; printf("%d + %d = %d\n", num1, num2, num1+num2); return 0; }

$printSum 1 + 2 = 3 $

• The control-string is a string constant. It is to be printed on the screen. • %x is a conversion specification. It controls the type and format of the item to be printed. An item will be substituted for it in the printed output. • If the control-string is longer than one line, the continuation character, i.e. \, must be used: e.g. printf("This string is too long to fit on \ one line, so it is written on two lines"); • The argument-list contains a list of items such as item1, item2, …, etc. Values are to be substituted into places held by the conversion specifications in the control string. An item can be a constant, a variable or an expression like num1 + num2.

• The number of items must be the same as the number of conversion specifications. • The type of the item must match the conversionSpecifier Items in memory num1

num2

11

22

printf("%d + %d = %d\n", num1, num2, num1+num2);

1+2=3

For example: #include main(void) { int num = 10; printf("print a constant %d here\n", 5); printf("The value of num is %d\n", num); printf("print 5 * num : %d\n", 5 * num); return 0; }

$printEx print a constant 5 here The value of num is 10 print 5 * num : 50 $

Conversion Specification • A conversion specification is of the form • % [flag] [minimumFieldWidth] [precision] [sizeSpecification] conversionSpecifier • where % and conversionSpecifier are compulsory. The others are optional.

• A flag can be any of the following five: +, -, space, #, 0 • Zero or more flags may be present. Three are described here. +

signed values are printed with a plus sign if positive and a minus sign otherwise

-

left justified e.g. “%-d”

space

signed values are printed with a leading space if positive and a minus sign otherwise

• The minimumFieldWidth gives the minimum width to be used during printing. A wider field will be used if not enough. • The precision specifies the number of digits after the decimal point. e.g. “%8.4f” prints a floating point number in a field 8 character wide with 4 digits after the decimal point. • The sizeSpecification:

h

short integer

l

long integer

L

long double

• The conversionSpecifiers :

d

signed decimal integer conversion

o

unsigned octal integer conversion

x,X

unsigned hexadecimal integer conversion

c

single character conversion

f

signed decimal floating point conversion

%

print the % symbol

• In the following examples, all results are obtained using a PC.

Example 4_1: Printing Integers #include main(void) { short i = 10; int j = 10; long k = 1234567890L; printf("short i = %hd\n", i); printf("integer j = %d\n", j); printf("long k = %ld\n", k); /* not a one in %ld ! */ return 0; }

$ex4_1

short i = 10 integer j = 10 long k = 1234567890 $

Example 4_2: Printing Floating Point Numbers #include main(void) { float i = 10.3; double j = 100.0; printf("float i = %f\n", i); printf("double j = %f\n", j); /* by default, 6 digits printed after decimal point */ printf("float i = %4.1f\n", i); printf("double j = %12.1f\n", j); return 0; }

$ex4_2

float i = 10.300000 double j = 100.000000 float i = 10.3 double j = 100.0 $

Example 4_3: Printing Floating Point Numbers with other Specifications #include main(void) { double k = 0.123456789; printf("double k = %%%20f%%\n", k); printf("double k = [%20.9f]\n", k); printf("double k = [%-20.9f]\n",k); return 0; }

$ex4_3

double k = % 0.123457% double k = [ 0.123456789] double k = [0.123456789 ] $

4.3 The scanf() Function • A scanf() statement has the form scanf(control-string, argument-list);

• Example

#include main(void) { int n1, n2; printf(“Please enter 2 integers:\n”); scanf("%d %d", &n1, &n2); printf(“The sum = %d\n”, n1+n2); return 0; }

$sum2 Please enter two integers: 5 10 The sum = 15 $

• control-string is a string constant containing conversion specifications. • A conversion specification is of the form • % [flag] [maximumFieldWidth] [precision] [sizeSpecification] conversionSpecifier • where % and conversionSpecifier are compulsory. The others are optional. • It is similar to the conversion specification of printf(). Some differences: (1) The flag value of * will mean reading without changing the value of the corresponding item, i.e. skipping. (2) The field width here is the maximum rather than the minimum.

• The items in scanf() may be any variable matching the type given by the conversion specification. It cannot be a constant. It cannot be an expression like n1 + n2. • The variable name has to be preceded by an &. This is to tell scanf() the address of the variable so that scanf() can read the input value and store it in the variable. • scanf() reads data from the keyboard. • It stops reading when it has read all the items as indicated by the control string or the EOF (end of file) is encountered. On Unix, EOF is ^D (control-D). On DOS, it is ^Z (control-Z)

Example 4_4: #include main(void) { short num1; int num2; long num3; float real1, r1, r2, r3; double real2; long double real3;

printf(“Please enter a date:”); /* read in 3 decimal integers separated by / */ scanf("%hd/%d/%ld", &num1,&num2, &num3); printf(“The date : %2hd-%2d-%4ld\n\n”, num1, num2, num3); printf(“\nEnter 3 real numbers:\n”); /* read in 3 floating point numbers separated other by space, tabs or newlines */

from each

scanf("%f %lf %lf", &real1, &real2, &real3); printf(“They are:%4.2f,%f, %8.4lf\n\n”, real1, real3); printf(“Enter 3 real numbers:\n”); scanf("%2f%2f%2f", &r1, &r2, &r3); printf(“They are : %f,%f,%f\n”, r1, r2, r3); return 0; }

real2,

Please enter a date:14/07/1993 The date : 14- 7-1993 Enter 3 real numbers: 14 7.5 993.54321 They are : 14.0,7.500000,993.5432 Enter 3 real numbers: 14701A They are : 14.000000,70.000000,1.000000 $

scanf("%hd/%d/%ld", &num1, &num2, &num3);

14/07/1993

num1

num2

num3

14 14

07 07

1993 1993

#include main(void) { char reply; int number; printf("Please enter the number: "); scanf("%d", &number); printf("The number read is %d\n", number); printf("Correct (y/n)? "); scanf("%c", &reply); printf(“your reply : %c\n”, reply); return 0; }

$ex4_5

Please enter the number: 1234 The number read is 1234 Correct (y/n)? your reply : $

Input Buffer: Empty ?

?

?

?

? Memory

1234 Input Buffer ? 1

? 2

? 3

? 4

\n

?

Buffer position indicator

scanf("%d",&number);

number ? 1234

Input Buffer ? 1

? 2

? 3

? 4

\n

?

Buffer position indicator

reply scanf("%c",&reply);

? \n

Input Buffer ? 1

? 2

? 3

? 4

\n

?

Buffer position indicator

Reason: there is a hidden character '\n' included in your input when you type 1234 and then press ENTER

4.4 Character Input/Output • There are two functions in library to manage single character input and output: putchar() getchar() putchar( ) • The syntax of calling putchar is putchar(characterConstantOrVariable); • It is equivalent to printf("%c", characterConstantOrVariable);

• The difference is that putchar is faster because printf needs to process the control string for formatting. Also, it returns either the integer value of the written character or EOF if an error occurs. getchar ( ) • The syntax of calling getchar is ch = getchar(); • where ch is a character variable. • It is equivalent to scanf("%c", &ch);

Example ex3_6: /* example of using getchar() & putchar() */ #include main(void) { char ch, ch1, ch2; putchar('1'); putchar(ch='a'); putchar('\n'); printf("%c%c\n", 49, ch); ch1 = getchar(); ch2 = getchar(); putchar(ch1); putchar(ch2); putchar('\n'); return 0; }

$ex3_6 1a 1a ab (User Input) ab $

Operation of getchar() Input Buffer: Empty ?

?

?

?

?

ab Input Buffer ? a

? b

\n ?

?

?

Buffer position indicator

ch1=getchar(); ch2=getchar();

Input Buffer ? a

? b

Memory

\n ?

?

Buffer position indicator

?

ch1

ch2

?a

?b