Friday, 25 May 2012

gets and puts function

Situation : suppose we have list of students . And we need to enter their name. As normal no. of character in each name varies . So We can't use looping for storing values in string because looping runs till string size.


Similarly we want to enter space among different words. using normal input functions it's not possible.
Unformatted  String Input  function


If we want to enter a paragraph where we don't know expected word length then  formatted input function we can't use. For this purpose we we following function.
  1. gets() for input
  2. puts() for output


gets() function

We use this function for storing a string value in memory. using this we don't need to use looping. and this function doesn't bound us about memory.
syntex
         char str[size];
         gets(str);

int main()
{
    char str[10];
    printf("enter value of str  ");
    gets(str);
    printf("\n value of str is \t ");
    printf("%s",str);
  }

Input      enter value of  str  minty ji
          
Output   value of str is       minty

What's this output here. this happen because %s doesn't print after space. So how to handle this situation.


Solution:  Use puts() function. This function print every things untill enter character hit.


puts() function

We use this function for printing a single character value. 

syntex
         puts(str);

int main()
{
    char str[10];
    printf("enter value of str  ");
    gets(str);
    printf("\n value of str is \t ");
    puts(str);
  }

Input      enter value of  str  minty ji
          
Output   value of str is       minty ji


Read latest blog 

No comments:

Post a Comment