C-Programming-Class 8
C-Programming-Class 8
1
Session Objectives
2
Session Topics
• Streams and C
• C's Stream Functions
• Accepting Keyboard Input
• Screen Output
• Redirecting Input and Output
3
Streams and C
• A stream is a sequence of characters. More exactly, it is a
sequence of bytes of data.
• A sequence of bytes flowing into a program is an input
stream; a sequence of bytes flowing out of a program is an
output stream.
• Advantages:
– The major advantage of streams, therefore, is that
input/output programming is device independent.
– Programmers don't need to write special input/output
functions for each device (keyboard, disk, and so on).
– The program sees input/output as a continuous stream
of bytes no matter where the input is coming from or
going to.
4
Where is data?
5
Types of Streams
• Text Streams:
– A text stream consists only of characters, such as text
data being sent to the screen.
– Text streams are organized into lines, which can be up
to 255 characters long and are terminated by an end-of-
line, or newline, character.
– Certain characters in a text stream are recognized as
having special meaning, such as the newline character.
6
Types of Streams
• Binary Stream:
– A binary stream can handle any sort of data, including,
but not limited to, text data.
– Bytes of data in a binary stream aren't translated or
interpreted in any special way; they are read and
written exactly as-is.
– Binary streams are used primarily with disk files
7
Types of Streams
• Predefined Streams:
– ANSI C has three predefined streams, also referred to
as the standard input/output files.
8
C's Stream Functions
9
Example Program:
10
Accepting Keyboard Input
• Input functions are divided into a hierarchy of
three levels: character input, line input, and
formatted input.
• Character input:
– The character input functions read input from a
stream one character at a time.
– When called, each of these functions returns the next
character in the stream, or EOF if the end of the file
has been reached or an error has occurred.
11
Character input
12
Example Program: getchar() function
13
Example program: Using the getchar() function to input an entire
line of text
/* Using getchar() to input strings. */
#include <stdio.h>
#include <conio.h>
#define MAX 80
main()
{
char ch, buffer[MAX+1];
int x = 0;
while ((ch = getchar()) != '\n' && x < MAX)
buffer[x++] = ch;
buffer[x] = '\0';
printf("%s\n", buffer);
getch(); This is a string
return 0; This is a string
}
14
Character input
int getch(void);
15
Example Program: Using the getch() function.
16
Character input
• The getche() Function: getche() is exactly like
getch(), except that it echoes each character to
stdout.
• The getc() and fgetc() Functions:The getc() and
fgetc() character input functions don't
automatically work with stdin. Instead, they let the
program specify the input stream. They are used
primarily to read characters from disk files.
17
Line Input
18
Line Input
• The gets() Function: This is a straightforward
function, reading a line from stdin and storing it in a
string.
• The function prototype is: char *gets(char *str);
• gets() takes a pointer to type char as its argument and
returns a pointer to type char.
• The gets() function reads characters from stdin until a
newline (\n) or end-of-file is encountered; the newline
is replaced with a null character, and the string is stored
at the location indicated by str.
19
Line Input
• The fgets() Function: The fgets() library function is
similar to gets() in that it reads a line of text from an
input stream.
• It's more flexible, because it lets the programmer
specify the specific input stream to use and the
maximum number of characters to be input.
• The fgets() function is often used to input text from
disk files,
• The prototype of fgets() is
– char *fgets(char *str, int n, FILE *fp);
20
Example Program: Using the fgets() function for keyboard
input
/* Demonstrates the fgets() function. */
#include <stdio.h>
#define MAXLEN 10
main()
{
char buffer[MAXLEN];
puts("Enter text a line at a time; enter a blank to exit.");
while (1)
{
fgets(buffer, MAXLEN, stdin);
if (buffer[0] == '\n')
break;
puts(buffer);
}
return 0;
}
21
Enter text a line at a time; enter a blank to exit.
Output Roses are red
Roses are
red
Programming in C
Programmi
ng in C
22
Formatted Input
• scanf()
• fscanf()
23
The scanf() function’s Arguments
• The scanf() function takes a variable number of arguments; it
requires a minimum of two. The first argument is a format string
that uses special characters to tell scanf() how to interpret the
input. The second and additional arguments are the addresses of
the variable(s) to which the input data is assigned. Here's an
example:
scanf("%d", &x);
• The scanf() format string can contain the following:
– Spaces and tabs, which are ignored (they can be used to make the
format string more readable).
– Characters (but not %), which are matched against nonwhitespace
characters in the input.
– One or more conversion specifications, which consist of the %
character followed by special characters. Generally, the format
string contains one conversion specification for each variable.
24
Example Program: /* Clearing stdin of extra characters. */
#include <stdio.h>
void clear_kb(void);
main()
{
int age;
char name[20];
/* Prompt for user's age. */
puts("Enter your age.");
scanf("%d", &age);
/* Clear stdin of any extra characters. */
clear_kb();
/* Now prompt for user's name. */
puts("Enter your first name.");
scanf("%s", name);
/* Display the data. */
printf("Your age is %d.\n", age);
printf("Your name is %s.\n", name);
return 0;
}
25
void clear_kb(void)
/* Clears stdin of any waiting
characters. */
{
char junk[80];
gets(junk);
}
26
Screen Output
27
Using the putchar() Function
• The prototype for putchar(), which is located in
STDIO.H
– int putchar(int c);
– This function writes the character stored in c to stdout
– Although the prototype specifies a type int argument, you
pass putchar() a type char
– One can also pass it a type int as long as its value is
appropriate for a character (that is, in the range 0 to 255)
28
/* Demonstrates putchar(). */
#include <stdio.h>
main()
{
int count;
for (count = 14; count < 128; )
putchar(count++);
return 0;
}
29
Using printf() and fprintf() for Formatted Output
• The printf() function takes a variable number of
arguments, with a minimum of one.
• The first and only required argument is the format
string, which tells printf() how to format the output.
• The optional arguments are variables and expressions
whose values one want to display.
30
/* Demonstration of printf(). */
#include <stdio.h>
char *m1 = "Binary";
char *m2 = "Decimal";
char *m3 = "Octal";
char *m4 = "Hexadecimal";
main()
{
float d1 = 10000.123;
int n, f;
puts("Outputting a number with different field widths.\n");
printf("%5f\n", d1);
printf("%10f\n", d1);
printf("%15f\n", d1);
printf("%20f\n", d1);
printf("%25f\n", d1);
31
puts("\n Press Enter to continue...");
fflush(stdin);
getchar();
puts("\nUse the * field width specifier to obtain field
width");
puts("from a variable in the argument list.\n");
for (n=5;n<=25; n+=5)
printf("%*f\n", n, d1);
puts("\n Press Enter to continue...");
fflush(stdin);
getchar();
puts("\nInclude leading zeros.\n");
printf("%05f\n", d1);
printf("%010f\n", d1);
printf("%015f\n", d1);
printf("%020f\n", d1);
printf("%025f\n", d1);
32
puts("\n Press Enter to continue...");
fflush(stdin);
getchar();
puts("\nDisplay in octal, decimal, and hexadecimal.");
puts("Use # to precede octal and hex output with 0 and 0X.");
puts("Use - to left-justify each value in its field.");
puts("First display column labels.\n");
printf("%-15s%-15s%-15s", m2, m3, m4);
for (n = 1;n< 20; n++)
printf("\n%-15d%-#15o%-#15X", n, n, n);
puts("\n Press Enter to continue...");
fflush(stdin);
getchar();
puts("\n\nUse the %n conversion command to count
characters.\n");
printf("%s%s%s%s%n", m1, m2, m3, m4, &n);
printf("\n\nThe last printf() output %d characters.\n", n);
return 0;
}
33
Outputting a number with different field widths.
10000.123047
10000.123047
10000.123047
10000.123047
10000.123047
34
Use the * field width specifier to obtain field width
from a variable in the argument list.
10000.123047
10000.123047
10000.123047
10000.123047
10000.123047
35
Include leading zeros.
10000.123047
10000.123047
00010000.123047
0000000010000.123047
000000000000010000.123047
36
Display in octal, decimal, and hexadecimal.
Use # to precede octal and hex output with 0 and 0X.
Use - to left-justify each value in its field.
First display column labels.
38
Thank You!
39