Input/Output Functions in C

C language has a collection of functions that can be used in a program with the required number of arguments written in parentheses. Input/Output Functions in C are also included in the user program by using the header file < stdio.h> which stands for standard input-output. header.
There are major two types of Input/Output Functions in the C programming language.

  • Formatted input/output functions
  • Character input/output functions

    1. Formatted input/output functions :

  • scanf() Function : scanf() function is used to read/input values of variables using the standard input device such as keyboard. It has the following the syntax :
    scanf("format string", &variable1,&variable2,&variable3,...&variablen);
    

    where “format string” is the control string which represents the format specification, the symbol & (ampersand) which represents the memory address where the variable value is to be stored.

    Example :

    1. scanf("%d%d", &a,&b); // to read value of <strong>int</strong> variables a and b.
    2. scanf("%f%f", &a,&b); // to read value of <strong>float</strong> variables a and b.
    3. scanf("%c", &school); // to read value <strong>char</strong> value for variable school.
    4. scanf("%s", str); // to read a string of <strong>char</strong> variable str.
    

    format specifiers

  • printf() Function : printf() function is used to print/display values of variables using the standard output device such as monitor. It has the following syntax :

    ("format string", variable1,variable2,variable3,...variablen);
    

    where variable1,variable2,variable3,…variablen are variables whose values are to be displayed in the monitor.
    1. printf("%d", a); // to print value of int variables a.
    2. printf("%f", x); // to print value of float variables x.
    

    2. Character input/output functions :

  • getch() Function : getch() function is used to read a character from the keyboard and it does not expect the enter key press. It has the following syntax :
    ch=getch(); // where ch is a char variable.
    
    Example:
    char ctr;
    ctr=getch();
    

  • getchar() Function : getchar() function is used to read one character at a time from the standard input device such as keyboard. It has the following syntax :
    ch=getchar(); // where ch is a char variable.
    

  • putchar() Function : putchar() function is used to display one character at a time on the monitor screen. It has the following syntax :
    putcher(ch); // where ch is a char variable.
    Example :
    char c='M';
    putchar(c);
    

  • putch() Function : putch() function is used to display a character on the monitor screen.It has the following syntax :
    putch(ch); // where ch is a char variable.
    Example:
    char c='s';
    putch(c);
    

  • getche() Function : getche() function is used to read a character from the keyboard without expecting the enter key press. It has the following syntax :
    ch=getche(); // where ch is a char variable.
    
    Example:
                  char ctr;
                  ctr=getche();
    

  • gets() Function : gets() function is used to read a string of characters including white spaces. It has the following syntax :
    gets(str); // where str is a character string variable.
    Example :
    char str[15];
    gets(str);
    

  • puts() Function : puts() function is used to display a character string on the monitor screen. It has the following syntax :
    puts(str); // where str is a string (array of characters)
    Example :
    char str[20]=" Hello World"
    puts(str);
    

  • clrscr() Function : clrscr() function is used to clear the monitor screen. It has the following syntax :
    clrscr();
    Note: The header file <conio.h> must included to use this function in a program.
    

    Recommended Posts:

    C
        1. Fundamental of C Language
        2. Keywords in C
        3. Data Types in C
        4. Operators in C
        5. Type Conversion in C
        6. Input/Output Functions in C
        7. C Program to Swap Two Numbers With or Without Temporary Variables
        8. Control Statements in C – if, else, switch
        9. Loop Control Statements in C | for, while, do-while loop
        10. Comparison Between for loop while loop and do-while loop
        11. Unconditional Control Transfer Statements in C
        12. C Program to Check Armstrong Number
        13. C Program to Generate Fibonacci Series
        14. C Program to Check Whether a Number is Prime or Not
        15. C Program to Convert Binary Number to Decimal
        16. C Program to Convert Decimal Number to Binary
        17. Arrays in C – One-Dimensional, Two-Dimensional Array
        18. C Program to Find the Largest Number in an Array
        19. C Program to Sort an Array in Ascending Order
        20. C Program to Add Two Matrices
        21. C Program to Multiply Two Matrices
        22. C Program to Find Transpose of a Matrix
        23. String Manipulations In C
        24. C program to count number of vowels in a string
        25. C Program to Check whether the Given String is a Palindrome
        26. C Program to Concatenate Two Strings without using strcat()
        27. C Program to Convert Lowercase to Uppercase
        28. Functions in C
        29. C Program to Find Factorial of a Number using Function
        30. C Program to Reverse a Number using Function
        31. C Program to Reverse a String using Function
        32. Recursion in C
        33. C Program to Find Factorial of a Number Using Recursion
        34. C Program to Print Fibonacci Series using Recursion
        35. Local and Global Variables in C
        36. Storage Classes in C
        37. Pointers in C
        38. Call by Value in C using Pointers
        39. Call by Reference in C using Pointers
        40. Difference between Call by Value and Call by Reference
        41. C Program to Add Two Numbers using Pointers
        42. C Program to Find Largest of Two Numbers using Pointers
        43. Dynamic Memory Allocation in C | malloc(), calloc(), free() and realloc()
        44. C Program to count Vowels in a string using Pointer
        45. C program to Check if a string is palindrome using Pointers
        46. C Program to Copy String Using Pointers
        47. Structure and Union in C
        48. Difference between Structure & Union
        49. File handling in C
        50. C Program to Create a File and write in it
        51. C program to copy contents of one file to another