1,214 views
1 votes
1 votes

What will be the value of f after the execution of following program

void main()
{
    char a;
    float f = 10;
    for(a=1; a<=5; a++)
    {
        f-=.2;
        printf("\nf = %g", f);
    }
}


A) 5.0
B) 9
C) 9.0
D) error

1 Answer

Best answer
2 votes
2 votes

Given Program is very straightforward .The only logic here is:

  f-=.2;

The above statement will be interpreted as

$f=f-.2;$

Here the for loop is executive five times ,and each time the value of f is decremted by $.2$.

So output will be :

Now the question is asking What will be the value of $f$ after the execution of following program

So final value of $f$ is $9$.

Why not option C?

%$g$  is for  It's there only to remove the trailing zeroes, otherwise it does the same thing as %$f$ or %$e$ (both of which are much more common).

 %$g$  specifies the shortest representation of %$e$ or %$f$.

So option B is correct.

edited by

Related questions

0 votes
0 votes
3 answers
1
Sankaranarayanan P.N asked Oct 27, 2016
584 views
What is the output of this C code?#include<stdio.h int main() { do printf("Inside while loop"); while(0); printf("After while loop"); }A) Infinite loopB) Compilation erro...
3 votes
3 votes
2 answers
2
Sankaranarayanan P.N asked Oct 27, 2016
411 views
What will be the output of the following C program fragment?int n =1; switch(n) { case 1: printf("One"); case 2: printf("Two"); case 3: case 4: case 5: default: printf("W...
2 votes
2 votes
1 answer
3
Sankaranarayanan P.N asked Oct 27, 2016
324 views
int add(int a) { static int count = 0; count = count + a; return(count); } main() { int a, b; for(a=0; a<=4; a++) b=add(a); }What is the value of b at the end of executio...
2 votes
2 votes
1 answer
4