gpt4 book ai didi

c - printf 在 VS2013 中不使用 __cdecl

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:09 25 4
gpt4 key购买 nike

我今天的面试中有这样一个问题。

#include <stdio.h>

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

我的答案是5,1,但是面试官说是5,2!当然,我知道C中默认的调用约定是__cdecl,参数传递顺序是从右到左,所以我把这件事告诉了他。但他不相信。比我们在 VS2013 上运行它。它显示 5,2!!!!!

现在,我回到家并在 ideone 上再次尝试。答案是 5,1!!! http://ideone.com/sq6yRE为什么?!我对此很困惑。请问谁能帮助我?

最佳答案

在 C 中,函数参数的计算顺序是未指定的。

下面写的代码

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

可能会产生任何一个

Hello
Hi
3 6

Hi
Hello
3 6

作为输出。

你和你的面试官都不应该质疑为什么、如何等等。

关于c - printf 在 VS2013 中不使用 __cdecl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23357094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com