89

I'm a little confused by "C standard lib" and "C POSIX lib", because I found that, many header files defined in "C POSIX lib" are also part of "C standard lib".

So, I assume that, "C standard lib" is a lib defined by ANSI C organization, and there are different implementation on different platforms (Win32/Unix-like), and "C POSIX lib" is just a implementation for "C standard lib" on Unix-like OSes, right?

But "C POSIX lib" contains some headers not specified in "C standard lib", such as <sys/types.h>, <sys/wait.h>, and <pthread.h>.

Take <pthread.h> as an example, I presume its "C standard lib" counterpart is <threads.h>, then if I want to write a multi-threaded program on Linux, which header file should I include, <pthread.h> or <threads.h>?

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Alcott
  • 17,905
  • 32
  • 116
  • 173
  • 11
    ANSI C has been obsolete since 1990, when C became an international standard. Since then, C is maintained by an [ISO working group](http://www.open-std.org/jtc1/sc22/wg14/). – Lundin Feb 21 '12 at 12:12
  • 7
    @Lundin Totally nonsense. ANSI C is nothing but obsolete. In fact C11 was adopted by INCITS as an ANSI standard known as [INCITS/ISO/IEC 9899-2012](http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+9899-2012) which is the new ANSI C standard. – Wiz Dec 14 '13 at 09:31
  • 2
    @Wiz It is an international standard and this is an international site. With that in mind, it has not made any sense to speak about ANSI C since 1990. It is the same as if I would start referring to C on this site as "SS C", since I'm from Sweden and the Swedish standard is called SS/ISO/IEC 9899. – Lundin Dec 16 '13 at 07:38
  • 2
    @Lundin While people consistently think that 'ANSI C' means C89 doesn't make it true; especially when ANSI C adopted C11 in 2012; thereby making 'ANSI C' mean C11. No matter how much people might think it's not. – Wiz Dec 16 '13 at 09:09
  • 11
    @Wiz You are missing the point. For the international audience, it only made sense to speak of ANSI C from the year 1989 to the year 1990, when the ISO standard was released. Thus informally C89 and ANSI C have become synonymous, even though ISO standards are still adopted by ANSI today. But since 1990, people outside the US couldn't care less about what the American National Standard Institute is up to. – Lundin Dec 16 '13 at 09:21
  • As for what relevance this has to the specific question: the question incorrectly states that ANSI is defining the C language. – Lundin Dec 16 '13 at 09:24
  • @Lundin Actually he’s not. He very much is correct. And yes, ANSI _does_ define C. It is completely applicable. (At the moment as much as ISO C) So, as long as you don’t mean C18, it’s fully applicable. But the latest version is defined by ISO. – jgh fun-run Feb 25 '20 at 21:50
  • @jghfun-run Right. So would you care to tell me what exactly is "ANSI"? Americans should only care about INCITS/ISO/IEC 9899:2018. – Lundin Feb 26 '20 at 08:01

3 Answers3

69

POSIX is a superset of the standard C library, and it's important to note that it defers to it. If C and POSIX is ever in conflict, C wins.

Sockets, file descriptors, shared memory etc. are all part of POSIX, but do not exist in the C library.

pthread.h is used for POSIX threads and threads.h is a new header for C11 and is part of the C library. Perhaps pthreads will be deprecated sometime in the future in favor of the C ones, however you probably can't count on C11 to have widespread deployment yet. Therefore if you want portability you should prefer pthreads for now. If portability is not a concern, and you have C11 threads available, you should probably use those.

Per Johansson
  • 6,697
  • 27
  • 34
  • For example, signal.h is both mentioned in http://en.wikipedia.org/wiki/C_standard_library and in http://en.wikipedia.org/wiki/C_POSIX_library. But I still do not understand. Is signal.h a header of standard C library, or is it only available in POSIX systems? – Koray Tugay Jun 04 '15 at 19:34
  • 2
    @KorayTugay the best way to figure it out is IMO to go to the [POSIX webpage](http://pubs.opengroup.org/onlinepubs/9699919799) and search for [signal.h](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html). It will note that the header exists in the C standard library but that POSIX extends to it. Extensions are marked with `CX`. It says you have to add a define, but usually the extensions are enabled by the default compiler flags. Another option is to read the [C standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). – Per Johansson Jun 05 '15 at 08:48
  • I don't think pthreads are going to be deprecated in favour of C11 threads, especially since C11 threads are (except for one unimportant detail) a proper subset of pthreads. – fuz Oct 04 '16 at 17:17
  • @Alcott - To add to @Per Johansson great answer, as far as I understand, one can "take into consideration", that the C standard "assumes minimal" OS capabilities, thus, for example, assumes only a single process is running on the system, thus there is "no need" (no definition) for headers such as `, ` and so on (yet, I should note that later versions of C, as far as I know, C11, does contain `` definition). – Guy Avraham Aug 22 '18 at 10:44
  • 1
    @PerJohansson what do you call the part of POSIX C library which isn't the standard C library? – Tim Sep 03 '18 at 22:27
  • @Tim I guess that is what this answer states by saying "superset", though "superset" is a strong word, I would say POSIX C library is basically an extension to the C standard library, to have more functionalities to control low-level stuffs like threads, processes, file systems, I/O etc. – Sourabh Choure Jun 07 '21 at 18:47
8

POSIX 7 quote

http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap01.html#tag_14_01

1.1 Relationship to Other Formal Standards

Great care has been taken to ensure that this volume of POSIX.1-2008 is fully aligned with the following standards:

ISO C (1999) ISO/IEC 9899:1999, Programming Languages - C, including ISO/IEC 9899:1999/Cor.1:2001(E), ISO/IEC 9899:1999/Cor.2:2004(E), and ISO/IEC 9899:1999/Cor.3.

Parts of the ISO/IEC 9899:1999 standard (hereinafter referred to as the ISO C standard) are referenced to describe requirements also mandated by this volume of POSIX.1-2008. Some functions and headers included within this volume of POSIX.1-2008 have a version in the ISO C standard; in this case CX markings are added as appropriate to show where the ISO C standard has been extended (see Codes). Any conflict between this volume of POSIX.1-2008 and the ISO C standard is unintentional.

I have listed some major API extensions at: I never really understood: what is POSIX?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

ANSI C is still alive, I think: ANSI C is inherited and extended by ISO C, Cxx. POSIX have been obeying ANSI C absolutely."

We can write ANSI C on Windows, Unix-Like, embedded device easily; but Cxx, or POSIX may have issue.