Search code examples
cprintfcalling-conventioncdecl

Doesn't printf use __cdecl in VS2013


There is a such question in my interview ,today.

#include <stdio.h>

int main(void) 
{
    char *s="123456790";
    printf("%c,%c",*(char *)((int *)s+++1),*s);
    return 0;
}

my answer is 5,1, but the Interviewer said it's 5,2! Of course, I knew the default calling convention in C is __cdecl,the argument-passing order is right to left, so I told to him about this. But he didn't believe it. Than we run it on VS2013. IT SHOWED 5,2!!!!!

Now,I come back home and try it again on ideone.THE ANSWER IS 5,1!!! http://ideone.com/sq6yRE WHY?! I am so confused about it .Who can help me,please?


Solution

  • In C the order of evaluation of function argument is unspecified.

    Code written below

    int main()
    {
      printf("%d %d\n", printf("Hi\n"), printf("Hello\n"));
      return 0;
    }
    

    May produce either

    Hello
    Hi
    3 6
    

    or

    Hi
    Hello
    3 6
    

    as output.

    And neither you nor your interviewer should question why, how etc.