Formatted and UnformattedInput/Output
In C, the input/output functions can be classified into two categories on the
basis of how they handle the input/output in terms of formatting or structure:
Formatted I/O Functions
Formatted I/O functions help to display the output to the user in different
formats using the format specifiers. These I/O supports all data types like int,
float, char, and many more.
The formatted I/O functions in C are discussed below:
printf()
printf() function is used in a C program to display any value like float, integer,
character, string, etc on the console screen. It is a pre-defined function that is
already declared in the stdio.h(header file).
#include <stdio.h>
int main()
{
// Creating an int type variable
int a = 20;
// Printing the value of a variable
printf("%dn", a);
// Printing a string
printf("This is a string");
return 0;
}
Output
20
This is a string
scanf()
scanf() function is used in the C program for reading or taking any value
from the keyboard by the user, these values can be of any data type like
2.
integer, float, character,string, and many more. This is a pre-defined
function declared in stdio.h(header file). In scanf() function we use
&(address-of operator) which is used to store the variable value on the
memory location of that variable.
Syntax:
scanf("Format Specifier", &var1, &var2, ...., &varn);
#include <stdio.h>
int main()
{
int num1;
// Printing a message on
// the output screen
printf("Enter a integer number: n");
// Taking an integer value
// from keyboard
scanf("%d", &num1);
// Displaying the entered value
printf("You have entered %d", num1);
return 0;
}
Output
Enter a integer number:
You have entered 0
sprintf()
3.
sprintf stands for"string print". This function is similar to printf() function but
this function prints the string into a character array instead of printing it on
the console screen.
Syntax:
sprintf(array_name, "format specifier", variable_name);
#include <stdio.h>
int main()
{
char str[50];
int a = 2, b = 8;
// The string "2 and 8 are even number"
// is now stored into str
sprintf(str, "%d and %d are even number",
a, b);
// Displays the string
printf("%s", str);
return 0;
}
Output
2 and 8 are even number
sscanf()
sscanf stands for "string scanf". This function is similar to scanf() function but
this function reads data from the string or character array instead of the
console screen.
Syntax:
4.
sscanf(array_name, "format specifier",&variable_name);
#include <stdio.h>
Output
c = 2 and d = 8
Unformatted Input/Output Functions
Unformatted I/O functions are used only for character data type or character
array/string and cannot be used for any other datatype. These functions are
used to read single input from the user at the console and it allows to display
the value at the console.
The unformatted I/O functions in C are discussed below:
getch()
getch() function reads a single character from the keyboard by the user but
doesn't display that character on the console screen and immediately
returned without pressing enter key. This function is declared in
conio.h(header file). getch() is also used for hold the screen.
Syntax:
getch();
#include <conio.h>
#include <stdio.h>
int main()
{
printf("Enter any character: ");
5.
// Reads acharacter but
// not displays
getch();
return 0;
}
Output:
Enter any character:
getche()
getche() function reads a single character from the keyboard by the user and
displays it on the console screen and immediately returns without pressing
the enter key. This function is declared in conio.h(header file).
Syntax:
getche();
#include <conio.h>
#include <stdio.h>
int main()
{
printf("Enter any character: ");
// Reads a character and
// displays immediately
getche();
return 0;
}
Output:
Enter any character: g
6.
getchar()
The getchar() functionis used to read only a first single character from the
keyboard whether multiple characters is typed by the user and this function
reads one character at one time until and unless the enter key is pressed.
This function is declared in stdio.h(header file)
Syntax:
getchar();
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
// Reads and stores the character
ch = getchar();
printf("You entered: %cn", ch);
return 0;
}
Output:
Enter the character: a
a
putchar()
The putchar() function is used to display a single character at a time by
passing that character directly to it or by passing a variable that has already
stored a character. This function is declared in stdio.h(header file)
7.
Syntax:
putchar(variable_name);
#include <conio.h>
#include <stdio.h>
intmain()
{
char ch;
printf("Enter any character: ");
// Reads a character
ch = getchar();
// Displays that character
putchar(ch);
return 0;
}
Output:
Enter any character: Z
Z
gets()
gets() function reads a group of characters or strings from the keyboard by
the user and these characters get stored in a character array. This function
allows us to write space-separated texts or strings. This function is declared
in stdio.h(header file).
Syntax:
//Declare a char type variable of any length
char str[length of string in number];
gets(str);
8.
#include <conio.h>
#include <stdio.h>
intmain()
{
// Declaring a char type array
// of length 50 characters
char name[50];
printf("Please enter some texts: ");
// Reading a line of character or
// a string
gets(name);
// Displaying this line of character
// or a string
printf("You have entered: %s",
name);
return 0;
}
puts()
In C programming puts() function is used to display a group of characters or
strings which is already stored in a character array. This function is declared
in stdio.h(header file).
Syntax:
puts(identifier_name );
9.
#include <stdio.h>
int main()
{
charname[50];
printf("Enter your text: ");
// Reads string from user
gets(name);
printf("Your text is: ");
// Displays string
puts(name);
return 0;
}
putch()
putch() function is used to display a single character which is given by the
user and that character prints at the current cursor location. This function is
declared in conio.h(header file)
Syntax:
putch(variable_name);
#include <conio.h>
#include <stdio.h>
10.
int main()
{
char ch;
printf("Enterany character:n ");
// Reads a character from the keyboard
ch = getch();
printf("nEntered character is: ");
// Displays that character on the console
putch(ch);
return 0;
}