quiet小朋友 发表于 2012-4-12 15:51:16

难解的问题

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

一代码:
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp;
fp = fopen("noexist","rb");
fprintf(fp,"aa");
if(fp==NULL) return;
fclose(fp);
}编译成功,可加了以下语句就不行:
#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);
}编译器提示:
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

Lei 发表于 2012-4-12 16:53:29

#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语言不允许随时定义变量,所有定义的变量都只能放在函数开头,这也是C和C++的重要区别。

quiet小朋友 发表于 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);

}
可这也不行

quiet小朋友 发表于 2012-4-12 16:57:50

写错写错:dizzy::dizzy::dizzy::dizzy:

quiet小朋友 发表于 2012-4-12 17:01:25

三克油,:lol:lol:lol

quiet小朋友 发表于 2012-4-12 17:05:09

Lei 发表于 2012-4-12 16:53 static/image/common/back.gif
#include
#include
//FILE *fp;


谢谢lei老师!

Lei 发表于 2012-4-12 17:08:41

quiet小朋友 发表于 2012-4-12 17:05 static/image/common/back.gif
谢谢lei老师!

不用谢,我才学C两年。不算老师。

芊芊 发表于 2012-4-15 09:33:20

回帖 就奖励~~

小小小菜菜菜 发表于 2018-12-20 15:03:50

{:10_277:}

1809228982 发表于 2018-12-21 11:48:50

我编译怎么没问题???用的codeblocks
页: [1]
查看完整版本: 难解的问题