fbpx

Exploring the Differences Between Formatted and Unformatted I/O in C

Formattted an unformatted Input/output functions


INTRODUCTION

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:

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.

Formatted I/O Functions

Formatted input/output  functions:

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);

}

Unformatted string:

-It cannot use format specifiers

-It is only used in  characters  array or string and cannot be used anyother datatypes.

getch():

  1. getch()
  • Description: Reads a single character from the keyboard without echoing it on the screen.
  • Header File: <conio.h> (in Windows-based compilers like Turbo C++ or Code::Blocks).
  • Behavior:
    • Does not require the user to press Enter.
    • Does not display the character on the screen.
  • Use Case: Often used to create pause screens or to receive hidden input (e.g., passwords).

#include <conio.h>

#include <stdio.h>

 

int main() {

    char ch;

    printf(“Press any key: “);

    ch = getch();

    printf(“\nYou pressed: %c”, ch);

    return 0;

}

 

2. getche()

  • description: reads a single character from the keyboard and echoes it on the screen.
  • header file: <conio.h> (windows-based compilers).
  • behavior:
    • does not require enter to be pressed.
    • displays the character as it is typed.
  • use case: used when immediate feedback for a single keypress is required.

example:

#include <conio.h>#include <stdio.h> int main() {    char ch;    printf(“press any key: “);    ch = getche();    printf(“\nyou pressed: %c”, ch);    return 0;}

 

3. getchar()

  • description: reads a single character from the standard input (keyboard).
  • header file: <stdio.h>
  • behavior:
    • requires the user to press enter after typing the character.
    • displays the character as it is typed.
  • use case: a standard and portable function for reading a character.

example:

#include <stdio.h> 

int main() {    char ch;    printf(“enter a character: “);    ch = getchar();    printf(“you entered: %c”, ch);    return 0;}  

4. putchar()

  • Description: Outputs a single character to the standard output (screen).
  • Header File: <stdio.h>
  • Behavior:
    • Prints a single character to the screen.
  • Use Case: A simple function for outputting characters.

Example:

#include <stdio.h> 

int main() 

{    

char ch = ‘A’;    printf(“The character is: “);    putchar(ch);    return 0;} 

5. puts()

  • Description: Outputs a string (sequence of characters) to the standard output (screen).
  • Header File: <stdio.h>
  • Behavior:
    • Automatically adds a newline after printing the string.
  • Use Case: A convenient function for outputting strings.

#include <stdio.h> 

int main() {    

char str[] = “Hello, World!”;    

puts(str);    

return 0;}

 

7. putch()

  • Description: Writes a single character to the standard output (screen).
  • Header File: <conio.h> (Windows-based compilers).
  • Behavior:
    • Similar to putchar() but specific to <conio.h>.
  • Use Case: Used in older compilers or Windows-specific programs.

Example:

#include <conio.h>

int main() {
char ch = ‘B’;
putch(ch);
return 0;
}