The Gateway to Computer Science Excellence
+41 votes
5.5k views

Consider the following C program:

#include<stdio.h>
int main()
{
    int i, j, k = 0;
    j=2 * 3 / 4 + 2.0 / 5 + 8 / 5;
    k-=--j;
    for (i=0; i<5; i++)
    {
        switch(i+k)
        {
            case 1: 
            case 2: printf("\n%d", i+k);
            case 3: printf("\n%d", i+k);
            default: printf("\n%d", i+k);
        }
    }
    return 0;
}

The number of times printf statement is executed is _______.

in Programming by Veteran (105k points) | 5.5k views
0
how the different cases have chosen when the loop is executed?
+9

Important point -->

'*'  and '/'  have the same precedence and are always executed left to right. https://stackoverflow.com/questions/21500961/which-has-higher-precedence-in-c-multiplication-or-division

0
1,1,3,3,2   =10

1 Answer

+77 votes
Best answer
 j=2 * 3 / 4 + 2.0 / 5 + 8 / 5;

$j = (((2 * 3) / 4) + (2.0 / 5) ) + (8/5)$; //As associativity of $+,*$ and $/$ are from left to right and $+$ has less precedence than $*$ and $/$.

$j = ((6/4) + 0.4) + 1);$ //$2.0$ is double value and hence $5$ is implicitly typecast to double and we get $0.4$. But $8$ and $5$ are integers and hence $8/5$ gives $1$ and not $1.6$

$j = (1 + 0.4) + 1$; // $6/4$ also gives $1$ as both are integers

$j = 1.4 + 1$; //$1 + 0.4$ gives $1.4$ as $1$ will be implicitly typecast to $1.4$

$j = 2.4$; // since $j$ is integer when we assign $2.4$ to it, it will be implicitly typecast to int.

So, $j = 2$;

$k -= --j$;

This makes $j = 1$ and $k = -1$.

The variables $j$ and $k$ have values $1$ and $-1$ respectively before the for loop. Inside the for loop, the variable $i$ is initialized to $0$ and the loop runs from $0$ to $4$.

$i=0, k=-1, i+k=-1$, default case is executed, printf $count = 1$

$i=1, k=-1, i+k=0$, default case is executed, printf $count = 2$

$i=2, k=-1, i+k=1$, case 2, case 3 and default case is executed, printf $count = 5$

$i=3, k=-1, i+k=2$, case 2, case 3 and default case is executed, printf $count = 8$

$i=4, k=-1, i+k=3$, case 3 and default case is executed, printf $count = 10$

$i=5$, loop exits and the control returns to main

Answer is 10.

by Active (1.6k points)
edited by
0
j=2 * 3 / 4 + 2.0 / 5 + 8 / 5;
2.0/5 is 0   integer division,  8/5 is 1 ,
now 2*3/4  here * and / having same precedence then check associativity.. associativity is left to right then 2*3 should b calculated first then division..
so by doing this value of j is 2.
sir correct  me ..
+9
2.0/5 is not integer division. It is floating point division. You can see the updated answer.
0
thanku sir
+3
Sir , I have a doubt... sir only I know that when switch expression matches with any case then only that case will be execute... but here it is not happening...Please clear my doubt
+3
@ankit only that case will be execute if there is break; statement in each case but here no break statement so it will go through all cases till default case
0
ok thanks :)
–1
how you take count value in c programe....i did not understand SHYAM
0
@ankit pandey

Question  is asking about the number of times printf statement is executed  . So here printf  count is used just for count the printf statement .  When printf  count =1 means it has executed one time .....I

hope it will clear u a bit.
+1
Sir, when all variable are given as int then why we are taking double type division, we should take only integer division right?

Why we are doing explicit type casting when it not needed?
0
very beautiful question
0
at i=2 , how is printf count = 5?

so as for i=3 how is it 8?

please explain
+1
@Parth

bcoz, as there is no break statements in the switch so once a case is matched then all other subsequent cases(including default)

will be executed without looking at the case labels.
0
even if we don't consider implicit typecasting then also we're getting j=2
0

If we put  2 * 3 / 4 + 2.0 / 5 + 8 / 5 in GATE calc it is giving us 3.5 !

Please explain the reason.

Thanks in advance!

+1
@Hirak In general Calci follows BODMAS Rule but C has its own rules...
0

@Punit Sharma

Is there any rule like, character scans from right to left (like we do it in printing statement , which scans variable from right to left , but prints from left to right)??

Or only associativity or precedence can do everything??

0

@Arjun sir

I think computation for j is wrong as * and / are not sequence point in C.

So expression 2*3/4 can have value either 0(by evaluating 3/4 first and then multiplying with 2) or 1(by evaluating 2*3 first and then dividing by 4).

Please suggest whether I'm right or wrong.

0

@dileepbhagat2196 you are wrong. You should know precedence and Associativity rule for this question. * and / are of same precedence but Associativity is left to right so first 2*3 will be solved. So (2*3)/4= 6/4= 1

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,645 questions
56,586 answers
195,788 comments
101,839 users