c - printf 序列点

标签 c

下一个代码的输出是:

40
30
20
10

 unsigned char numbers[] = {10,20,30,40};
 unsigned char* ptr = numbers;
 printf("%d\n%d\n%d\n%d",*ptr, *(ptr++), *(ptr++), *(ptr++) );

我认为++ 运算符是先完成的,所以我明白为什么打印的第一个值是 40,但是后面如何打印 30、20 和 10 呢?就这样倒退了!

最佳答案

您调用的是未定义的行为。

在 C 语言中,函数参数的计算之间没有顺序点。

你必须写:

printf("%d\n%d\n%d\n%d",*ptr, *(ptr), *(ptr+1), *(ptr+2) );
ptr += 3;

关于c - printf 序列点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53678109/

相关文章:

c - 二叉树只显示一半内容

c - 检查 IP 地址是否在范围/子网内的标准/安全方法

c - 在 Linux 终端中设置字体格式

c - 输入的值未写入数组

c - 在C中的字结构数组中存储元素

c - fork() - 多进程和系统调用

c - C : How can I stop the threads from interfering with each other? 中的多线程

c - 相同 C 库的路径/ namespace 冲突

c - popen() 和 sleep() 调用不起作用

c - 如何将数组的元素转换(不仅仅是输出)为其十六进制值