Math, asked by srsamanta87, 5 months ago

Find below few statements in C-language.
float x=18.23;
printf("%1.1f\n",x);
printf("%-8.3f" , x);
Determine the output of the above statements.​

Answers

Answered by mad210203
0

Output:

18.2

18.230  (At last there are 2 white spaces)

Explanation:

Program:

float x=18.23;

printf("%1.1f\n",x);

printf("%-8.3f",x);

Let us understand the program.

float x=18.23; //declaration of a float type variable

/* Consider %1.1f,

Generally,

a. represents minimum number of characters to be displayed. It may display more characters if the integer is bigger.

If the integer digits are less than a, it prints spaces.

.a represents minimum number of characters to be displayed after decimal point (.).

Here, 1 is the minimum number of characters and 1 is the number of characters after decimal point to be printed.

So, it prints 18.2.

*/

printf("%1.1f\n",x);

/* Consider %-8.3f,

Generally,

a. represents minimum number of characters to be displayed. It may display more characters if  the integer is bigger.

If the integer digits are less than a, it prints spaces.

.a represents minimum number of characters to be displayed after decimal point (.).

'-' sign tells that, spaces should be given at right side.

If negative is not given, spaces are given left side.

Here, 8 is the minimum number of characters and 3 is the number of characters after decimal point to be printed.

But, we have only 2 characters after decimal, so a zero (0) will be printed to compensate that condition.

To compensate 8 characters, it prints 2 spaces extra at right side.

So, it prints 18.230  .

*/

printf("%-8.3f",x);

Refer the attached image for the output.

Attachments:
Similar questions