Wednesday, September 25, 2013

Trace output of C program

TRACE THE OUTPUT OF THE FOLLOWING PROGRAMS 
#include<stdio.h>
#include<conio.h>
void main() {
  clrscr();
       int a=1;
          switch(a)
          {
                  case '1':
                      printf("ONE\n");
                      break;
                  case '2':
                      printf("TWO\n");
                      break;
                  defa1ut:
                      printf("NONE\n");
          }
         getch();
  }

=================================================================================
There is a mistake in the following program, can you identify it?

#include <stdio.h>
#include<conio.h>

void main()
{
clrscr();
        int a = 1,2;
        printf("a : %d\n",a);
        getch();
}

=================================================================================
Explain the output of the following C program (No, the output is not 20).
  #include<stdio.h>
  #include<conio.h>
  void main()
  {
      int a=1;
      switch(a)
      {   int b=20;
          case 1
          printf("b is %d\n",b);
          break;
          default:
          printf("b is %d\n",b);
          break;
      }
      getch();
  }

=================================================================================

What is the error of the following program?
  #include <stdio.h>
  #include <conio.h>
  void main()
  {
          int i;
          i = 1,2,3;
          printf("i:%d\n",i);
          getch();
  }


=================================================================================
The following is a simple C program to read and print an integer. But it is not working properly. What is(are) the mistake(s)?
  #include <stdio.h>
  #include <conio.h>
void main() { int n; printf("Enter a number:\n"); scanf("%d\n",n); printf("You entered %d \n",n); getch(); }

=================================================================================
What is the output of the following program?
  #include <stdio.h>
  #include <conio.h>
void main() { int i = 6; if( ((++i < 7) && ( i++/6)) || (++i <= 9)) printf("%d\n",i); getch(); }

=================================================================================

Write the output of the following program 

#defineABC     20
#define XYZ    10
#define XXX    ABC - XYZ
void main()
{
        int     a;
        a = XXX * 10;
        printf("%d ", a);
}

Solution:
a = xxx * 10  

which is => a = ABC - XYZ * 10

         => a = 20 - 10 * 10

         => a = 20 - 100

         => a = -80

=================================================================================
Writethe output of this program 

void main()
{        int  a, b, c, abc = 0;
         a = b = c = 40;
         if (c) {
               int abc;
               abc = a*b+c;
        }
        printf ("c = %d, abc = %d ", c, abc);
}

Solution: The result will be c = 40

================================================================================


No comments:

Post a Comment