What can you do in C without "std"? Are they part of "C" or just libraries?

I apologize if this is a subjective or recurring question. It's awkward to look for, so I was not sure which conditions to include.

What I would like to know is that the basic tools / functions are based on C when you do not include standard libraries like stdio and stdlib .

What to do if there is no printf() , fopen() , etc.

Also, are these libraries technically part of the “C” language, or are they very useful and efficient core libraries?

+46
c standards
Apr 03 '10 at 23:09
source share
10 answers

Standard C says (5.1.2.3/5):

Least requirements for the corresponding implementation:

- At sequence points, unstable objects are stable in the sense that previous accesses are full and subsequent accesses have not yet occurred.

- At the end of the program, all the data written to the files should be identical to the result that the program execution is in accordance with the semantics abstract.

- Dynamics of entry and exit interactive devices as specified in 7.19.3.

Thus, without standard library functions, the only behavior guaranteed by the program is associated with the values ​​of mutable objects, since you cannot use any guaranteed access to files or “interactive devices”. Pure C provides interoperability only through standard library functions.

Pure C is not the whole story, though, since your equipment may have certain addresses that do certain things when reading or writing (whether it be a SATA or PCI bus, raw video memory, a serial port, something to make an audio signal or a blinking LED ) So, knowing something about your hardware, you can write a lot in C without using the standard library functions. Potentially, you could implement the standard C library, although this may require access to special CPU instructions as well as special memory addresses.

But in pure C, without extensions and standard library functions, you basically cannot do anything except read command line arguments, do some work, and return the status code from main . In order not to be sniffed, Turing is nevertheless fully subject to resource restrictions, although your only resource is automatic and static variables, without heap allocation. This is not a very saturated programming environment.

Standard libraries are part of the C language specification, but in any language, a line is usually drawn between the "as such" language and the libraries. This is a fundamental difference, but ultimately not very important in principle, because the standard says that they come together. Anyone who does something non-standard can just as easily remove language features as libraries. In any case, the result does not match the implementation of C.

Note that the “stand-alone” C implementation only for implementing a subset of the standard does not include any I / O, so you are in the position that I described above, relying on hardware extensions to get anything interesting done. If you want to distinguish between the "main language" and the "libraries" based on the standard, then this can be a good place to draw a line.

+29
Apr 03 '10 at 23:35
source share

What can you do? All!

There is no magic in C except possibly a preprocessor.

The most difficult thing is to write putchar - since it depends on the I / O platform.

This is a good workout to create your own version of varargs, and as soon as you get it, make your own version of vaprintf, then printf and sprintf.

I did all this on a Macintosh in 1986, when I was unhappy with the stdio routines that Lightpeed C provided, wrote my own window handler with win_putchar, win_printf, in_getchar and win_scanf.

This whole process is called bootstrapping, and it can be one of the most enjoyable examples in coding - working with a basic design that makes practical sense.

+14
Apr 03 '10 at 23:15
source share

Of course, you are not required to use standard libraries if you do not need them. Quite a lot of embedded systems either do not have standard library support, or cannot use it for one reason or another. The standard even specifically speaks of implementation without the support of the library, standard C99 5.1.2.1 "Free environment":

In a stand-alone environment (in which the execution of program C can take place without any benefit from the operating system), the name and type of function called when the program starts are determined in accordance with the implementation. Any library facilities available for a stand-alone program other than the minimum set required in accordance with Section 4 are implementation defined.

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

Without a standard library, you are completely dependent on your own code, any non-standard libraries that may be available to you, and any system calls of the operating system that you might interact with (which may be considered non-standard, standard library calls). It is possible you will have to have C program call assembly routines to interact with devices and / or any other operating system on the platform.

+8
Apr 04 2018-10-12T00:
source share

What to do if there is no printf (), fopen (), etc.

As long as you know how to interact with the system you are using, you can live without the standard C library. On embedded systems, where you only have a few kilobytes of memory, you probably don't want to use the standard library at all.

Here is the Hello World! example on Linux and Windows without using any standard C functions:

For example, on Linux, you can call Linux system calls directly in the built-in assembly:

 /* 64 bit linux. */ #define SYSCALL_EXIT 60 #define SYSCALL_WRITE 1 void sys_exit(int error_code) { asm volatile ( "syscall" : : "a"(SYSCALL_EXIT), "D"(error_code) : "rcx", "r11", "memory" ); } int sys_write(unsigned fd, const char *buf, unsigned count) { unsigned ret; asm volatile ( "syscall" : "=a"(ret) : "a"(SYSCALL_WRITE), "D"(fd), "S"(buf), "d"(count) : "rcx", "r11", "memory" ); return ret; } int _start() { const char hwText[] = "Hello world!\n"; sys_write(1, hwText, sizeof(hwText)); sys_exit(12); return 0; } 

Compile this with:

 gcc -nostdlib nostd.c 

And he displays Hello world! and comes out.

In Windows, system calls are not published, but are hidden behind another level of abstraction - kernel32.dll. It always loads when your program launches whether you want it or not. Thus, you can simply enable windows.h and use the Win32 API:

 #include <windows.h> int _start() { const char str[] = "Hello world!\n"; HANDLE stdout = GetStdHandle(STD_OUTPUT_HANDLE); DWORD written; WriteFile(stdout, str, sizeof(str), &written, NULL); ExitProcess(12); return 0; } 

windows.h has nothing to do with the standard C library, since you can also write Windows programs in any other language.

You can compile it using the MinGW tools, for example:

 gcc -nostdlib C:\Windows\System32\kernel32.dll nostdlib.c 

Then the compiler is smart enough to allow import dependencies and compile your program.

If you disassembled the program, you will see only your code, there is no standard library there.

So you can use C without a standard library.

+5
Mar 01 '17 at 16:28
source share

You can't do much, since most of the standard library functions depend on system calls; you are limited by what you can do with the built-in keywords and C statements. It also depends on the system; on some systems, you can manipulate bits in a way that leads to some external functions, but this is most likely an exception rather than a rule.

C elegance is simplicity. Unlike Fortran, which includes great functionality as part of the language, C is completely dependent on its library. This gives him a greater degree of flexibility, due to the fact that it is somewhat less coordinated from platform to platform.

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

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

+2
Apr 03 '10 at 23:14
source share

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

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

From a practical point of view, library functions are primarily designed to save you the trouble of rethinking the wheel. Some things (like string and library functions) are easier to implement. Other things (like I / O) are very dependent on the operating system. Writing your own version is possible for one O / S, but this will make the program less portable.

But you could write programs that do a lot of useful things (for example, calculate PI or the meaning of life or model automata). However, if you did not directly use the OS for I / O, it would be very difficult to observe what the output is.

In everyday programming, the success of a programming language usually requires a useful, high-quality standard library and libraries for many useful tasks. They may be third-party or third-party, but they must be there.

+2
Apr 03 '10 at 23:15
source share

The std libraries are "standard" libraries, since for a standard compiler C (such as C99), these libraries must be "include-able". For an interesting example that can help understand what this means, look at Jessica McKellar's call here:

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

+2
Apr 3 '10 at 23:20
source share

CRT is part of the C language just like keywords and syntax. If you use C, your compiler MUST provide an implementation of your target platform.

Edit: This is the same as STL for C ++. All languages ​​have a standard library. Maybe assembler as an exception, or some other languages ​​with a very low level. But most mid / high levels have standard libraries.

0
Apr 03 '10 at 23:23
source share

The C standard library is part of ANSI C89 / ISO C90. I recently worked on a library for a C compiler that was not previously compatible with ANSI.

The book C Standard Library by PJ Plauger was an excellent reference to this project. In addition to writing standard requirements, Plauger explains the history of each .h file and the reasons behind the design of the API. It also provides a complete library implementation, which helped me a lot when something in the standard was not clear.

The standard describes macros, types and functions for each of the 15 header files (including stdio.h, stdlib.h, as well as float.h, limits.h, math.h, locale.h, etc.).

The compiler cannot claim ANSI C status if it does not contain a standard library.

0
Apr 3 '10 at 23:39
source share

The assembly language has simple commands that move values ​​to the registers of the processor, memory, and other basic functions, as well as perform basic functions and machine calculations. C libraries are mostly pieces of assembly code. You can also use assembly code in your C. programs. Var - assembly code instruction. When you use 0x in front of a number to make it hex, this is an assembly instruction. Assembly code is a readable form of machine code that is a visual form of the actual states of circuit path switches.

Thus, while machine code, and therefore assembly code, is built into the machine, C languages ​​are combined from all kinds of pre-formed code combinations, including your own functions, which can be partially in the assembly language and partially call other functions in the assembler language or other C libraries. Thus, assembler code is the basis of all programming, and after that it all guesses what’s what. That is why there are so many languages ​​and so few true standards.

-2
Jan 16 2018-12-12T00:
source share



All Articles