Formatted I/O

Formatted I/O

Published by: Nuru

Published date: 21 Jun 2021

Formatted I/O Photo

Formatted I/O

Formatted I/O can be studied in the following two topics:

1. Formatted Input

  • Formatted input refers to input data that has been arranged in a particular format.
  • The built-in function scanf() can be used to enter input data into the computer from a standard input device.
  • The general form of scanf() is, scanf(“control string”, arg1, arg2, ..., argN);
  • The general format of a control string is:
    [whitespace character][ordinary character]%[field width][conversion character]

Note:

The programs succeeding this slide clarify the use of formatted input.

#include
#include
void main()
{
int n1;
char ch;
printf(“Enter a number:”);
scanf(“%d”,&n1);
printf(“\nEnter a character:”);
scanf(“ %c”,&ch); //space before %c
printf(“\n Number: %d\t Character: %c”,n1,ch);
getch();
}

#include
#include
void main()
{
int day,month,year;
clrscr();
printf("Enter day, month, year in DD ("Enter day, month, year in DD-MM-YYYY format:"); YYYY format:");
scanf(" %d-%d-%d", &day, &month, &year);
printf("\nDay: %d \t Month: %d \t Year:%d", day, month, year);
getch();
}

#include
#include
void main()
{
int n1;
clrscr();
printf("Enter a number of max 5 digits:");
scanf("%5d",&n1);
printf("Number: %d",n1);
getch();
}

#include
#include
#include
void main()
{
float p,t,r,si,ci,sa,ca;
clrscr();
printf("Enter principal in Rs:");
scanf(" %f",&p);
printf("\nEnter time in year:");
scanf("%f",&t);
printf("\nEnter rate in percent per annum:");
scanf("%f",&r);
si=(p*t*r)/100;
printf("\nSimple interest:%.2f",si);
ci=p*(pow(1+r/100,t)-1);
printf("\nCompound interest:%.2f",ci);
sa=p+si;
printf("\nSimple amount:%.2f",sa);
ca=p+ci;
printf("\nCompound amount:%.2f",ca);
printf("\nThe difference between compound and simple amount:%.2f",ca-sa);
getch();
}

Formatted Output

  • Formatted output refers to the output of data that has been arranged in a particular format.
  • The built-in function printf() is used to output data from the computer onto an standard output device i.e. screen.
  • The general form of printf() is, printf(“control string”, arg1, arg2, ..., argN);
  • The control string has the form,
    %[flag][field width][.precision]conversion character

Flags

Flags may be,

  • - (left-justified)
  • + (sign precedes)
  • 0 (leading 0s)
  • blank space (preceding space)
  • # (causes octal and hex items to be preceded by 0 and 0x)

Field Width

  • The field width is an integer specifying the minimum output field width.
  • If the no. of characters in the corresponding data item is less than the specified field width, then the data item will be preceded by enough leading blanks to fill the specified field.
  • If the no. of characters in the data item exceeds the specified field width, then additional space will be allocated to the data item so that the entire data item will be displayed.

#include
#include
void main() {
int a=10;
float x=11.123456;
clrscr();
printf("%-6d",a);
printf("\n");
printf("%+d",a);
printf("\n");
printf("%09d",a);
printf("\n");
printf("% d",a);
printf("\n");
printf("%#o",a);
printf("\n");
printf("%#0x",a);
printf("\n");
printf("%7.2f",x);                //precision format %w.pf
fetch();