Loading...
C

Input And Output Functions

In almost every programming problem user need to enter some data to the program and want to get some specific result from the program. In programming , the instruction used to perform the function of Data Input from standard input such as keyboard and to get Output on standard Output unit such as Monitor are called Standard Input Output Functions.

In “C” language, these Input Output functions can be categorize into two categories:-

1. Formatted Input Output Function

2. Unformatted Input Output Function

1. Formatted Input Output Functions:

these functions are used to Input data from standard Input unit such as Keyboard and to get result on the standard Output unit such as Monitor. The formatted input and output functions are can read and write any type of data. As name suggest, these functions follow a basic fix format.

There are two Input Output functions.

  1. printf()     (Formatted Output Function)
  2. scanf()     (Formatted Input Function)

printf() Function:-

This is a formatted output function used to get printed output on the screen. It follows a fixed format. Its general form is 

Syntax:     printf(“format string”, list of variables);

Format string: It can contain Character that are simply printed as they are written

Example: printf(“Hello How are you?”);

Conversion or format specifier that begin with % sign , Escape sequencer begin with \ sign 

scanf() Function :-

This is a Formatted Input function used to enter data to the program. As printf() , it also follow a specific format .

Syntax: scanf(” Format string “,List of Addresses of variables);

Format string: It contain format specifiers starting with % sign , which tell the compiler the data type of the Input. Different format specifiers are shown below.

Format specifiers:-

Format specifiers begin with %sign and are used to tell the compiler to print the value store in variable and also tell the data type format.

Integer                                        %d

Short Signed                                     %u

Short Unsigned                                %ld

Long Signed                                      %lu

Long Unsigned                                 %x

Unsigned Hexadecimal                  %o

Float or Real                                     %f

Double                                               %lf

Character                                          %c

String or Array Of Characters      %s

List of addresses of variables contain the memory addresses of variable where the Input is stored. The memory of variable can be given using of & symbol.

Eg. scanf(“%d”, &num);

Example Program:

#include <stdio.h>

void main()

{

     int i;

     float f;

     char c;

     printf(“Enter an integer, float and character type values\n”);

     scanf(“%d %f %c”,&i, &f, &c);

     printf(“You entered:\n”);

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

}

2. Unformatted Input Output Functions :-

These are console Input Output Library function which deal with one character at a time and string function for Array of characters (String). The unformatted Input and Output functions are can read and write only character type of data.

The unformatted Input and Output functions are two types.

     1.Character input and output functions 

     2.String input and output functions

1.Character input and output functions:

Character input functions are getchar(), getche(), getch()

Character output functions are putchar(), putch()

1)getchar() Function:-

This is a unformatted console Input function which is used to enter or Input one character at a time from a standard Input device such as keyboard.

Eg.     char alphabet;

           alphabet= getchar();

/* In this , firstly we press a key which we want to enter ( as say ‘A’) and we need to press Enter key to move to the next instruction*/

2)getche() Function :-

This also comes in the unformatted console Input function which is used to enter (input) One character at a time. In this, the entered character is echoed/ displayed on the screen but the user not need to press Enter key to submit the character. As the key is press , it is accepted from the program.

Eg. char alphabet;

      alphabet-getche();

/* In this, user only press the character key (say ‘A’) and is accepted */

let’s see a demo how we use it in Array of characters.

3)getch() Function :-

This is also unformatted console Input function which is used to enter one character at a time. In this, the user neither to press Enter key nor is the character echoed on the screen. It is used where the user not want to show the Input.

Eg. char alphabet;

      alphabet-getch();

/* In this, we need only to press the character key and is immediately accepted */

let’s see a demo how we use it in Array of characters.

4)putchar() Function :-

This function is unformatted console Character Output function which is use to print one character (at a time) on the screen.

Eg.  main()

      {

            char ch;

            printf(“Enter a key”);

            ch=getchar();

            printf(“\n You have entered character”);

            putchar(ch);

        }

5)putch() Function :-

This function is unformatted console Character Output function which is use to print one character (at a time) on the screen.

Eg.  main()

      {

            char ch;

            printf(“Enter a key”);

            ch=getchar();

            printf(“\n You have entered character”);

            putch(ch);

        }

String Input and Output Functions :-

These functions are used to read a set of characters (string) from the keyboard and display a set of characters (string) on the screen.

gets() Function :-

This function is unformatted sting Input function. This is used to enter a string (Array of character) from standard Input device (such as keyboard). The length of entering string is limited from declaring string length (array. With this function, the user can enter multiple words in the same time.

Example :-If we want to enter the NAME of a person, we use string for this purpose. As trying this with scanf() function, we write it as

Using scanf() function 

#include <stdio.h>

void main()

{

   char name[15];

   clrscr();

   printf(“Enter name of 15 Characters”);

   scanf(“%s”, name);    /* We do not use & operator for string*/

   getch();

Using gets()function

#include <stdio.h>

void main()

{

   char name[15];

   clrscr();

   printf(“Enter name of 15 Characters”);

   gets(name);

}

puts() Function :-

This is an unformatted String Output function. This is used to print the Content of string on the standard Output device such as Screen.

Let’s discuss this function with the help of suitable example.

#include <stdio.h>

void main()

{

   char name[15];

   clrscr();

   puts(“Enter name of 15 Characters”);

   gets(name);                                          /*  String Input */

   puts(“The Name Entered is “);             /* String Output*/

   puts(“name”);

   getch();

Output:

Enter name of 15 Characters Raj Kumar

The Name Entered is Raj Kumar

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *