Friday, January 27, 2012

confusing code of loop in c programming

Q.1) What is the output of the program.
      #include<stdio.h>
      #include<conio.h>
       main()
      {
        for(; ;);
        printf("C program");
        getch();
      }
Ans:-Nothing will print.
Discusssion:-Here for loop ended with semicolon. Such for loop behave like infinite                         .                    loop. And the printf function never executed.

Q2)How many time loop will execute?
   #include<stdio.h>
   #include<conio.h>
   main()
  { 
      int i=5;
      for(;printf("%d",i););
          
      getch();
  }
Ans:-Loop will execute infinite time. And 5 will print infinite number of time.
Discussion:-In for loop increment or decrement part and also loop termination condition is missing.

Q3)Output of the program
 #include<stdio.h>
 #include<conio.h>
 main()
 { 
    while(0)
    {
            printf("C");
    }
    printf("C++");
    getch();
 }
Ans:-C++;
Discussion:-If we put 0 in condition checking in while loop then compiler treat  while    loop condition as false. And it simply avoid the while loop.

 Q4)Output of program
      #include<stdio.h>
      #include<conio.h>
      main()
     { 
       while(1)
      {
           printf("C");
      }
           printf("C++");
   
          getch();
     }
Ans:-Infinite number of C
Discussion:-In while loop checking condition, the non-zero value is treated by compiler 
                    As the condition is true. So while loop will continue infinite number of time.

Q5)Output of the program.
      #include<stdio.h>
      #include<conio.h>
       main()
      { 
        while(int i=0;i<10)
        {
           printf("C");
           i++;
        }
        printf("C++");
        getch();
    }
Ans:-Compilar error
Discussion:-We cannot use multiple condition/initialization within while loop. And we
                    Also cannot make while condition as blank. For example while( ) is syntactical  error.

 Q6) Output of the program.
 #include<stdio.h>
#include<conio.h>
main()
{
     
      while(printf("a"))
      {
        printf("b");
      }
     
      getch();
}
Ans:- infinite number of times ab.
Discussion:-At first printf function within while condition will print a. Then printf function will return 1.And then while loop will true. And the printf function within loop will print. And the iteration will go on infinite number of times.


Q7) What will be the output of the program.
 #include<stdio.h>
#include<conio.h>
main()
{
     
      int p;
      while(scanf("%d",&p))
      {
        printf("%d",p);
      }
     
      getch();
}

Ans:-p will scan and value of p will print infinite number of time.
Discussion:-At first scanf function(Within while loop)will scan a value and then scanf function  will return 1, ie loop condition is true. Within loop the value will print.And as the loop condition is true then loop will compute again. And this will happen infinite number of times.

 
Q8) Output of the program.
#include<stdio.h>
#include<conio.h>
main()
{
     
  int i,p,q;
  for(p=1,i=5,q=5;i<=10;i++)
  printf("A");
  getch();
}               

Ans:-Five times A will print.
Discussion:-We can use more than one (n numbers) initialization within for loop. No syntax error will occur.

Q9)output of the program.

 #include<stdio.h>
 #include<conio.h>
  main()
  {
   int a=0,b=1,c=2;   
   while(b,c,a)
   {
     printf("C");
   }
    getch();
}

Ans:-Nothing will print.
Discussion:-Here comma operator is used in while loop condition .In comma operator the last value will check in while loop here 0 will come in place of while loop .So the loop will false.

 
Q10)Output of the program.
#include<stdio.h>
#include<conio.h>
main()
{
      int p=10;
      for(p<50;p++;)
      printf("C");                
      getch();
}

Ans:-C will print infinite number of time.
Discussion:-Within for loop we are checking condition in first section and incrementing p in place of condition section. So loop syntax is violated. Loop will continue infinite number of times.


 Q11)Out put of the program.
#include<stdio.h>
#include<conio.h>
main()
{
     int a=1,b=2;
      while(a*b+10)
      {
             printf("C++");
      };
      getch();
}
Ans:-C++ will print infinite number of time.
Discussion:-a*b+10=12. In while loop condition non zero value is always true. So loop will continue infinite number of time.



1 comment: