-3

I know that linux kernel source is in pure c. So I want to know how can I write simple Hello, World program in pure C without using printf api?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Milad Khajavi
  • 2,769
  • 9
  • 41
  • 66
  • 2
    This may come as a surprise, but `printf` is written using "pure" C as well. – JJJ Oct 07 '13 at 06:32
  • 1
    possible duplicate of [source code of c/c++ functions](http://stackoverflow.com/questions/1127328/source-code-of-c-c-functions) – JJJ Oct 07 '13 at 06:33
  • 1
    You can fall back to inline assembler and syscalls. –  Oct 07 '13 at 06:34
  • No, printf use write system call. I want to write hello, world without using any system call. – Milad Khajavi Oct 07 '13 at 06:34
  • @Khajavi Then grab a paper and a pencil, and write "Hello world" on the paper using the pencil. –  Oct 07 '13 at 06:35
  • @H2CO3 No, I want no use of system call, no use of any OS. writing hello world program. so when I boot my computer, the computer can run it. – Milad Khajavi Oct 07 '13 at 06:37
  • 3
    Well, it's a kernel and, handle directly hardware stuff. A kernel is very different from a user application. `write()` system call may be written in assembly using `int` in the right memory address and you have a message on computer screen... `and then you do a `printf()` call to it internally. If you implement this functions yourself, it will be "pure" according to you concept. But nowdays you can't do a DMA on modern systems(sa far I know). So, it is not possible really and the only way to go is do a sys call. – The Mask Oct 07 '13 at 06:40
  • 3
    @Khajavi: It implies to write an operating system. – The Mask Oct 07 '13 at 06:41
  • tons of misspelling ... I need to sleep – The Mask Oct 07 '13 at 06:46
  • @TheMask I found this http://www.acm.uiuc.edu/sigops/roll_your_own/1.helloworld.html seems very exciting practice. – Milad Khajavi Oct 07 '13 at 06:58

2 Answers2

5

I know that linux kernel source is in pure c.

It is most certainly not. The Linux kernal is infamous for the frequent use of non-standard extensions from the GCC compiler.

So I want to know how can I write simple Hello, World program in pure C without using printf api?

printf is just a wrapper around the OS API functions. Do you mean to ask how to write printf using only Linux API functions? Becase "pure C" is defined in the C standard ISO 9899, which has nothing to do with the Linux OS.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • No, I explained my idea to H2CO3. I want to write Hello, World that run on computer machine, without using any OS. When booting the os, the hello, world execute. – Milad Khajavi Oct 07 '13 at 06:41
  • 4
    @Khajavi Then you'll have to know the details of how that particular machine's output works. The point of an OS is to save you form having to deal with the specifics of the platform's hardware. – David Schwartz Oct 07 '13 at 06:42
  • 3
    @Khajavi Then you have to write everything from scratch, either in assembler or in a compiler port designed to generate code for the specific CPU. You cannot use a compiler port for a specific OS such as GCC for Linux. – Lundin Oct 07 '13 at 06:46
3

When running in a process in any operating system, you have no direct access to hardware resources. So there's no way to print anything without asking the OS to do it for you.

Instead of printf, you can use a lower level API, such as write.

You can go further and issue a system call yourself, using the right machine instruction (which is OS and architecture dependent). This way you won't use any library, but will still rely on the OS.

ugoren
  • 16,023
  • 3
  • 35
  • 65