从函数内部调用函数

标签 c function

我正在尝试构建一个 CARDIAC C 中的计算机 模拟器,我到了需要从函数内部调用函数的阶段。


在下面的代码中,我试图调用函数 JAZ(),在函数 CARDIAC() 中,我得到一个明显的错误,说我不能调用它因为它不是预先定义的。
我需要做什么才能让编译后的程序 printf “Point reached!”

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int input;

void CARDIAC(int* ptr)
{
    int input = *ptr;

    while(input<900)
    {
        if(input<100 && input>=0)
        {
            // INP();
        }
        else if(input<200 && input>99)
        {
            printf("Point reached!\n");
            break;
        }
        else if(input<300 && input>199)
        {
            // LDA();
        }
        else if(input<400 && input>299)
        {
            // LDI();
        }
        else if(input<500 && input>399)
        {
            // STA();
        }
        else if(input<600 && input>499)
        {
            // STI();
        }
        else if(input<700 && input>599)
        {
            // ADD();
        }
        else if(input<800 && input>699)
        {
            // SUB();
        }
        else if(input<900 && input>799)
        {
            JAZ();
        }
        else
        {
            // HRS();
        }
    } 
    printf("Done\n");
}

void JAZ()
{   
    input = 180;
    CARDIAC(&input);
}

int main()
{
    input=820;
    CARDIAC(&input);

    return 0;
}


这是我得到的输出:

test.c: In function ‘CARDIAC’:
test.c:47:4: warning: implicit declaration of function ‘JAZ’ [-Wimplicit-function-declaration]
    JAZ();
    ^~~
test.c: At top level:
test.c:57:6: warning: conflicting types for ‘JAZ’
 void JAZ()
      ^~~
test.c:47:4: note: previous implicit declaration of ‘JAZ’ was here
    JAZ();
    ^~~

最佳答案

你必须把函数原型(prototype)放在两个函数之前,比如:

#include <stdio.h>
#include <stdlib.h>

int input;

/* function prototypes */
void CARDIAC(int *ptr);
void JAZ(void);

void CARDIAC(int* ptr)
{
    int input = *ptr;
 ....

关于从函数内部调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53622950/

相关文章:

Python 模块安装失败,因为 gcc 命令缺少标志...gcc 命令具有

c - 在冒泡排序中得到错误的输出

我的 SELECT DISTINCT 查询出现 Mysql 错误

python - 什么时候最好在 Python 中使用类?

Javascript 函数不返回数据 - Node-MySQL

c - fscanf 读取输入文件的问题

c - 如何使用 shift + ctrl 键在 C 中获得完全交互式的 bash 终端?

c - 用于传递多维数组的函数声明

python - 如何使用Cython将Python 3编译成C

function - 检查 Go 中首先完成的任务是什么?