Wrong number of parameters to printf leads to strange results

后端 未结 4 1728
臣服心动
臣服心动 2021-01-27 01:26
#include 

int main() {
   int i=10,j=20;
   printf(\"%d%d%d\",i,j);
   printf(\"%d\",i,j); 
   return 0;
}

Using the Turbo C compiler,

4条回答
  •  无人共我
    2021-01-27 02:01

    printf("%d%d%d",i,j); =>
    

    you are telling printf to print three integer, but you provide just three, so printf prints some garbage from the stack. the other:

    printf("%d",i,j);
    

    is supposed to take just one integer, but you are passing two. The C language does not guard against such errors, and what's happen is completely undefined, so to explain how you see exactly these outputs is difficult unless you know your compiler internal, and not so useful since that code is wrong and expected to fail.

提交回复
热议问题