Thursday 5 December 2019

C Tutorial - Basic Input Output with Examples

Basic Input/Output

Introduction

Input refers to accepting data while output refers to presenting data. Every program performs three main functions accepting data from user, processing it and producing the output.

In C language there are several input and output functions.

These functions are collectively found in IO.h and such IO function together form a library namestdio.h .

User will require all such library function.

These library functions are classified into three broad categories:

 1. Console I/O functions

2. Disc I/O functions

3. Port I/O functions


Console I/O function

Console I/O refers to the operation that occur at the keyboard and the screen of your computer.

Console I/O function can also be classified in two parts:

1. Formatted console I/O:

Formatted I/O function accepts or present in a particular format. The example of formatted console I/O function is printf( ) and scanf( ).

printf

It is highly desirable that the output are presented in such a way that they are understandable and are in a form easy to use.

The printf() statement provides certain features through which the screen output is effectively controlled.
The general form of printf() function is:

printf("Control String ",arg1,arg2....);

Control string may contain:

1. character that are simply printed as they are.

2. Conversion specification that begin with a sign.

3. Escape sequence that begin with \ sign.

Given below is a list of conversion charcter that can be used with printf() function:


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. Therefore the names of variables are preceeded by the & sign.

Character strings are an exception to this. Since a string is already a character pointer, we give the names of string variables unmodified by a leading &.

Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalents.

2. Unformatted console I/O:

This function cannot control the format of reading and writing the data. some of the example of this function are getch( ),getche( ),getchar( ),gets( ),putchar( ),putch( )and puts( ).

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 useful 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.


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 toupper from the character conversion library ctype.h to each character in turn.


gets

gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines. When all input is finished,NULL as defined in stdio.h is returned

puts

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



Note: putchar, printf and puts can be freely used together. So can getchar, scanf and gets.

Disc & Port I/O function

Function which performs secondary storage devices like floppy disk or hard disk is called disk I/O functions.


Port I/O functions

This type of functions performs I/O operation among various codes like printer portmouse port.


Previous Page                                      Next Page 

More Topics : 

100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...