Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C without stdio, what is possible?

I've been interested in programming an operating system for some time. Delving through a few different sites, I've come across an interesting concept (to paraphrase): if you start writing your bootloader with #include, you've already made a fatal mistake.

I've gone through K&R, and the entire book includes it throughout each lesson. Having used it throughout learning C, I don't know what I've learned that uses stdio and what doesn't. What can you do in C without stdio?

like image 255
Matticus Avatar asked Jun 30 '11 06:06

Matticus


3 Answers

The C standard (ISO/IEC 9899:1999) recognizes two types of implementation (§4 Conformance, ¶6):

  • Freestanding - where the implementation (compiler plus library) only provides seven headers:

    <float.h>
    <iso646.h>
    <limits.h>
    <stdarg.h>
    <stdbool.h>
    <stddef.h>
    <stdint.h>
    

    These provide basic facilities for the language and declare no functions (the facilities in <stdarg.h> are explicitly defined as macros in the standard). Note that it does not include complex numbers.

  • Hosted - where the implementation provides the complete library defined by the standard.

The whole point of a freestanding implementation is to allow you to write any code you need unencumbered by the standard library in general, and the standard I/O functions specifically. The downside is that you not only can but must provide those functions - or make use of alternatives provided by the implementation. Note that in a freestanding implementation, the entry point for the program need not be called main.

What you are seeking is a freestanding implementation - or to use the freestanding portion of a hosted implementation. You will use headers (unless you're insane), but they probably won't be the standard C library headers other than those listed for use with a freestanding implementation.

like image 107
Jonathan Leffler Avatar answered Sep 21 '22 17:09

Jonathan Leffler


Any bootloader will almost certainly have #include directives, unless it is poorly designed. I don't know where you got that quote, but perhaps you misunderstood it. A bootloader will invariably start with a piece of pure assembly code for the initial low-level processor initialization and to initialize the C runtime environment. This code is simply not possible to write in C. But the rest of the code can be as complex as needed, within the restrictions of the environment (memory, etc).

stdio is simply a set of file streams connected to some kind of device which can receive input and output. You can implement stdin, stdout, stderr or any subset of them. You also may implement a filesystem for which you can open arbitrary file streams. In a modern OS, this ends up connected to some kind of terminal typically, which involves quite a bit of layers in-between because it's a virtual terminal that shows up on your monitor and thus requires a graphics driver and so forth. On a primitive embedded system, stdio might be connected to a serial port, or an LED display.

stdio is implemented with the read() and write() functions. If these are not implemented, you simply can't use the stdio functions like printf(), fprintf(), fgets(), etc. That doesn't mean you can't write to your graphics display, serial port or whatever else. It just means you don't have a standard facility to do so and will need to call custom functions.

To answer your question: you can do anything in C without stdio. It's all written in C anyway, you're just losing some common functionality implemented in any C standard library.

like image 35
djs Avatar answered Sep 20 '22 17:09

djs


stdio stands for Standard Input/Output. As the name said, it contains things related to standard IO. The Wikipedia article of stdio.h lists the contents of stdio.h. You will need stdio.h if you use them. And you can check the man page of stdio.h too for further details.

And for the OS part, writing an OS is much more than simple programming, even if it is an academic one. Before jumping to that you should learn data structures, algorithms, OS theories and much more. The Design of the UNIX Operating System is a very good book to learn OS. Nachos is an academic OS simulation program. You can check that too. And if you are fanatic about OS, then you should read the autobiography of Linus Torvalds Just for Fun. Well, it's not a technical book, but you will get a feeling what it means writing an OS.

like image 21
taskinoor Avatar answered Sep 19 '22 17:09

taskinoor