0% found this document useful (0 votes)
43 views

Lect07 IO PDF

1) The document discusses input/output streams in C programming. It describes how formatted input and output functions like scanf() and printf() use streams to convey data between programs and input/output. 2) Streams serve as channels that organize input/output into sequences of characters. scanf() reads data from stdin and stores it in variables based on format specifiers, while printf() writes data from variables to stdout based on format specifiers. 3) Common conversion specifiers for scanf() and printf() are described for reading and writing different data types like integers, floats, characters, and strings. Examples are provided to illustrate how formatted I/O works with streams in C.

Uploaded by

abhimanyu raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Lect07 IO PDF

1) The document discusses input/output streams in C programming. It describes how formatted input and output functions like scanf() and printf() use streams to convey data between programs and input/output. 2) Streams serve as channels that organize input/output into sequences of characters. scanf() reads data from stdin and stores it in variables based on format specifiers, while printf() writes data from variables to stdout based on format specifiers. 3) Common conversion specifiers for scanf() and printf() are described for reading and writing different data types like integers, floats, characters, and strings. Examples are provided to illustrate how formatted I/O works with streams in C.

Uploaded by

abhimanyu raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Input/Output

1
Topics
• Streams
• Formatted input
• Formatted output

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

•printf()
Example:
printf(“The value of x is %d\n”, x);
•#include <stdio.h>

3
Input/Output

Program

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

5
Streams: Input -- Example
int item;
135 25.5
float cost; _
scanf(“%d %f”, &item, &cost);

1 3 5 2 5 . 5 \n

input buffer 6
Streams: Input -- Example (cont)
int item;
135 25.5
float cost; _
scanf(“%d %f”, &item, &cost);

1 3 5 2 5 . 5 \n

item cost

7
Streams: Input – Example (cont)
int item;
135 25.5
float cost; _
scanf(“%d %f”, &item, &cost);

2 5 . 5 \n

item cost
135
8
Streams: Input – Example (cont)
int item;
135 25.5
float cost; _
scanf(“%d %f”, &item, &cost);

\n

item cost
135 25.5
9
Streams: Output -- Example

printf(“Hello!\n”);

H e l l o ! \n

output buffer

10
Streams: Output – Example (cont)

printf(“Hello!\n”);

H e l l o ! \n

11
Streams: Output – Example (cont)

printf(“Hello!\n”);

e l l o ! \n

12
Streams: Output – Example (cont)

printf(“Hello!\n”);

l l o ! \n

He

13
Streams: Output – Example (cont)

printf(“Hello!\n”);

l o ! \n

Hel

14
Streams: Output – Example (cont)

printf(“Hello!\n”);

o ! \n

Hell

15
Streams: Output – Example (cont)

printf(“Hello!\n”);

! \n

Hello

16
Streams: Output – Example (cont)

printf(“Hello!\n”);

\n

Hello!

17
Streams: Output – Example (cont)

printf(“Hello!\n”);

Hello!
_

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'

19
"Standard" Streams
• Standard streams:
– stdin - standard input
• usually from keyboard
– stdout - standard output
• usually to screen
– stderr - standard error
• usually to screen
• must have at the top of your program
#include <stdio.h>

20
stdin: Input
• Data is read in from stdin (into a
variable) using the scanf() function
• When input ends, the scanf() function
returns a special value: EOF

21
Example: ReadData

Input name, age, gender, idNumber

22
#include <stdio.h>
/*************************************\
Read in important info about a student
\**************************************/
int main()
{

return 0;
}

23
#include <stdio.h>
/*************************************\
Read in important info about a student
\**************************************/
int main()
{
char name[100] ;

float age ;

char gender ;

int idNumber ;

return 0;
}

24
#include <stdio.h>
/*************************************\
Read in important info about a student
\**************************************/
int main()
{
char name[100] ; Ashley
float age ; 19.2
char gender ; M
int idNumber ; 3825

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


return 0;
}
Input: Ashley 19.2 M 3825 25
stdout:Output
• Data (e.g., from a variable) is written out to
stdout using the printf() function.

26
Example: WriteData
Set name to “Ashley”
Set age to 18.2
Set gender to ‘M’
Set idNumber to 3825
Output name, age, gender, idNumber

27
#include <stdio.h>

/*****************************************\
Write out important info about a student
\*****************************************/

int main()
{
Ashley
char *name = ”Ashley" ; 18.2
float age = 18.2; M
char gender = ’M'; 3825
int idNumber = 3825 ;
_

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


return 0;
}
28
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);

29
printf -- Format-Control-String

• Describes the format of the data for output


• Contains “conversion specifiers” and “literal
characters”
Example:
printf(“%s is %d years old.\n”, name, age);

30
printf -- Format-Control-String (cont)

• 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 31
printf -- Format-Control-String (cont)

• Describes the format of the data for output


• Contains “conversion specifiers” and “literal
characters”

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

literal characters
32
printf -- Other-Arguments

• For printf: variables containing data for output


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

33
scanf -- Format-Control-String
• Describes the format of the data given as input
• Contains “conversion specifiers”
Example:
scanf("%s %f %c %d", name, &age, &gender,&id);

conversion
specifiers

34
scanf -- Other-Arguments
• For scanf: “pointers” to variables where the input
will be stored
Example:
scanf("%s %f %c %d", name, &age, &gender, &id);

35
scanf -- Other-Arguments (cont)
• For scanf: “pointers” to variables in which the
input will be stored
Example:
scanf("%s %f %c %d", name, &age, &gender, &id);

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


• Do NOT use ‘&’ with strings!
• ‘&’ is for scanf only!
36
Common Conversion Specifiers
for Numerical Information
• decimal integer: %d
printf(“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);
37
Conversion Specifiers for
Alphanumeric Information
• char: %c
printf(“What letter follows %c?\n”,ch);
scanf(“%c”, &nextchar);
• string: %s
printf(“Name: %s\n”, name);
scanf(“%s”, name);

38
printf: Conversion Specifiers
• 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
• L: placed before any float conversion
specifier to indicate that a long double is
displayed

39
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 integer


int idNumber;
scanf("%d", &idNumber);

40
scanf: Conversion Specifiers (cont)
• h or l: placed before any integer
conversion specifiers to indicate that a
short or long integer is to be input
long int idNumber;
scanf("%ld", &idNumber);

• l or L: placed before any float conversion


specifiers to indicate that a double or long
double is to be input
41
Conversion Example
Input octal integer
Output integer as decimal

42
Conversion Example (cont)
#include <stdio.h>
int main()
{
int i ;

scanf("%o", &i);
printf("%d\n", i);
return 0;
}

43
Conversion Example (cont)
#include <stdio.h>
int main() _
{
int i ;

scanf("%o", &i);
printf("%d\n", i);
return 0;
}

44
Conversion Example (cont)
#include <stdio.h>
int main() _
{
int i ;

scanf("%o", &i);
printf("%d\n", i);
return 0;
}

45
Conversion Example (cont)
#include <stdio.h>
int main() _
{
int i ;

scanf("%o", &i);
printf("%d\n", i);
return 0;
}
i

46
Conversion Example (cont)
#include <stdio.h>
int main() _
{
int i ;

scanf("%o", &i);
printf("%d\n", i);
return 0;
}
i

47
Conversion Example (cont)
#include <stdio.h>
int main() 70
{ _
int i ;

scanf("%o", &i);
printf("%d\n", i);
return 0;
}
i

48
Conversion Example (cont)
#include <stdio.h>
int main() 70
{ _
int i ;

scanf("%o", &i);
printf("%d\n", i);
return 0;
}
i
56
49
Conversion Example (cont)
#include <stdio.h>
int main() 70
{ _
int i ;

scanf("%o", &i);
printf("%d\n", i);
return 0;
}
i
56
50
Conversion Example (cont)
#include <stdio.h>
int main() 70
{ 56
int i ; _

scanf("%o", &i);
printf("%d\n", i);
return 0;
}
i
56
51

You might also like