POSIX C & ANSI C

本文介绍了ANSI C与POSIX C两种标准的区别。ANSI C关注于语言本身的定义,而POSIX C则在此基础上增加了针对多进程、线程等高级功能的支持,旨在提高程序在不同UNIX变种中的可移植性。

  • 以下部分转载自:http://blog.youkuaiyun.com/skylj0209/article/details/3071507
  • ANSI C

  • 这一标准是 ANSI(美国国家标准局)于 1989 年制定的 C 语言标准。 后来被 ISO(国际标准化组织)接受为标准,因此也称为 ISO C。
    ANSI C 的目标是为各种操作系统上的 C 程序提供可移植性保证,而不仅仅限于 UNIX。 该标准不仅定义了 C 编程语言的语发和语义,而且还定义了一个标准库。这个库可以根据 头文件划分为 15 个部分,其中包括:字符类型 (<ctype.h>)、错误码 (<errno.h>)、 浮点常数 (<float.h>)、数学常数 (<math.h>)、标准定义 (<stddef.h>)、 标准 I/O (<stdio.h>)、工具函数 (<stdlib.h>)、字符串操作 (<string.h>)、 时间和日期 (<time.h>)、可变参数表 (<stdarg.h>)、信号 (<signal.h>)、 非局部跳转 (<setjmp.h>)、本地信息 (<local.h>)、程序断言 (<assert.h>) 等等。

  • POSIX C

  • 该标准最初由 IEEE 开发的标准族,部分已经被 ISO 接受为国际标准。该标准的具体内容 见 1.1.3。POSIX.1 和 POSIX.2 分别定义了 POSIX 兼容操作系统的 C 语言系统接口 以及 shell 和工具标准。这两个标准是通常提到的标准。POSIX 表示可移植操作系统接口(Portable Operating System Interface ,缩写为 POSIX 是为了读音更 像 UNIX。电气和电子工程师协会(Institute of Electrical and Electronics Engineers,IEEE) 最初开发 POSIX 标准,是为了提高 UNIX 环境下应用程序的可移植性。然而,POSIX 并不局限于 UNIX。 许多其它的操作系统,例如 DEC OpenVMS 和 Microsoft Windows NT,都支持 POSIX 标准,尤其是 IEEE Std. 1003.1-1990(1995 年修订)或 POSIX.1,POSIX.1 提供了源代码级别的 C 语言应用编程 接口(API)给操作系统的服务程序,例如读写文件。POSIX.1 已经被国际标准化组织(International Standards Organization,ISO)所接受,被命名为 ISO/IEC 9945-1:1990 标准。
  • 以下转载自:http://www.thejaywalker.net/2010/12/difference-between-posix-and-standard-c.html

  • Difference Between POSIX and Standard C Library

    If everything you learn about Unix development using C/C++ is POSIX then what the hell does the standard C library do?

    First things first. POSIX stands for "Portable Operating System Interface (for UniX)." Now that's a fancy name but the requirement becomes very clear when you see the disparity amongst so many flavors of Unix. There was a need to unify the programming of these flavors of Unix (include Linux); POSIX provides this unified API. Though the standard is not tied to just C, there is no known full implementation of POSIX in any other environment.


    What does POSIX contain?


    A lot of things! For example, threads, semaphores, file system access API, etc.

    What's wrong with/ limiting in Standard C Library?


    Standard C library (i.e., ANSI/ ISO C) assumes a very minimalist operating system. For example, no standard C library functions exist to manage threads. Heck, there is no IPC (inter process communication); in other words, ISO C doesn't assume that the program would be running on a multi-processing operating system!

    Again, what exactly does POSIX add to the standard C library?


    To answer this, one must know which header files ISO/ ANSI C contains. There are 24 header files including the well known  <stdio.h><stdlib.h>, etc. 

    <assert.h><complex.h><ctype.h><errno.h><fenv.h>
    <float.h><inttypes.h><iso646.h><limits.h><locale.h>
    <math.h><setjmp.h><signal.h><stdarg.h><stdbool.h>
    <stddef.h><stdint.h><stdio.h><stdlib.h><string.h>
    <tgmath.h><time.h><whcar.h><wctype.h>


    As you can see, there is nothing like  <socket.h><thread.h><sharedmemory.h>, <graphics.h>, etc., since this kind of functionality is heavily dependent on the kind of environment the application would run in while C was designed to be a "portable" language.

    Not having this "advanced" functionality, C becomes severely limited and each operating environment is bound to introduce its own standard for defining multi-processing, interprocess communication, advanced memory management, etc. That's where POSIX (and other kinds of libraries such as graphics toolkit) kicks in.

    If you have programmed for Unix/ Linux, you know that there are several library functions beyond system calls which are not in the standard C list. This includes functions like  fork(), which is neither a system call nor part of the standard C library (remember C doesn't assume a multi-processing OS?). In fact, POSIX goes well beyond a programming API and defines the "environment" in which the application would run. For example, POSIX defines that there shall be a root directory "/" in the operating environment---no such definition is part of C standard library.

    The complete list of  POSIX header files should be referred as well. Below is a listing of the header files additional to ISO C:

    <aio.h><arpa/inet.h><assert.h><ctype.h>
    <dirent.h><dlfcn.h><fcntl.h><fmtmsg.h>
    <fnmatch.h><ftw.h><glob.h><grp.h>
    <iconv.h><langinfo.h><libgen.h><monetary.h>
    <mqueue.h><ndbm.h><net/if.h><netdb.h>
    <netinet/in.h><netinet/tcp.h><nl_types.h><poll.h>
    <pthread.h><pwd.h><regex.h><sched.h>
    <search.h><semaphore.h><spawn.h><strings.h>
    <stropts.h><sys/ipc.h><sys/mman.h><sys/msg.h>
    <sys/resource.h><sys/select.h><sys/sem.h><sys/shm.h>
    <sys/socket.h><sys/stat.h><sys/statvfs.h><sys/time.h>
    <sys/times.h><sys/types.h><sys/uio.h><sys/un.h>
    <sys/utsname.h><sys/wait.h><syslog.h><tar.h>
    <termios.h><trace.h><ulimit.h>
    <unistd.h><utime.h><utmpx.h><wordexp.h>


    Is POSIX a superset of standard C library?


    Yes! POSIX, for example, redefines all standard C header files (sometimes augmenting them with advanced functionality), and uses the following statement in its documentation:
    The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2008 defers to the ISO C standard.


    What's the use of standard C library, then?


    Nothing if you are programming for a POSIX system but not all systems are POSIX compliant. To ensure maximum portability---ranging from desktop machines to handheld devices running a C runtime---restrict yourself to the standard C library and you can fly quite a distance with that.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值