You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于C语言中单个语句调用两个函数未影响值的疑问

关于C语言中单个语句调用两个函数未影响值的疑问

嘿各位C语言大佬,我最近用链表实现栈的时候遇到了个搞不懂的问题,先把我写的部分代码贴出来,麻烦帮我看看:

// Stack using LinkedList 
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* top = NULL;

short isEmpty(void) {
    if (top == NULL) {
        printf("error: The stack is empty.\n");
        return 1;
    }
    
    return 0;
}

void push(int value) {
    struct Node* newNode = (void*)malloc(sizeof(struct Node));
    if (!newNode) {
        printf("error: Memory allocation failed.\n");
        return;
    }
    newNode->data = value;
    newNode->next = top;
    top = newNode;
}

我在测试这个栈的时候,写了类似if (!isEmpty() && pop())这种单个语句里调用两个函数的代码,结果发现明明isEmpty判断栈是非空的,但执行完pop后,栈顶的状态好像没按我预期的变化;还有一次我写了isEmpty() & push(10),结果有时候栈明明已经被push了元素,isEmpty却还是打印了栈为空的错误提示。

我实在搞不懂,为什么同一个语句里调用两个操作栈的函数,栈的值就没被正确影响到?是我函数写的有问题,还是C语言里这种多函数调用的执行顺序有什么门道啊?有没有大佬能帮我捋捋这其中的逻辑?

备注:内容来源于stack exchange,提问作者Rohan Bari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 17:34:35