Question-
int i = 0 ;
main( )
{
printf ( "\nmain's i = %d", i ) ;
i++ ;
val( ) ;
printf ( "\nmain's i = %d", i ) ;
val( ) ;
}
val( )
{
i = 100 ;
printf ( "\nval's i = %d", i ) ;
i++ ;
}
Solution-
Easy problem but only confuses sometimes.so, let's see first i is a global variable. so changes made in the value of i would last ,returned accordingly , doesn't matter if you use call by value way.first i=0 ,1st printf statement would print 0. next,i++ would make value of i=1; in the function val(), i would become 100. so second printf statement, which in inside first function,would print 100.next i++ would make i=101. third printf statement would print i=101. it is changed value of i was changes by i++ in first function call. that's really fascinating about global variable.now,second function call would again make i=100 and final printf statement would print i=100. then , again i would be increment by i++ to i=101. if we put one more printf statement in the program, we can verify it.so finally results are
i=0;
i=100;
i=101;
i=100;
int i = 0 ;
main( )
{
printf ( "\nmain's i = %d", i ) ;
i++ ;
val( ) ;
printf ( "\nmain's i = %d", i ) ;
val( ) ;
}
val( )
{
i = 100 ;
printf ( "\nval's i = %d", i ) ;
i++ ;
}
Solution-
Easy problem but only confuses sometimes.so, let's see first i is a global variable. so changes made in the value of i would last ,returned accordingly , doesn't matter if you use call by value way.first i=0 ,1st printf statement would print 0. next,i++ would make value of i=1; in the function val(), i would become 100. so second printf statement, which in inside first function,would print 100.next i++ would make i=101. third printf statement would print i=101. it is changed value of i was changes by i++ in first function call. that's really fascinating about global variable.now,second function call would again make i=100 and final printf statement would print i=100. then , again i would be increment by i++ to i=101. if we put one more printf statement in the program, we can verify it.so finally results are
i=0;
i=100;
i=101;
i=100;
No comments:
Post a Comment