+ All Categories

lec3_io

Date post: 10-Dec-2015
Category:
Upload: ahmed-awad
View: 3 times
Download: 0 times
Share this document with a friend
Description:
computation c
Popular Tags:
43
1 Topics • Streams Formatted input Formatted output
Transcript
Page 1: lec3_io

1

Topics

• Streams

• Formatted input

• Formatted output

Page 2: lec3_io

2

• scanf()Example:scanf(“%d”, &x);

• printf()Example:printf(“The value of x is %d\n”, x);

• #include <stdio.h>

Recall

Page 3: lec3_io

3

Input/Output

Program

Page 4: lec3_io

4

Streams

• Text input or output is dealt with as a sequence of characters

• A stream serves as a channel to convey characters between I/O and programs

Page 5: lec3_io

5

Streams: Input -- Example

135 25.5_

1 3 5 2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

input buffer

Page 6: lec3_io

6

Streams: Input -- Example (cont)

135 25.5_

1 3 5 2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item cost

Page 7: lec3_io

7

Streams: Input – Example (cont)

135 25.5_

2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item

135

cost

Page 8: lec3_io

8

Streams: Input – Example (cont)

135 25.5_

\n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item

135

cost

25.5

Page 9: lec3_io

9

Streams: Output -- Example

H e l l o ! \n

printf(“Hello!\n”);

output buffer

Page 10: lec3_io

10

H e l l o ! \n

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 11: lec3_io

11

e l l o ! \n

H

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 12: lec3_io

12

l l o ! \n

He

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 13: lec3_io

13

l o ! \n

Hel

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 14: lec3_io

14

o ! \n

Hell

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 15: lec3_io

15

! \n

Hello

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 16: lec3_io

16

\n

Hello!

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 17: lec3_io

17

Hello!_

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 18: lec3_io

18

Streams

• From the program's point of view, the characters are queued in a pipe

• The sequence of characters is organized into lines

• Each line:

– can have zero or more characters– ends with the "newline" character '\n'

Page 19: lec3_io

19

Formatted Input and Output

• General form:printf( format-control-string, other-arguments);

scanf( format-control-string, other-arguments);

• Examples:printf( "%s\n%f\n%c\n%d\n" , name,age,gender,idNumber );

scanf( "%s %f %c %d" , name, &age, &gender, &idNumber );

Page 20: lec3_io

20

• Describes the format of the data for output

• Contains “conversion specifiers” and “literal characters”

Example:printf(“%s is %d years old.\n”, name, age);

printf -- Format-Control-String

Page 21: lec3_io

21

• Describes the format of the data for output

• Contains “conversion specifiers” and “literal characters”

Example:printf(“%s is %d years old.\n ”, name, age );

conversion specifiers

printf -- Format-Control-String (cont)

Page 22: lec3_io

22

printf(“ %s is %d years old.\n”, name, age );

literal characters

• Describes the format of the data for output

• Contains “conversion specifiers” and “literal characters”

Example:

printf -- Format-Control-String (cont)

Page 23: lec3_io

23

• For printf: variables containing data for output

Example:printf (( ““ %s is %d years old.\n”, name, age );

printf -- Other-Arguments

Page 24: lec3_io

24

• Describes the format of the data given as input

• Contains “conversion specifiers”

scanf -- Format-Control-String

Example:scanf ( "%s %f %c %d", name, &age, &gender,&id );

conversion specifiers

Page 25: lec3_io

25

• For scanf:“pointers” to variables where the input will be stored

scanf -- Other-Arguments

scanf( "%s %f %c %d" , name, &age, &gender, &id );Example:

Page 26: lec3_io

26• ‘&’ is for scanf only!

scanf("%s %f %c %d", name, &age, &gender, &id);

scanf -- Other-Arguments (cont)

• For scanf: “pointers” to variables in which the input will be stored

Example:

• Variables of type int, float or char need ‘&’

• Do NOT use ‘&’ with strings!

Page 27: lec3_io

27

Common Conversion Specifiers for Numerical Information

• decimal integer: %dprintf( “What is %d plus %d?\n”, x, y );scanf(“ %d”, &sum);

• float: %f

printf( “ %f squared is...? ”, x );

scanf(“ %f”, &ans);

• double: printf( “ %f squared is...? ”, x );

scanf(“ %lf ”, &ans);

Page 28: lec3_io

28

Conversion Specifiers for Alphanumeric Information

• char: %cprintf( “What letter follows %c?\n”,ch );scanf( “ %c”, &nextchar );

• string: %sprintf( “Name: %s\n”, name );scanf( “ %s”, name );

Page 29: lec3_io

29

• i or d: display a signed decimal integer• f : display a floating point value• e or E: display a floating point value in

exponential notation• g or G: display a floating point value in

either f form or e form• L: placed before any float conversion

specifier to indicate that a long doubleis displayed

printf: Conversion Specifiers

Page 30: lec3_io

30

scanf: Conversion Specifiers

• d: read an optionally signed decimal integer

• i : read an optionally signed decimal, octal, or hexadecimal integer

i and d: the argument is a “pointer” to an integerint idNumber;

scanf("%d", &idNumber);

Page 31: lec3_io

31

scanf: Conversion Specifiers (cont)

• h or l : placed before any integer conversion specifiers to indicate that a shortor longinteger is to be inputlong int idNumber;

scanf("%ld", &idNumber);

• l or L: placed before any float conversion specifiers to indicate that a doubleor long doubleis to be input

Page 32: lec3_io

32

Conversion Example

Input octal integer

Output integer as decimal

Page 33: lec3_io

33

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

Page 34: lec3_io

34

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

_

Page 35: lec3_io

35

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

_

Page 36: lec3_io

36

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

_

i

Page 37: lec3_io

37

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

_

i

Page 38: lec3_io

38

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

70_

i

Page 39: lec3_io

39

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

70_

i

56

Page 40: lec3_io

40

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

70_

i

56

Page 41: lec3_io

41

Conversion Example (cont)

#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

7056_

i

56

Page 42: lec3_io

42

Skipping Characters in Input Stream

• Skipping blank spaces

scanf("%d %d %d", &day, &month, &year);

• Skipping dashes– Enter data as dd-mm-yyyy: 16-3-1999 – Store each number in date variables

scanf("%d-%d-%d", &day, &month, &year);

Page 43: lec3_io

43

Summary • Input from keyboard is via the stdin stream

• Output to the screen is via the stdout stream

• Streams carry characters– divided into lines with ‘\n’ character

– input ends with special value: EOF

• To use the C I/O functions, you must include the stdio.h header file

• Input and output can be formatted and converted between data types