Jump to content

Using C without any include files?

Gat Pelsinger

I know you can use C without using any include files, but that needs inline insertion of assembly code. I could print something to the screen with directly inserting assembly instructions, if only I could code in assembly or if dumb ChatGPT wasn't that dumb (like seriously, I scolded it like 10 times but not a single runnable assembly code came out).

 

So, there are 2 things I want to ask. Can you code in C without including any files, and code in actual C language instead of using assembly?

 

And second, I wanted to dig down the implementation of printf and just copy its code to make a program that has just it's pure and direct implementation of the code it needs. I could find printf, but it uses a lot of other functions which use other functions and so on. ChatGPT gave me a code which only use 1 function that is imported from another library, and that is the write function, used to write something to the terminal. I could nowhere find the implementation of write. It seems like it is defined in io.h but I could never find an actual io.c source file or something. I want to see how the bare assembly is coded for those functions.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, Gat Pelsinger said:

So, there are 2 things I want to ask. Can you code in C without including any files, and code in actual C language instead of using assembly?

Headers have the declaration of the functions you use, if you add that declaration yourself and link to the proper library than it will compile without the need of include. But it depends on the function whether its defined in the header itself or if its using a custom data type that is defined in the current header or another one.

 

Otherwise I can only go as far as the main function returning 0 without doing anything.

Link to comment
Share on other sites

Link to post
Share on other sites

@Kamran2001 I had tried cipying the whole stdio.h code and not including the header and it works. But I want to see the bare assembly code or whatever which is at the most bottom of all this code that doesn't use another function or anything to work. And I also want to know if I can code in C without including ANY files and not using assembly at all.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

50 minutes ago, Gat Pelsinger said:

@Kamran2001 I had tried cipying the whole stdio.h code and not including the header and it works. But I want to see the bare assembly code or whatever which is at the most bottom of all this code that doesn't use another function or anything to work. And I also want to know if I can code in C without including ANY files and not using assembly at all.

The answer here is no. The C programming language provides no facility, other than assembly, to make system calls. So you'll never be able to do any file IO (including to the terminal) without at least a little assembly.

EDIT: As far as the assembly required to actually display something to the terminal, that will depend on your OS. But if you're running a POSIX system, you want to make a WRITE syscall to file descriptor 1. That would look something like this,

 

mov     $1, %rax                # set the syscall number -- 1 is write
mov     $1, %rdi                # set the file descriptor -- 1 is stdout
mov     <address here>, %rsi    # give it the pointer to the string you want to print
mov     <length>, %rdx          # give it the number of bytes to print
syscall                         # make the syscall

 

Edited by Flavius Heraclius
Added syscall example
Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Flavius Heraclius said:

Snip

Funny thing about linux os is that everything is a file from process to devices. You can technically manipulate another process memeory and issue command to devices like printers using just simple read/write syscalls(may need root access).

 

@Gat Pelsinger

I personally would not want to reinvent the wheel but if you are looking to like say reimplement libc, knock yourself out. Below is relevant to linux 

 

 https://linux-kernel-labs.github.io/refs/heads/master/lectures/syscalls.html

 

https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/

 

Do note that printf is buffered. Characters won't actually be printed out to stdout(file number/descriptor 1) until buffer is fill or until you invoke the fflush call so it is higher level than just the syscall to the kernel. 

 

 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

@Flavius Heraclius But can I not see the actual source code about the most base level C functions? I tried to look up but couldn't find any.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Gat Pelsinger said:

@Flavius Heraclius But can I not see the actual source code about the most base level C functions? I tried to look up but couldn't find any.

You can. Gnu libc aka glibc, true to its philosophy, is entirely open sourced. You can download the whole thing here. 

 

https://ftp.gnu.org/gnu/libc/

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×