What can you do in C without “std” includes? Are they part of “C,” or just libraries?

后端 未结 11 1002
孤独总比滥情好
孤独总比滥情好 2021-02-02 06:11

I apologize if this is a subjective or repeated question. It\'s sort of awkward to search for, so I wasn\'t sure what terms to include.

What I\'d like to know is what th

相关标签:
11条回答
  • 2021-02-02 06:26

    The Standard C Library is part of ANSI C89/ISO C90. I've recently been working on the library for a C compiler that previously was not ANSI-compliant.

    The book The Standard C Library by P.J. Plauger was a great reference for that project. In addition to spelling out the requirements of the standard, Plauger explains the history of each .h file and the reasons behind some of the API design. He also provides a full implementation of the library, something that helped me greatly when something in the standard wasn't clear.

    The standard describes the macros, types and functions for each of 15 header files (include stdio.h, stdlib.h, but also float.h, limits.h, math.h, locale.h and more).

    A compiler can't claim to be ANSI C unless it includes the standard library.

    0 讨论(0)
  • 2021-02-02 06:27

    The std libraries are "standard" libraries, in that for a C compiler to be compliant to a standard (e.g. C99), these libraries must be "include-able." For an interesting example that might help in understanding what this means, have a look at Jessica McKellar's challenge here:

    http://blog.ksplice.com/2010/03/libc-free-world/

    0 讨论(0)
  • 2021-02-02 06:32

    You can't do a lot, since most of the standard library functions rely on system calls; you are limited to what you can do with the built-in C keywords and operators. It also depends on the system; in some systems you may be able to manipulate bits in a way that results in some external functionality, but this is likely to be the exception rather than the rule.

    C's elegance is in it's simplicity, however. Unlike Fortran, which includes much functionality as part of the language, C is quite dependent on its library. This gives it a great degree of flexibility, at the expense of being somewhat less consistent from platform to platform.

    This works well, for example, in the operating system, where completely separate "libraries" are implemented, to provide similar functionality with an implementation inside the kernel itself.

    Some parts of the libraries are specified as part of ANSI C; they are part of the language, I suppose, but not at its core.

    0 讨论(0)
  • 2021-02-02 06:34

    None of them is part of the language keywords. However, all C distributions must include an implementation of these libraries. This ensures portability of many programs.

    First of all, you could theoretically implement all these functions yourself using a combination of C and assembly, so you could theoretically do anything.

    In practical terms, library functions are primarily meant to save you the work of reinventing the wheel. Some things (like string and library functions) are easier to implement. Other things (like I/O) very much depend on the operating system. Writing your own version would be possible for one O/S, but it is going to make the program less portable.

    But you could write programs that do a lot of useful things (e.g., calculate PI or the meaning of life, or simulate an automata). Unless you directly used the OS for I/O, however, it would be very hard to observe what the output is.

    In day to day programming, the success of a programming language typically necessitates the availability of a useful high-quality standard library and libraries for many useful tasks. These can be first-party or third-party, but they have to be there.

    0 讨论(0)
  • 2021-02-02 06:38

    Yes you can do a ton of stuff without libraries.

    The lifesaver is __asm__ in GCC. It is a keyword so yes you can.

    Mostly because every programming language is built on Assembly, and you can make system calls directly under some OSes.

    0 讨论(0)
  • 2021-02-02 06:40

    You're certainly not obligated to use the standard libraries if you have no need for them. Quite a few embedded systems either have no standard library support or can't use it for one reason or another. The standard even specifically talks about implementations with no library support, C99 standard 5.1.2.1 "Freestanding environment":

    In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

    The headers required by C99 to be available in a freestanding implemenation are <float.h>, <iso646.h>, <limits.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, and <stdint.h>. These headers define only types and macros so there's no need for a function library to support them.

    Without the standard library, you're entire reliant on your own code, any non-standard libraries that might be available to you, and any operating system system calls that you might be able to interface to (which might be considered non-standard library calls). Quite possibly you'd have to have your C program call assembly routines to interface to devices and/or whatever operating system might be on the platform.

    0 讨论(0)
提交回复
热议问题