Input – Output in C

Input and Output

Input and output are covered in some detail. C allows quite precise control of these. This section discusses input and output from keyboard and screen.

The same mechanisms can be used to read or write data from and to files. It is also possible to treat character strings in a similar way, constructing or analysing them and storing results in variables. These variants of the basic input and output commands are discussed in the next section

  • The Standard Input Output File
  • Character Input / Output
    • getchar
    • putchar
  • Formatted Input / Output
    • printf
    • scanf
  • Whole Lines of Input and Output
    • gets
    • puts

printf

This offers more structured output than putchar. Its arguments are, in order; a control string, which controls what gets printed, followed by a list of values to be substituted for entries in the control string

Example: int a,b;

Printf(“welcome”);

printf(“ a = %d,b=%d”,a,b);.

The printf() function is used for formatted output and uses a control string which is made up of a series of format specifiers to govern how it prints out the values of the variables or constants required. The more common format specifiers are given below

 

%c   character %f   floating point
%d   signed integer %lf  double floating point
%i    signed integer %e   exponential notation
%u   unsigned integer %s   string
%ld  signed long %x   unsigned hexadecimal
%lu  unsigned long %o   unsigned octal
%%  prints a % sign

 

For Example :-

int i ;

printf(  “%d”, i ) ;

scanf

scanf allows formatted reading of data from the keyboard. Like printf it has a control string, followed by the list of items to be read. However scanf wants to know the address of the items to be read, since it is a function which will change that value. T

Example: int a,b;

scan f(“%d%d”,& a,& b);

getchar

getchar returns the next character of keyboard input as an int. If there is an error then EOF (end of file) is returned instead. It is therefore usual to compare this value against EOF before using it. If the return value is stored in a char, it will never be equal to EOF, so error conditions will not be handled correctly.

As an example, here is a program to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control – d.

#include <stdio.h>

main()

{

int ch, i = 0;

while((ch = getchar()) != EOF)

i ++;

printf(“%d\n”, i);

}

putchar

putchar puts its character argument on the standard output (usually the screen).

The following example program converts any typed input into capital letters. To do this it applies the function to upper from the character conversion library c type .h to each character in turn.

#include <ctype.h>  /* For definition of toupper */  #include <stdio.h>  /* For definition of getchar, putchar, EOF */   main()  {   char ch;       while((ch = getchar()) != EOF)          putchar (toupper(ch));  }

gets

gets reads a whole line of input into a string until a new line or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines.

#include <stdio.h>

main()  {

char ch[20];

gets(x);

puts(x);  }

puts

puts writes a string to the output, and follows it with a new line character.

Example: Program which uses gets and puts to double space typed input.

#include <stdio.h>

main(){

char line[256];

while(gets(line) != NULL)

{

puts(line);

printf(“\n”);      /

}}

_flushall()

The _flushall function writes the contents of all output buffers to the screen and clears the contents of all input buffers. The next input operation (if there is one) then reads new data from the input device into the buffers.

This function should be used always in conjunction with the buffered input functions to clear out unwanted characters from the buffer after each input call.

getch() and getche()

These functions perform the same operation as getchar() except that they are unbuffered input functions i.e. it is not necessary to type ret to cause the values to be read into the program they are read in immediately the key is pressed. getche() echoes the character hit to the screen while getch() does not.

For example :-

char ch ;

ch = getch() ;

error: Content is protected !!