Aman Arora Aman Arora - 2 months ago 22
C Question

Code not executing

I am solving basic C exercises. The following code, when written, failed to execute. Can anyone suggest why?

#include <stdio.h>

int main()
{
int i = 4, j = -1, k = 0, w, x, y, z;

w = i || j || k;
x = i && j && k;
y = i || j && k;
z = i && j || k;

printf("w=%d x=%d y=%d z=%d", w, x, y, z);
return 0;
}


P.S. No errors being shown, but the execution window doesn't open.

Answer

The reason nothing seems to be happening is that the execution window doesn't hang around after the application terminates, and the application itself executes so quickly that you never get a chance to see the window before it's already gone.

One way to work around this is to insert a code that reads input before the application exits:

#include <stdio.h>

int main()
{
  int i = 4, j = -1, k = 0, w, x, y, z;

  w = i || j || k;
  x = i && j && k;
  y = i || j && k;
  z = i && j || k;

  printf("w=%d x=%d y=%d z=%d", w, x, y, z);  /* You probably want \n here. */

  getchar();

  return 0;
}
Comments