Friday, 25 May 2012

input and output in detail

C language provides two basic method for input and output.

  1. Formatted I/O function
  2. Unformatted I/O function
Formatted I/O function

In this category printf() and scanf() comes. Because we need a specific format for I/O.
we  study above functions.

Unformatted I/O function

In this category getch(), getchar(), getche(), gets() comes for Input.

putch(), putchar(), puts() comes for Output.

Because we don't need any specific format for using these function.

getchar() function

We use this function for storing a single character value in variable. 

syntex
    
         char ch;
         ch=getchar();
program

int main()
{
    char ch;
    printf("enter value of ch  ");
    ch =getchar();
    printf("\n value of ch is \t");
    printf("%c",ch);
}

Input      enter value of ch a then hit enter
Output   value of ch is        a


This is simple implementation of getchar() function but if we enter more character in input
ex

Input      enter value of ch school then hit enter

In the above case first value stored in variable ch while other stored in buffer of computer. Which are automatically assigned to next character variable .


Example


int main()
{
    char ch,ch2;
    printf("enter value of ch  ");
    ch =getchar();
    ch2=getchar();
    printf("\n value of ch and ch2 is \t");
    printf("%c %c",ch,ch2);
}

Input      enter value of ch school then hit enter
Output   value of ch and ch2 is       sc
Points

  1. we need to hit enter after input .
  2. if we enter more than one character at time of input then first is stored in variable while other are stored in buffer memory.
  3. values in buffer memory automatically  assigned to next char input variable.
























No comments:

Post a Comment