C运行程序提示undeclared identifier

本文通过一个斐波那契数列的例子介绍了C语言中递归函数的使用,并强调了函数声明的重要性。文章还列举了一些常见的错误原因,帮助读者理解如何避免这类问题。

因为之前写java程序所以对于c的语言特性不是很了解,今天写一个递归小程序时一直提示该错误。

程序如下:

#include <stdio.h>
/*
斐波拉契序列
*/
int static i = 0;

int main(void){
    int first = 1;
    int second = 2;
    resNext(first,second);
    return 0;
}


void resNext(int a ,int b){
    int temp = 0;
    temp = b;
    b = a+b;
    a = temp;
    i++;
    printf("我是第%d次结果,值=%d\n",i,b);
    if(i<10){
        resNext(a,b);
    }
}

后来发现,c语言程序必须先声明函数再调用,java是可以先调用后定义函数,所以将代码中主函数放最后就可以正确运行,或者在主函数前先声明函数,即增加void resNext(int a,int b;。

另外附上一些该错误的其他可能原因,从网上找到的:

1,可能是你的变量名没有输对

2,可能没有包含标准头文件


我发现,只要你给的代码就没有一次能通过编译的,只要给代码编译错误必然一大堆! procedure GlobalExceptionHandler(Sender: TObject; E: Exception); begin // 记录异常到日志文件 AssignFile(LogFile, 'crash.log'); if not FileExists('crash.log') then Rewrite(LogFile) else Append(LogFile); Writeln(LogFile, '[' + DateTimeToStr(Now) + '] '); Writeln(LogFile, 'Exception class: ', E.ClassName); Writeln(LogFile, 'Exception message: ', E.Message); CloseFile(LogFile); // 显示错误信息 MessageBox(0, PChar('程序发生致命错误:' + E.Message), '错误', MB_ICONERROR); end; begin Application.Initialize; // Application.MainFormOnTaskbar := True; Application.OnException := GlobalExceptionHandler; // 设置全局异常处理 try Application.CreateForm(TMainForm, MainForm); except on E: Exception do begin // 创建主窗体失败,记录日志 AssignFile(LogFile, 'crash.log'); Append(LogFile); Writeln(LogFile, '[' + DateTimeToStr(Now) + '] 创建主窗体失败'); Writeln(LogFile, 'Exception class: ', E.ClassName); Writeln(LogFile, 'Exception message: ', E.Message); CloseFile(LogFile); MessageBox(0, PChar('创建主窗体失败:' + E.Message), '错误', MB_ICONERROR); Exit; end; end; [dcc32 Error] Project_HIDComm.dpr(13): E2003 Undeclared identifier: 'Exception' [dcc32 Error] Project_HIDComm.dpr(17): E2003 Undeclared identifier: 'FileExists' [dcc32 Error] Project_HIDComm.dpr(21): E2003 Undeclared identifier: 'DateTimeToStr' [dcc32 Error] Project_HIDComm.dpr(21): E2003 Undeclared identifier: 'Now' [dcc32 Error] Project_HIDComm.dpr(22): E2029 ',' or ')' expected but identifier 'ClassName' found [dcc32 Error] Project_HIDComm.dpr(33): E2009 Incompatible types: 'method pointer and regular procedure' [dcc32 Error] Project_HIDComm.dpr(38): E2005 'Exception' is not a type identifier [dcc32 Error] Project_HIDComm.dpr(43): E2003 Undeclared identifier: 'DateTimeToStr' [dcc32 Error] Project_HIDComm.dpr(43): E2003 Undeclared identifier: 'Now' [dcc32 Error] Project_HIDComm.dpr(44): E2029 ',' or ')' expected but identifier 'ClassName' found [dcc32 Error] Project_HIDComm.dpr(50): E2029 '.' expected but ';' found
10-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值