鱼C论坛

 找回密码
 立即注册
查看: 3352|回复: 9

难解的问题

[复制链接]
发表于 2012-4-12 15:51:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 quiet小朋友 于 2012-4-14 16:39 编辑

一代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main()
  4. {
  5. FILE *fp;
  6. fp = fopen("noexist","rb");
  7. fprintf(fp,"aa");
  8. if(fp==NULL) return;
  9. fclose(fp);
  10. }
复制代码
编译成功,可加了以下语句就不行:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main()
  4. {
  5.         int a;
  6.         a = 3;
  7. FILE *fp;
  8. fp = fopen("noexist","rb");
  9. fprintf(fp,"aa");
  10. if(fp==NULL) return;
  11. fclose(fp);
  12. }
复制代码
编译器提示:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

small.c
d:\我的文档\桌面\small.c(7) : error C2275: 'FILE' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\VC98\include\stdio.h(156) : see declaration of 'FIL
E'
d:\我的文档\桌面\small.c(7) : error C2065: 'fp' : undeclared identifier
d:\我的文档\桌面\small.c(8) : warning C4047: '=' : 'int ' differs in levels of indirection from 'str
uct _iobuf *'
d:\我的文档\桌面\small.c(9) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of ind
irection from 'int '
d:\我的文档\桌面\small.c(9) : warning C4024: 'fprintf' : different types for formal and actual param
eter 1
d:\我的文档\桌面\small.c(10) : warning C4047: '==' : 'int ' differs in levels of indirection from 'v
oid *'
d:\我的文档\桌面\small.c(11) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of in
direction from 'int '
d:\我的文档\桌面\small.c(11) : warning C4024: 'fclose' : different types for formal and actual param
eter 1
请按任意键继续. . .

只加了"int a; a = 3;"这句话,怎么就不行了?
(我试过,其他代码也是这样):'(:Q
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-4-12 16:53:29 | 显示全部楼层

回帖奖励 +2 鱼币

#include <stdio.h>
#include <stdlib.h>
//FILE *fp;
void main()
{
FILE *fp;
int a;
a = 3;
//FILE *fp;
fp = fopen("noexist","rb");
fprintf(fp,"aa");
if(fp==NULL)
  //return;
  exit(1);
fclose(fp);
}
错误原因:C语言不允许随时定义变量,所有定义的变量都只能放在函数开头,这也是CC++的重要区别。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-4-12 16:56:40 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>
void main()
{
        int a;
        a = 3;
FILE *fp;
fp = fopen("noexist","rb");
fprintf(fp,"aa");
if(fp==NULL) return;
fclose(fp);

}
可这也不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-4-12 16:57:50 | 显示全部楼层
写错写错:dizzy::dizzy::dizzy::dizzy:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-4-12 17:01:25 | 显示全部楼层
三克油,:lol:lol:lol
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-4-12 17:05:09 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-4-12 17:08:41 | 显示全部楼层
quiet小朋友 发表于 2012-4-12 17:05
谢谢lei老师!

不用谢,我才学C两年。不算老师。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-4-15 09:33:20 | 显示全部楼层

回帖奖励 +2 鱼币

回帖 就奖励~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2018-12-20 15:03:50 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-21 11:48:50 | 显示全部楼层

回帖奖励 +2 鱼币

我编译怎么没问题???用的codeblocks
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 06:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表