开发者

How to understand stdio.h are different on different operating systems

First of all, I am talking about UNIX-like systems.

I look at the definition of the "FILE" struct at Mac OS, Linux, Minix and K&R C book, they are all different.

In K&R C book, it is quite clear

typedef struct _iobuf{
    int cnt;
    char *ptr;
    char *base;
    int  flag;
    int  fd;
} FILE;

on Mac OS, it has more stuff inside the struct.

on Linux (3.0), it is

typedef _IO_FILE FILE;

The header says "Define ISO C stdio on top of C++ iostreams." Em... ? ( C on Linux are implemented by C++ ? Shouldn't it be opposite?) Looks like _IO_FILE definition is in libio.h

on Minix, the definition is quite similar as K&R.

My unstanding was st开发者_Go百科dio.h should be part of C. The first C compiler is implemented by assembly language. And C should be independent of OS type.

machine code on HW -> asm -> C -> more complicated C -> UNIX

And now, there are different stdio.h on different OSs (all kinds of UNIX) and the compilers are all gcc.

How to understand this?

Thanks a lot, Alfred


The FILE struct is platform dependent and its fields are used internally by the C library. You shouldn't be relying on what's inside it.


Your own C code should not depend on OS. C headers and internal CRT implementation are OS-dependent. The meaning of cross-platform programming: write the code, compile it in different OS, and it should work. However, underlying tools (C, cross-platform libraries) are interacting with OS-specific API and they are different in different OS.

Of course, you should not use any fields of opaque structures, this breaks platform independent code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜