2

I double-checked all the similar questions here, I really hope not to duplicate. I am intrigued by the the following extract from stdio.h:

typedef struct _iobuf {
    /* Members here omitted ... */
} FILE;
extern FILE _iob[20];

We define a new type named FILE by using the struct syntax. Just after that, we declare an extern variable _iob of type FILE. Therefore, this variable must come from another place. But, since the definition of FILE is in this file, how can it be possible? I see two possibilities only. The first is to have something like:

#include <stdio.h>
FILE _iob[20] = /* definition */

The second is to have another source file where the struct is copy-pasted and the variable declared, I guess a very bad practice that we can omit.

Since I am a self-learner, I would be very happy to receive a confirm of my understanding.Thanks!

6
  • 1
    The typedef defines the layout of the data type FILE. The extern says that the code has one of those at its disposal, but its location is defined elsewhere. Commented May 11, 2021 at 16:43
  • Yes, I follow what you mean. But somehow it sounded "strange" to say something like: "we define a type, only here, and then a variable of this type that can comes from another source". I mean, how can that source know the type definition? I am glad I asked, since it helped a lot in clarifying some C syntax. Have a nice day. Commented May 11, 2021 at 17:12
  • The typedef can be repeated elsewhere without issues. Yes, it's true that it's normal to have the definition in one header file only and include that everywhere, but it's not a requirement. Commented May 11, 2021 at 17:16
  • But wouldn't be the repetition of 'typedef' a very bad practice? Concerning the remaining comment, thanks, crystal clear. Commented May 11, 2021 at 17:19
  • Yes, it's never a good idea to have multiple definitions of the same thing. Commented May 11, 2021 at 17:33

1 Answer 1

2

Either:

  • somewhere in the library source code files, there is text as you describe, an inclusion of <stdio.h> followed by a definition of FILE _iob[20], or
  • somewhere in the library source code files, there is a definition of _iob written in a programming language other than standard C.

The latter may be assembly language, non-standard C with extensions, or something else. An implementation of C is not required to use only C source code to define itself.

In general, it is good practice for the source file that defines an object to also include its own header. A primary purpose of a header file is to tell other source files about things defined in the associated source file. But including the header file in the source file serves another purpose: The compiler will see both the declarations in the header and the definitions in the source file and will issue error messages if they are incompatible. So this provides a check that catches typos or other errors when building the library.

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic explanation: thank you so much! Ps: it seems that I cannot upvote you answer since I have not enough reputation. I promise I'll do in the future, when possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.