Data Input and Output Functions in C Programming

Data Input and output functions in C:

The C programming language  supports many library functions which includes a number of input/output functions. These functions are used to transfer the information between the computer and device(user).


  Some basic input/output functions are: 


    • scanf() and printf(): These two functions are used to transfer the single characters, strings and numerical values.


    • getchar() and putchar(): These two functions are used to transfer single characters.

    • gets() and puts(): is used to input and output strings.



    1. How to use scanf and printf functions:


    scanf() function is used to take input from user(keyboard).

    printf() function is used to display output on the screen.


      Example Program:


    #include<stdio.h>

    void main()

    {

          int num;// variable definition

           /*Display message and ask user to enter some number... using printf()*/

          printf("Enter the number:");

          /*Read the number entered by user... using scanf()*/

          scanf("%d",&num);

         /*Display the number with message...using printf() */

          printf("You entered number is:  %d",num);

    }



    In above program %d is used to scan the intiger value.

    If value is char then we use %c.

    If value is float then we use %f and so on...


    Above program shows the following output:

    Data input and output functions in C programming language


    2. How to use getchar and putchar functions in our program: 


    getchar() function reads  a single character from the terminal and returns it as an intiger.

    putchar() function is used to display these single character.



      Example program:


    #include<stdio.h>

    void main()

    {

          int a;

          printf("Enter a character:");

          /*Here, getchar function is used to store the accepted character in variable a*/

         a = getchar();    

         /*Display the character stored in variable a using putchar function*/

         putchar(a);

    }


    Above program shows the following output:



    3. How to use gets() and puts() function in C:


    In simple words:

    gets() function is used to accept string from user.

    puts() function is used to display the string.


      Example program:

     

    #include<stdio.h>

    void main()

    {

          //Declare character array to store string

          char str[100];

          printf("Enter a string: ");

           /*store string in array str*/

          gets(str);

          /*Display string on the output screen*/

          puts(str);

    }


    Above program shows the following output:


    No comments:

    Post a Comment

    If you have any doubts, please discuss here...👇