Close
Close

predict output

   Bhashkar

#include<stdio.h>
int main()
{
char i=0;
for(i<=5&&i>= -1;++i;i>0)
    printf("%d ",i);
printf("\n");
return 0;
}

output: 1 2 3 4 5 ... 127 -128 -127... -2 -1

plz explain how this output came because there is no condn for termination of loop

  • This code is just printing every character and stops at the end.
    - Amit Kumar
  • But how it iterate 1 to -1 which cond in for loop to do that.
    - aMiT PaThAk
  • #include<stdio.h> int main() { char i=0; //for(i<=5&&i>= -1;++i;i>0) // printf("%d ",i); // can be transformed to: i<=5&&i>= -1; // FALSE but does nothing while (++i){ // beginning 0 gets set to 1,2,3, 127, -128 etc (8bit signed number) printf("%d ",i); i>0; //True or false, does nothing } //Ends after -128 because the next number is 0 which is FALSE in C and this ends the loop printf("\n"); return 0; }
    - Newboerg

Answers

  •   
    #include<stdio.h>
    int main() {
      char i=0;
      //for(i<=5&&i>= -1;++i;i>0)
        // printf("%d ",i); // can be transformed to:
    
      i<=5&&i>= -1; // FALSE but does nothing 
      while (++i){ // beginning 0 gets set to 1,2,3, 127, -128 etc (8bit signed number)
        printf("%d ",i); i>0; //True or false, does nothing
      } //Ends after -128 because the next number is 0 which is FALSE in C and this ends the loop 
    printf("\n");
     return 0; }
    

    • FOR (A,B,C) can be transformed into: A; while(B){ ...;C;}
      - Newboerg

Ask Yours
Post Yours
Write your answer