Skip to main content

Posts

Showing posts from March, 2023

What will be output of following c code using for loop?

  Output of following c code using for loop #include<stdio.h>  int main(){     static int i;     for(++i;++i;++i) {          printf("%d ",i);          if(i==4) break;     }     return 0; }   Output: 24 Explanation: Default value of static int variable in c is zero. So, initial value of variable i = 0 First iteration: For loop starts value: ++i i.e. i = 0 + 1 = 1  For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2 Loop incrimination: ++I i.e. i = 2 + 1 =3 Second iteration: For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 4. Since is equal to for so if condition is also true. But due to break keyword program control will come out of the for loop. Join us on Facebook:  Click Here  

What will be output of following c code while loop increment decrement?

  Output of following c code while loop                                increment decrement? #include<stdio.h> int main(){     int i,j;     i=j=2,3;     while(--i&&j++)          printf("%d %d",i,j);     return 0; } Output: 13 Explanation : Initial value of variable  i = 2 j = 2 Consider the while condition : --i && j++ In first iteration:  --i && j++ = 1 && 2 //In c any non-zero number represents true. = 1 (True) So while loop condition is true. Hence printf function will print value of i = 1 and j = 3 (Due to post increment operator) In second iteration: --i && j++ = 0 && 3  //In c zero represents false = 0  //False So while loop condition is false. Hence program control will come out of the for loop. Join us on Facebook:  Click Here