As the title states, I am getting the following error: "implicit declaration of function ‘fileno’" when I try to compile on Linux but not on a Mac. I know I can fix it with the simple line int fileno(FILE *stream);
, but I want to know why this happening.

- 51
- 1
- 2
-
Use strict warnings in both environments. I'd be surprised if you get the warning on missing prototype on only one of them then. – Yunnosch Jun 19 '17 at 05:30
-
1And where is the code which shows this problem? – Sourav Ghosh Jun 19 '17 at 05:32
-
1Related: [How do I remove the following 'implicit declaration of function' warnings?](https://stackoverflow.com/a/9427286/4520911) – iRove Jun 19 '17 at 05:32
-
2What compilation options are you using? `-std=c11` or `-std=c99`? Try `-std=gnu11` or `-std=gnu99` instead. – Jonathan Leffler Jun 19 '17 at 05:35
-
Sorry, but I don't know what ISO C and POSIX have to do with Linux and Mac or what they are. The line of code is this int f = fileno(outputFile);. I'm not looking for a fix, I already have one. What I'm trying to understand is why this is error is happening – helppls Jun 19 '17 at 05:38
-
4Officially, you have to request POSIX functions by having `#define _XOPEN_SOURCE 700` or `#define _POSIX_C_SOURCE 200809L` in effect before the first (POSIX) standard header is included. If you specify `-std=c11`, then those functions are not declared by defaul. – Jonathan Leffler Jun 19 '17 at 05:57
-
[As n.m. suggested](https://stackoverflow.com/a/44623177/4520911), try adding a `#define _POSIX_C_SOURCE 1` above `#include
` to explicitly state you are using POSIX. – iRove Jun 19 '17 at 06:04 -
"I don't know what ISO C and POSIX have to do with Linux and Mac or what they are" You probably want to learn these things if you plan to produce meaningful C code for these systems. – n. m. could be an AI Jun 19 '17 at 06:13
3 Answers
You should not declare functions provided by a system library. You should properly include an appropriate header.
Since fileno
is not a standard C function, it is not normally declared by in <stdio.h>
. You have to define a suitable macro in order to enable the declaration.
man fileno
Requirements for glibc (see feature_test_macros(7)):
fileno(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
Defining any of the the three macros before inclusion of should do the trick.

- 112,515
- 14
- 128
- 243
If you remembered to include <stdio.h>
, this warning indicates that your <stdio.h>
does not declare this function.
fileno
is not a standard function. It is perfectly expected that some implementations do not provide it, especially if you are compiling in strict standard-compliant mode.
If your code still links successfully, this almost certainly means that your Linux compiler settings direct your compiler to work in "strict" mode and thus force <stdio.h>
to "hide" declaration of fileno
.

- 312,472
- 42
- 525
- 765
I wrote a simple program in my Ubuntu 16.04
machine. And it compiles well.
#include <stdio.h>
int main ()
{
printf ("%d\n", fileno(stdout));
return 0;
}
I hope you know that fileno
is present in <stdio.h>
.
If you have not included the header file, please do so and re-compile.
I am not so sure why Mac version works.
EDIT:
I have not used C99 standard.
I compiled using: gcc -Wall -Werror test.c

- 574
- 7
- 24
-
-
1
-
If you don't use the -std=c99 flag, then there's no error because pre-99 C allowed functions without prototypes first and also had int as the default return value so it all works out. – SO Stinks Mar 31 '20 at 04:29