Pages

Unformatted input/output statements

Introduction :
 These statements are used to Input/Output a single/group of characters from/to the input/output devices. Here the user cannot specify the type of data that is going to be input/output.

Input and Output Functions
(1) Unformatted Input/Output Statements:

Input                           Output

getc( );                         putc( );

getchar( );                    putchar( );

gets( );                         puts( );

(2) Formatted Input/Output Statements:

Input                        Output

scanf( );                    printf( );

fscanf( );                   fprintf( );

Single Character I/P Function
A Single character can be given to the computer using 'C' input  library function getchar()
Example 
Testing a character type

1  /* Program to testing a character type */
2  #include<stdio.h>
3  main()
4  {
5  char ch; /* Local definition */                                                    
6  /* statements */
7  printf("Enter any Character/Digit........");
8  ch=getchar();
9  if(isalpha(ch)>0)
10  printf("It is a alphabet");
11  else if(isdigit(ch)>0)
12  printf("It is a digit");
13  else
14  printf("It is alphanumeric");
15  }/* main */

Single Character I/O Function
 The putchar() function is used to display one character at a time on the standard output device. This function does the reverse operation of the single character input function as already discussed.

Example 
Convert a Character from Lowercase to Uppercase and viceversa.

 /* Program to convert a character from Lowercase to Uppercase */
#include<stdio.h>
 main()
{                                                                                                
    char ch;   /* Local definition */
    /* Statements */
    printf("Enter any alphabet either in Lower or Uppercase........");
    ch=getchar();
    if(islower(ch))
    putchar(toupper(ch)); /* toupper() converts the character to upper case */
    else
    putchar(tolower(ch)); /* tolower() converts the character to lower case */
 }/* main */

getc() Function
This is used to accept a single character from the standard input to a character variable.

Syntax 
character variable=getc();

Description  Character variable is the valid 'c' variable of the
type of char data type

Example    char c;
                 c=getc();

putc() Fucntion
This is used to display a single character in a character variable to  standard output device.  

Syntax          putc(character variable);
Description  Character variable is the valid 'c' variable of the
type of char  data type

Example
char c;
putc(c);

gets & puts Function

The gets() function is used to read the string (string is a group of characters) from the standard input device (keyboard). 
The puts() function is used to display/write the string to the standard output device(Monitor).

Example 
Program using gets() and puts() function

/* Program using gets() and puts() function */

#include<stdio.h>
main()
{
      char scientist[40]; /* Local definition */
      /* Statements */
      puts("Enter Name:");
      gets(scientist); /* gets the character from keyboard */
      puts("Print the Name:");
      puts(scientist);
 } /* main */

Character Test Function
The 'C' language provides many of character test functions, that are used to test the character taken from the input. The below table summarizes some of the  'C'  character test functions.

 Function                                    Test
isalnum(ch)                     Is ch an alphanumeric character? 
isalpha(ch)                      Is ch an alphabetic character?
isdigit(ch)                       Is ch a digit?
islower(ch)                     Is ch a lowercase letter?
isupper(ch)                    Is ch a uppercase letter?
isspace(ch)                    Is ch a blank space character?
tolower(ch)                   Convert ch to lowercase.
toupper(ch)                   Convert ch to uppercase.

No comments:

Post a Comment