Your browser doesn't support JavaScript Input and Output - Windows Programming

Input and Output

In C, Input sources and output destinations are collectively known as devices. All input and output to these devices are through streams. A stream is a device-independent sequence of characters in the form of bytes of data. A sequence of bytes flowing into a program is an input stream and a sequence of bytes flowing out of a program is an output stream. Every C stream is connected to a file acting as an intermediate step between the stream and the actual physical device used for input or output. 

Text Versus Binary Streams

C streams can be either text or binary.  Text streams are organised into lines of data terminated by a newline character. Certain characters in a text stream are used to convey functionality, such as the newline character however characteristics may vary from system to system and may have to be altered to conform to differing conventions for representing text in the host environment. In contrast, a binary stream can handle any data type since bytes of data in a binary stream aren’t interpreted or translated in any special way. This type of stream is generally most useful for non-textual data

Predefined Streams

The ANSI standard for C has three predefined streams –

typename
Standard inputstdin
Standard outputstdout
Standard errorstderr

C’s Stream Functions

The C language has standard libraries containing a variety of functions or methods that allow input and output in a program. By calling these library functions the user can manipulate the stream data. 

Unformatted output with putchar() , putc() , and fputc()

Unformatted I/O is the most basic form of data exchange between devices. Data is transferred in raw form or internal binary representation without any conversions. It is simple, efficient and compact.

putchar 

The putchar function writes a character to the standard output stream(stdout). The prototype for putchar is as follows:

putchar(int ch);

The value parameter ch may be a type char or type int thus a value of int c=89 or char c=’Y’ will produce the same output.

puts

The puts function writes a string str and a trailing newline to stream stdout. The prototype for this function is

puts(char* str)

where str is the C string to be written

fputs

The fputs() outputs a null-terminated string to any named output stream.  The prototype for fputs is as follows

int fputs(const char *str, FILE *stream)

The fput function takes two arguments: the first is the string to be written to the file and the second is the stream where the string will be written.

The code section below demonstrates the use of puchar, puts and fputs –

#include<stdio.h>
int main () {
int c=89;
char str[]="hello world";
putchar(c);//writes the character y (ascii 89)to stdout.
putchar('\n');//writes 'newline' to stdout.
putchar('Y');//writes the character y to stdout.
putchar('\n');//writes 'newline' to stdout.
puts(str); // writes the string str and a trailing newline to stdout.
fputs(str,stdout);// writes the string str and a trailing newline to stdout.
return(0);
}

Unformatted input with fgets() and getchar()

getchar

The getchar() function reads a single character from the terminal and returns it as an integer. You can use this method in a loop to read more than one character.  The short program below demonstrates the use of getchar by asking the user to enter a character and then echoing the character to the screen.

#include <stdio.h>
int main( )
{
int c;
printf("Enter a character followed by return ");
c = getchar();//prompts for screen input store ascii code of character
putchar(c);//writes character to stdout
}

fgets()

The fgets() function reads a specified number of characters from the input stream and stores it in a string. The prototype of fgets() is

fgets(string,size,stdin);

Here string is the name of a char array, size is the amount of text to input plus one (this must be no larger than the size of the char array), and stdin is the name of the standard input device, as defined in the stdio.h header file. 

The short program below demonstrates the use of the fgets() by asking the user to enter a string and then echoing the string to the screen.

#include<stdio.h>
int main()
{
char str[100];//declared character array of length 100 
printf("Enter a string followed by enter ");
fgets(str,100,stdin);//stores user input in chr array str
puts( str );//writes str to stdout
}

Formatted Input and output with the scanf() & printf() functions

In formatted I/O, binary data is converted to an internal standard, usually ASCII, before processing. Formatted I/O is portable and human-readable, but uses more space and carries more computational overhead because of the conversions between input and output. 

scanf 

Is a function that reads and stores formatted data from the standard input stream stdin. The prototype for scanf is

int scanf ( const char * format, ... );

Scanf() accepts a variable number of arguments but requires a minimum of two. The first argument is a format string that uses special characters to indicate how to interpret the input data. The second, and remaining arguments, are the addresses of the variable(s) to which the input data is assigned. 

printf 

Is a function that writes formatted data to the standard output stream stdout. The printf() function is part of the standard C library and is complementary to scanf.  The prototype for printf is

int printf ( const char * format, ... );

The printf() function can accept a variable number of arguments but requires a minimum of one.  The format string contains the text to be written to stdout and optional embedded format tags. These optional parameters are variables and expressions which describe further output formatting information. 

The following code shows the usage of both printf and scanf. When the code is compiled, it will ask the user to enter a name and age separated by a space. When a value is entered, it will be displayed on the screen.  The purpose of %d inside the scanf() and printf() functions is to provide string formatting information. 

#include<stdio.h>
int main()
{
 char name[20];
 int age;
 printf("Please enter your name & age - ");
 scanf("%20s %d", name,&age);//prompts for input max 20 characters and value.stores data in name and age
 printf( "Hello: %s %d years old", name,age);//writes name and age to stdout
}

some of the more common format parameters 

Format String Meaning
%d signed decimal number
%f floating point number
%u  decimal
%i integer as a signed number.
%c character
%s character string. The scanning ends at whitespace.
 %g%G floating-point number in either normal or exponential notation. %g uses lower-case letters and %G uses upper-case.
 %x, %X hexadecimal number. %x uses lower-case letters and %Xuses upper-case.
 %o Scan an integer as an octal number.

It is also possible to limit the number of digits or characters that can be input or output, by adding a number with the format string specifier such as “%1d” or “%4s”. The first specifier indicates a single numeric digit and the second specifier indicates 4 characters. Entering 99, while scanf() has “%1d”, means only one digit will be accepted regardless of how many digits are entered.