Types of Output and Input Functions in C
Types of Output and Input Functions in C
In C programming language the input refers to feeding the information to the compiler and
the output refers to displaying the information on the screen. The C language contains the
library which contains input and output functions included in it we just have to mention the
library and after that, we can use all the input and output functions.
The input and output functions are mainly divided into two types in C language and those are:
1. Formatted: In essence, the prepared functions receive or show the available data
(input) in a predetermined format. There are several functions for input-output
operations in the C standard library. These routines’ scanf c and printf c) methods
assist programmers in formatting the functions in the way they like. These functions
can be used by the software to read any type of data, including real numbers, integers,
characters, and many more.
2. Unformatted: The format used to write and retrieve the available data cannot be
controlled by the unformatted functions. As a result, these operations represent the
most fundamental kinds of output and input. We refer to these functions as
unformatted functions for input and output since neither the provision of input nor the
presentation of output is permitted in user format.
In the c language, there are many format specifiers which are used while using input and
output functions in the c language like scanf c and printf c.
%i, %d int
%lf double
%u unsigned int
%c unsigned char
%c char
%f float
%c signed car
1. printf() function
The printf c function is used to print the output of the given statements. To show any value,
including float, integer, character, string, etc. on the console screen, a C program uses the
printf c function. It is an already declared pre-defined function that is found in the stdio.h file
(header file).
Syntax of printf c function
The printf c follows a general syntax and that is shown below:
printf("Format Specifier", var1, var2, ...., varn);
Example of printing integer values
In the given example we will see the example to print the value of integer type variable in c
using printf c.
Code implementation:
#include <stdio.h>
int main()
{
int a;
a = 22;
printf("%d", a);
return 0;
}
Output
22
Explanation of the above code
In the above example we have seen an example to print the value of an integer using the
printf c function. We have taken the value 22 in the example and hence printed the same
value in the output as you can see in the above output image.
Example of printing string Values
In this example, we will learn how to output the value of a string in c using the printf c
function with code and output.
Code Implementation:
C
#include <stdio.h>
// Driver code
int main()
{
printf("This is an example to print string");
return 0;
}
Output
This is an example to print string
Explanation of the above code
As you can see in the above example that we have output the string using the printf c
function. We have just written the content that we have to output in the double quotes inside
the printf c function.
2. Scanf() Function
In a C application, the scanf c function is used to read or extract any value from the user’s
keyboard. These values can be of any data type, including integer, float, character, string, and
many more. This function is also a pre-defined function because it is declared in the header
file stdio.h. We utilize the &(address-of operator) in the scanf() function to save the value of
a variable on the variable’s memory location.
Syntax of scanf c function
The general syntax of the scanf c function is shown below you just have to follow this syntax
to get or give the input to the compiler:
scanf("Format Specifier", &var1, &var2, ...., &varn);
Example of taking integer as an input
In the following example we will see how to take an integer as in input from the user with the
help of scanf c and then print it with the help of the compiler with the help of printf c.
Code implementation:
#include <stdio.h>
int main()
{
int num1;
printf("Enter a integer number: \n");
scanf("%d", &num1);
printf("You have entered %d", num1);
return 0;
}
Output
Enter a integer number:
You have entered 10
Explanation of the above code
In the above code as you can see first we have asked the user to give the output by using the
scanf c function and then whatever value the user has entered we will print that value using
printf function and in the above example you can see we have entered the value 10. Hence,
the compiler is printing the value 10 as output.
3. getchar() function
Many times you came across a situation when you have entered a lot of character but has to
work only on one of them or the first one itself so in these type of cases the getchar function
came into help as we can use scanf in c for normal input but If the user types numerous
characters, the getchar() method is used to read only the first one. This function reads one
character at a time until the enter key is pushed. In stdio.h, this function is declared (header
file).
Syntax
Here we will see the general syntax of the getchar function whenever the user wants to work
on the first character he can use the getchar function in c and whenever wants to work on the
full input provided he can use scanf c.
Example of using getchar
In this example we will see the getchar function with proper example and code.
Code Implementation:
#include <stdio.h>
int main()
{
char ch;
printf("Enter the character: ");
ch = getchar();
printf("%c", ch);
return 0;
}
Output
Enter the character: a
Explanation of the above code
In the above code we have seen that when we entered a character getchar was able to read
that single character like in the above example we entered the character n and hence the
output is n.
4. putchar() function
Like getchar is used to read only a single variable the putchar is used to display a single
variable only if we want to print the whole content we can use printf c. By supplying a
character to the putchar() method directly or by passing a variable that has previously stored a
character, one character can be shown at a time. In stdio.h, this function is declared (header
file).
Syntax of putchar
The general syntax of putchar is explained in this section we just have to follow the general
syntax to use the putchar in our code. This is the general syntax of putchar:
putchar(variable_name);
Example of putchar
In this section, we will explain the example of putchar with proper code and explanation and
will see how to use putchar in code as you know how to use scanf c and printf c in code.
Code Implementation:
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
// Reads a character
ch = getchar();
putchar(ch);
return 0;
}
Output
Enter any character: n
Explanation of the above example
In the above example we can see that we have only entered a single character using getchar
and will print the single character using putchar.
5. gets()
The user enters a string or a collection of characters into the gets() method, which then stores
the characters in a character array. We can enter space-separated texts or strings using this
function. In stdio.h, this function is declared (header file).
Syntax of gets
char str[Length of string];
gets(str);
Example of gets
In this section, we will explain the example of gets with proper code and explanation and will
see how to use gets in code as you know how to use scanf c and printf c in code.
Code Implementation:
#include <stdio.h>
// Driver code
int main()
{
char name[50];
printf("Please enter some texts: ");
gets(name);
printf("You have entered: %s",
name);
return 0;
}
Output
Please enter some texts: You have entered: n
Explanation of the above example
In the above example we have used printf c to show the output and gets to get the output
instead of scanf c.
6. Puts() function
The puts() method in the C programming language is used to show a collection of characters
or strings that have previously been saved in a character array. In stdio.h, this function is
declared (header file).
Syntax of puts
Here we will show the general syntax of puts unction you just have to follow this syntax to
use puts in the code.
puts(identifier_name );
Example of puts
In this section, we will discuss the example of the puts function with proper code and
explanation of the same.
Code Implementation:
int main()
{
char name[100];
printf("Enter your text: \n");
gets(name);
printf("Your text is: ");
puts(name);
return 0;
}
Output
Enter your text:
Your text is: naman is a developer
Explanation of the above code
In the above code we have declared a character array name and have taken the input in that
using gets and shown the output using puts instead of printf c and scanf c.