FILE undeclared - but its not, according to me.

W

W. Van Hooste

Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h"
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c
gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1


Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

Greetz..
 
M

Materialised

W. Van Hooste said:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h"
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c
gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1


Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

Greetz..

Change it so the command....
$cat source1.c

reads............


#include <stdio.h>
#include "isource2.h"
extern int getval(void);

main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}
 
M

Martin Dickopp

Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project: [...]
$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

The type `FILE' is declared in the standard header <stdio.h>. Since you
don't include that header in `isource2.c', `FILE' is in fact undeclared.

Martin
 
M

Materialised

Materialised said:
W. Van Hooste said:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h"
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1


Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????

Greetz..


Change it so the command....
$cat source1.c

reads............


#include <stdio.h>
#include "isource2.h"
extern int getval(void);

main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}
And also include stdio.h in the 2nd source file.
 
A

Artie Gold

W. Van Hooste said:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h"
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c

#include said:
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c
gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1


Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????
See above.
If you don't #include <stdio.h>, the type `FILE' is, indeed,
undeclared. Remember, a declaration has to be visible in each
translation unit in which it is referenced.

HTH,
--ag
 
M

Martin Dickopp

Materialised said:
W. Van Hooste said:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
Here is my Example project:
$ls
Makefile file1 isource2.c isource2.h source1.c
$cat source1.c
#include <stdio.h>
#include "isource2.h"
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}
$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};
$cat isource2.h
int getval(void);
$cat file1
50
$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c
$make
gcc -g -c source1.c gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1
Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????
Greetz..

Change it so the command....
$cat source1.c

reads............


#include <stdio.h>
#include "isource2.h"
extern int getval(void);

main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

Not only does this not solve the OP's problem, but it is also completely
unnecessary to declare `getval' twice.

Martin
 
J

Jeff

W. Van Hooste said:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project:

$ls
Makefile file1 isource2.c isource2.h source1.c

$cat source1.c
#include <stdio.h>
#include "isource2.h"
main()
{
int val;
val = getval();
printf("in main: %d\n", val);
}

$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

$cat isource2.h
int getval(void);

$cat file1
50

$cat Makefile
testtool: source1.o isource2.o
gcc -o source1 source1.o isource2.o
source1.o: source1.c isource2.h
gcc -g -c source1.c
isource2.o: isource2.c
gcc -g -c isource2.c

$make
gcc -g -c source1.c
gcc -g -c isource2.c
isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)
isource2.c:5: (Each undeclared identifier is reported only once
isource2.c:5: for each function it appears in.)
isource2.c:5: `fp' undeclared (first use in this function)
make: *** [isource2.o] Error 1


Don't understand, please explain - this should work, according to me
it's straigt forward! Isn't it????


Try to remove "#include <stdio.h>" in source1.c, add it to your header
file, and include your header file in the two source files.
 
W

W. Van Hooste

Martin Dickopp said:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE *fp! and
still get...

isource2.c: In function `getval':
isource2.c:5: `FILE' undeclared (first use in this function)

Here is my Example project: [...]
$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};

The type `FILE' is declared in the standard header <stdio.h>. Since you
don't include that header in `isource2.c', `FILE' is in fact undeclared.

Martin

You nailed it. I did not expect i needed to include stdio.h, and
assumed that this one ALWAY was loaded by default - not! I was not
even aware that i should include it more than once and thought that 1
include of stdio.h in the main source was more than enough. Man, was i
mistaken - but i absorb and learn ;-)...

Thanks,
W. Van Hooste.
 
G

goose

Materialised said:
Materialised wrote:

And also include stdio.h in the 2nd source file.

rather sadly, this doesn't fullfill the OP's request for
an explanation.

goose,
why "extern" ?
 
D

Dave Thompson

On 15 Sep 2003 17:20:38 -0700, (e-mail address removed) (W. Van Hooste)
wrote:
$cat isource2.c
int
getval(void)
{
FILE *fp;
int val;
fp = fopen("file1", "r");
fscanf(fp, "%u", &val);
printf("in getval: %d\n", val);
fclose(fp);
return (int) (val);
};
As others have said, you need to #include <stdio.h> in the source file
(translation unit) where you use FILE. And also where you use any of
fopen, fscanf, printf and fclose, although your compiler likely won't
give an error and perhaps not even a warning in these cases, and may
even generate code which works, but isn't required to and you can't
rely on it. The same is true for most other stdio routines; a handful
like getchar and putchar happen to work with the implicit declaration
in C90 only or a declaration you can write yourself without library
types, but it's not worth the trouble, just use <stdio.h> always.

You should check that the returned FILE* from fopen is non-null (!=
NULL or != 0) before using it, and should check the return value from
*scanf to make sure input was successful. In principle you should
also check the return value from fclose, although there are very few
ways for closing a read-only file to fail and you can't do anything
about them anyway; and could check the return from *printf but it's
hard(er) and especially for stdout you can't do anything useful.

*scanf %u is technically wrong for signed int, you should use %d,
although in practice this will almost certainly work.

There's no need to cast the return value to int; it already *is* int.
And the return statement will do the same (implicit) conversions as
assignment anyway.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top