• Input means to provide the program with some data to be used in the program
  • Output means to display data on screen or write the data to a printer or a file.
  • C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.
  • In this tutorial, we will learn about such functions, which can be used in our program to take input from user and to output the result on screen.
  • All these built-in functions are present in C header file "#include<stdio.h>"
  • To use the built in functions we have to specify header files at head in which a particular function is defined(written) while discussing about it.



String I/O
gets() and puts()

  • Till now we have taken input from the scanf() function and we have displayed the data on the screen using printf()
  • But printf() and scanf() function has one drawback , that they dont work properly on string which contains space
  • It means printf() and scanf() function takes space " " character as deliminator
  • See this program to understand it better:
    void main()
    {
        char s[20];
    
    
        printf("Enter the string: ");
        scanf("%s",s);  //%s is use to take string as input.
         //ther is no need of writing & while taking string as input.
    
        printf("\nString is: %s",s);
    }
    

    Output:
    Enter the string: learn programming

    String is: learn
  • As you saw that it only print learn , because as space character came scanf() character took space as deliminator and from space character it did not took any character as input.
  • And , then printf() displayed the input string which was learn on the screen.


Strings I/O

  • By overcoming from the above problem , C language provides the string Input and Output Functions.
  • String input and output functions are used to take string as input from the user or to display string on the screen.
  • This functions does not take " " space character as deliminator.
  • Because of above functionality strings functions comes in action.







gets() function


Syntax:
   gets(array_name);

Let's take string as input:
void main()
{   //declaring the character arrays
    char a[10],b[10],c[10];
    
    
    printf("Enter the first string: ");
    gets(a);
    
    printf("Enter the second string: ");
    gets(b);
    
    printf("Enter the third string: ");
    gets(c);
}

Output:
Enter the first string: learn programming

Enter the first string: Core programming

Enter the first string: hello world






  • Now let's see puts() function to display the above strings



  • puts() function


    Syntax:
       puts(array_name);
    

    Let's print the strings:
    void main()
    {   //declaring the character arrays
        char a[20],b[20],c[20];
    
    
        printf("Enter the first string: ");
        gets(a);
    
        printf("Enter the second string: ");
        gets(b);
    
        printf("Enter the third string: ");
        gets(c);
    
    //printing the strings a,b,and c.
        printf("First string is ");
        puts(a);
    
        printf("Second string is ");
        puts(b);
    
        printf("Third string is ");
        puts(c);
    
    
    }
    

    Output:
    Enter the first string: learn programming

    Enter the first string: Core programming

    Enter the first string: hello world

    First string is learn programming
    Second string is Core programming
    Third string is hello world.


    • This is how we can take string as input from the user and display that string on the screen.





    People Also Searched: