Answers to questions on statements

  1. What is the behaviour of the following programme?
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
          int j, k;
          j = 0;
          k = 2;
          while (j=k)
          {
             j++;
             printf("%d\n",j);
          }
          return EXIT_SUCCESS;
    }
    
    Plain text version to try out

The program will keep writing out the number 2, since the value in the parentheses after the work if will always be 2, which is equivalent to True in C.
Answers to these questions

Back to the notes on statements