Using Lunix What output do the following calls ofprintfshow

Using Lunix What output do the following calls ofprintfshow on screen?

a.printf(“%6d,%4d”, 86, 1040);

b.printf(“%.2f”, 20.253);

Solution

a) the following call of printf will show the output 86,1040 on the screen.

Even though it works fine it is preferrable to first initialise a variable and then execute the printf statement.

#include<stdio.h>

void main(){

printf(\"%6d,%4d\",86,1040);

return 0;

}

b) this statement is going to print 20.25

#include <stdio.h>

int main(void) {
  
   printf(\"%.2f\",20.253);
   return 0;
}

We use %2d,2.2f,6d, etc as width modifiers in a program. All these are used for formatting OutputStream specially Display devices data. Normally data forwarded to the I/O processor in actual format like number are in Decimal or fractional which one is this. Now if you want to do the formatting displaying content like currency, Time, Spacing and many more all these styles are used .

Suppose you are printing n= 18999 with printf(\"%2d\",n) then it will reserve the output console with 2 character and if the number of digits of n are larger than the specifier then it will print with no spaces, now suppose you print with %6d then what will happen the number of character it will reserve i.e. 6 now number of digits in n is 5 so it will print with after 1 character on the console printing with 1 space in %8d then leaving 3 spaces and then the number n.

%6.2f means:

that it will reserve the output console to 6 characters and rounding off to 2 decimal places.
n=1.9890778;
then printf(\"%6.2f\",n); will print 1.98
n=7687.87686;
then it will print 7687.87

Using Lunix What output do the following calls ofprintfshow on screen? a.printf(“%6d,%4d”, 86, 1040); b.printf(“%.2f”, 20.253);Solutiona) the following call of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site