Discover the fundamentals of formatted and unformatted input/output functions in C programming. This comprehensive guide explains how formatted I/O functions like printf
and scanf
provide flexibility for displaying and receiving various data types with custom formats. Learn the syntax, use cases, and examples of these powerful functions. Additionally, explore unformatted I/O functions such as getch
, getchar
, and putchar
, which offer simpler, direct interactions with characters and strings. Whether you’re a beginner or looking to refine your understanding, this blog breaks down the key differences and practical applications of both formatted and unformatted I/O functions in an easy-to-follow manner.
Formatted I/O functions are used to receive various inputs from the user and present multiple outputs to the user.
These functions allow displaying the output in different formats using format specifiers.
They support all data types, including int, float, char, and others.
printf:
-Which is going to display integer,float,double ,string to the console
-It predefined function present int stdio.h library
Syntax:
printf(“format specifiers”, variable_name);
Example1:
#include<stdio.h>
int main()
{
int i=1;
float f=2.4;
printf(“%d”,i)
printf(“%f”,f)
printf(“%lf”,f)
printf”%Lf”,f);
}
scanf :
-used to get input from the user either it can be int,float ,double
#include<stdio.h>
int main()
{
int I;
scanf(“%d”i);
Syntax:
scanf(“format specifiers”, &variable1,&variable2,&variable3,&varible4);
sprintf:
-string print.(sprintf)
1.Instead of printing the data in the console they print in the character array.
sprintf(array_name, “format specifier”, variable_name);
Example:
#include<stdio.h>
int main()
{
char c[5];
int a=5,int b=6;
sprintf(c,”%d %d”,a,b);
printf(“%d%d”,c[0],c[1]);
printf(“%s”,c);
}
Sscanf:
String scanf:
-Takes the input from the character array or from the
#include<stdio.h>
int main()
char c[5];
int a=5;
int b=6;
int e;
int f;
sprintf(c,”%d %d”,a,b);
sscanf(c,”%d%d”,&e,&f);
printf(“%s”,c);
}
-It cannot use format specifiers
-It is only used in characters array or string and cannot be used anyother datatypes.
#include <conio.h>
#include <stdio.h>
int main() {
char ch;
printf(“Press any key: “);
ch = getch();
printf(“\nYou pressed: %c”, ch);
return 0;
}
example:
#include <conio.h>#include <stdio.h> int main() { char ch; printf(“press any key: “); ch = getche(); printf(“\nyou pressed: %c”, ch); return 0;}
example:
#include <stdio.h>
int main() { char ch; printf(“enter a character: “); ch = getchar(); printf(“you entered: %c”, ch); return 0;}
Example:
#include <stdio.h>
int main()
{
char ch = ‘A’; printf(“The character is: “); putchar(ch); return 0;}
#include <stdio.h>
int main() {
char str[] = “Hello, World!”;
puts(str);
return 0;}
Example:
#include <conio.h>
int main() {
char ch = ‘B’;
putch(ch);
return 0;
}
Indian Institute of Embedded Systems – IIES