C Programming Puzzle #3


The code of the previous puzzle was the following:

#include <stdio.h>

void foobar(char *str1, char *str2){
  while (*((str1++)+6) = *((str2++)+8));
}

int main(){
  char str1[] = "Hello World";
  char str2[] = "Foo Bar Bar"; 

  foobar(str1,str2);

  printf("%s %sn",str1, str2);

  return 0;
}

The answer is that it prints “Hello Bar Foo Bar Bar”. Function foobar will basically copy the last 3 characters of str2 into str1, starting from the 7th character.

Ready for the next puzzle? Don’t worry, this one doesn’t involve strings…. What does the following program print?

#include <stdio.h>

int main(){
  int result,x,y,z;

  x = 1;
  y = 4;
  z = 3;

  result = x++ > 0 && --y%2 == 0 || ++z > 0;

  printf("%d %d %d %dn",result,x,y,z);
  
  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *