0

I'm now really diving into my OS project, called ForestOS, but now I'm needing to dive more into some simple and basic things of C. As now I'm having many problems with the correct variable to use and functions.

I want resources that only talk about variables, functions and how to develop without headers(stdio.h, math.h and all the others).

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
  • 11
    I'd suggest you start with "Hello world" before writing an operating system, personally. – Michael Myers Jan 20 '10 at 17:37
  • I've wrote many things, but in C++. THings like Compression Applications with a format that I've developed, A entire download application based on wget, also some games for Windows Mobile and Palm OS... Is this good for you before starting a OS? – Nathan Campos Jan 20 '10 at 17:42
  • 4
    That extensive knowledge of C++ does not match the "many problems with the correct variable to use and functions" comment. C++ has those too! – Clifford Jan 20 '10 at 19:49
  • I know! But I have many problems when using unsigned variables and pointers, even when on C++. – Nathan Campos Jan 20 '10 at 20:29
  • Duplicate of http://stackoverflow.com/questions/1345506/where-is-the-best-place-to-learn-c http://stackoverflow.com/questions/693210/best-book-to-learn-c-from-the-beginning-closed http://stackoverflow.com/questions/815712/whats-a-really-good-book-to-learn-c-closed http://stackoverflow.com/questions/803522/after-kr-what-book-to-use-to-learn-programming-in-plain-c http://stackoverflow.com/questions/257416/ and so on all taken from http://stackoverflow.com/search?q=[c]+book+learn which Nathan should have been more than capable of finding for himself. For shame! – dmckee --- ex-moderator kitten Jan 26 '10 at 16:08

5 Answers5

13

Best starting place is probably the book The C Programming Language.

The book was central to the development and popularization of the C programming language and is still widely read and used today.

Community
  • 1
  • 1
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
  • Cmon.. he still has to code his first "hello world"... shouldn't he at least follow a few free Internet tutorials before investing money in books? :) – littlegreen Jan 20 '10 at 17:47
  • 3
    @ littlegreen: He is attempting to write an OS; let's assume that he is past "hello world"! – Clifford Jan 20 '10 at 17:56
  • 2
    @Clifford I think your assumption may be a little off. Nathan has rather, let us say, "grandiose" ideas about his own programming abilities. –  Jan 20 '10 at 17:59
  • Of course! And I've already spent my money. I've bought this book since 1 minute and I'm going to spend more, this is the best project that I've already started! **:D** – Nathan Campos Jan 20 '10 at 18:02
  • 1
    If you're going to spend all that time working in C, buying one book is a reasonable investment, and that particular book will be very useful. – David Thornley Jan 20 '10 at 18:21
  • @Neil: I see that (and suggested it in my response), but by taking the question at face value, the answer may at least be of use to others even if it is useless to the OP. Also I see no reason not to recommend K&R over the available on line resources of varying quality. – Clifford Jan 20 '10 at 19:26
  • @Clifford Absolutely. K&R is my ideal of what a programming text book should be. –  Jan 20 '10 at 19:37
  • @littlegreen: the first exercise in K&R *is* "Hello, world!". – dmckee --- ex-moderator kitten Jan 26 '10 at 16:05
  • It seems that I have underestimated K&R :) maybe I should buy it too! Does it cover C++ as well? – littlegreen Jan 27 '10 at 00:23
  • K&R (first edition 1978) predates C++, which was started by Bjarne Stroustrup in 1979 – Richard Ev Jan 27 '10 at 09:22
4

A guide to OS development suggests CProgramming.com as the best place to start. There's tutorials, links to further resources, and everything for free.

littlegreen
  • 7,290
  • 9
  • 45
  • 51
3

Building an OS is non-trivial, I suggest if you are "having many problems with the correct variable to use and functions" then you may be attempting to walk before you can run!

Quote:

how to develop without headers(stdio.h, math.h and all the others).

I assume that you actually mean that you want to code without using the standard library rather than "without headers". Header files are intrinsic to modularisation in C; if you did not use headers, your code would have to be one monolithic module. Don't confuse headers with libraries.

However, even then there is no need not to use the standard library when writing 'bare-metal' code. You simply need a library that does not have OS dependencies, and you write the low level glue code to make things like stdio and memory allocation work on your system. Such a library is Newlib for example. It will make your life a whole lot easier if you have standard library support.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    +1, Newlib seems like a timesaver. Unless of course, you want to reinvent the *whole* wheel. Wheel 2.0. – littlegreen Jan 20 '10 at 18:18
  • @littlegreen: lol, Yeah I want to reinvent the wheel and make a Wheel 2.0, but I'm going to use Newlib as a base for learning. **;-)** – Nathan Campos Jan 20 '10 at 18:32
  • Is low-level C = C without headers? - Like low-level Assembly and HLA? – Nathan Campos Jan 20 '10 at 18:32
  • Essentially, C is a high-level language. Low-level would be writing assembler/processor code directly, which C cannot do. But there is a diff. between high- and low-level C, which is basically what Clifford describes: whether you link your program to libraries or not. Headers are irrelevant (although you usually include headers in order to describe functions in libraries, to which you subsequently link your program....) – littlegreen Jan 20 '10 at 19:04
  • @Nathan: The term "low level" was used to refer to the purpose of the code, not the language used or its complexity or even if it uses other headers - the answer to that is no, and that should have been clear from the first part of my answer. "Low level" in this context means the level of least abstraction, that interfaces directly with the hardware or perhaps BIOS services for example. It by no means implies that the code is either simple or primative. I have no idea what HLA means. – Clifford Jan 20 '10 at 19:37
2

You only need headers to provide declarations of functions and external variables.

It is possible to eliminate the header files and provide your declarations within the translation unit (a.k.a. source file). Although possible, this is not recommended.

Here is an example of a legal C program without header files:

/* Forward declaration of main(). */
int main(void);

/* Definition for main() function. */
int
main(void)
{
  return 13; /* 42 is such an overrated number. */
}

Some reasons for using header files are: code / typing reduction and single point of maintenance. If two modules need the same structure declaration, placing it in a header file will reduce typing (you only have to #include it in both files instead of copying it into both files). Also, if you need to change any declaration, if it is copied, you'll have to hunt down all copies and change every instance vs. making one change in a header file.

As far as standard header files, such as math.h and stdio.h, if you don't need them, don't include them. An OS should not require stdio.h, but may use math.h. Most standard header files do not contribute to the code size; only to the compile time.

I highly suggest you focus on the correctness of your OS and don't worry about trivialities such as header files. After your OS is working correctly and robust, go ahead and trim the fat.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Go through Kernighan C or the book named "Let Us C".It would help you learn much better as a beginner