1,093 views
0 votes
0 votes
#Iinclude<stdio.h>                              /*output:Enter two four digit numbers:1234  5678

main()                                                              The two nubers are:12 and 34   */

{   int a,b;

printf("\n Enter two four digit numbers:");

scanf("%2d %4d",&a,&b);

printf("\n The two numbers are:%d and %d",a,b);

return 0;

}

----------Someone please explain why the output is such using binary bits concept.

1 Answer

1 votes
1 votes

%2d means:

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.

Similarly in your code while scanning the input %2d after 2 digit it will consider no space for scanning next %4d integer(maximum 4 digits expecting) continuously.

Let's consider your e.g

when you have entered 1234 first %2d take first 2 character for it's input and left 34 as treated as input for %4d which maximum requirement is 4 digit but it will take 2 digits as input after that compiler find space which mean the size of %4d input is only 2 digit that's why compiler not read your second input 5678.

If you want the perfect use, then first enter 2 digit number and give a space as u define in your scanf() then input your second integer may be lower, greater or equal to 4 digits. Now second number is use as second input in this case but it will take 4 consecutive  digits at max

%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
if you write printf("%10.2f",n);
then it prints 7687.87 with three space in the beginning because total character to the output console is 7 while we have reserved 10 characters so to compensate there will be 3 space in the beginning.

Related questions

0 votes
0 votes
0 answers
1
mohitjarvissharma asked May 21, 2018
987 views
Is reema thareja a suitable book for GATE Data structures?
1 votes
1 votes
1 answer
3