C Input Output Functions


In C language, input and output operations are performed using a set of library functions that are supplied with every C compiler. These functions are formally not a part of the language but they are considered standard for all input output operations in C. The set of library functions that perform input output operations is known as standard I/O library.

In C, if a program uses any function from the standard I/O library, then it should include the header file "stdio.h" as -
#include<stdio.h>
"stdio.h" header file contains library functions used for input and output operations. Note that conversion specifiers (%d, %c, %f etc.) are always used with printf() and scanf() functions to print or accept data values.
All the functions used for input and output operations are listed below :

C Output Functions

Output functions are used to write data from computer memory to the standard output devices such as monitor screen. The output function used widely in C language is printf() library function. With this function all type of values (numeric, character, or string) can be written as output. 

1. printf() Function

The printf function takes the following form -
printf("Anything written inside inverted commas will be printed as output");
Any Conversion specifiers used inside inverted commas will take corresponding Variable values. 
int age = 25;
printf("Age is %d",age);
// OUTPUTS 25

float marks = 87.85;
printf("Marks is %f",marks);
// OUTPUTS 87.85

char letter = 'R';
printf("Letter is %c",letter);
// OUTPUTS R

The printf() function can be used to print values of any type(int, char,float and double) including the string.


2. putchar() Function

The putchar() function can be used to output a single character value. putchar() outputs one single character at a time to the standard output device like Monitor Screen or We can say putchar() function puts a character on the screen. 
The syntax of putchar() is :
putchar(variable_name) ;
For Example :
char ch='A';

printf("The character is = ");
putchar(ch);
/* putchar(ch) outputs 'A' */

C Input Functions

Input functions are used to accept data into the Device memory from a standard input device like keyboard. 

1. scanf() Function


C provides scanf() library function to accept input data. This function can accept all types of input values including int,char, float and double.

scanf() function has the below syntax :
scanf("conversion_specifiers", address1, address2, ....); 
Conversion Specifiers should match the type of value accepted by the program. By using scanf() function we write the data on the address of the variable. Address of any variable can be accessed or used using the ampersand (&) sign.

Let's understand scanf() function with an example -
int num;
/* num will hold entered number and is of integer type */

printf("Enter a Number : ") ;
scanf("%d", &num);
/* We have used "%d" because we are accepting an int type of value and "&num" means the address of the variable num */
So, basically ; By using the scanf() function we are actually writing the value on the address of the variable.

Note : We don't use Conversion Specifiers for string values(Strings will be discussed later ).

Program Example :
#include <stdio.h>
int main(void){

    int age;
    printf("Enter age = ");
    scanf("%d",&age);
    printf("\nAge is = %d\n",age);
    // OUTPUTS age

    float marks;
    printf("Enter marks = ");
    scanf("%f",&marks);
    printf("\nMarks is = %f\n",marks);
    // OUTPUTS marks
}
Click on Run and Enter desired values in input field given below :

2. getchar() Function

You can accept a character value with the help of scanf() function too, however, some compilers doesn't support scanf() function to accept character type values. In those situations, We can read a single character value from the user with the help of getchar() function or We can say that getchar() function gets a character input from the user.

The syntax of getchar() is :
variable_name = getchar();
For Example :
char ch ;

printf("Enter a Character = ");
ch = getchar() ;
/* getchar() accepts user input and stores it in variable 'ch' */
printf("The character is = ");
putchar(ch);
/* putchar(ch) outputs character stored in 'ch' */
Click on Run and Enter desired values in input field given below :


A few points to be noted :

  • Everything inside printf() function's inverted Commas ("") will be printed as it is except for Conversion Specifiers and Escape Sequences.
  • printf() and scanf() functions can be used to output and input all the types of values respectively.
  • scanf() function uses addresses of the variables by using ampersand(&) sign to accept input values.
  • putchar() and getchar() are characters specific functions, used to output and input character values respectively.


Post a Comment