printf(char*, ...)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
PlayOS

printf(char*, ...)

Post by PlayOS »

Hi,

Have finally started on my C Kernel, I am after a tutorial that specifically teaches the use of C in OS design. The reason I want this is because I am having doing stuff in C that I can already do in asm. In asm I am have gone all the way up to having paging enabled and working properly, GDT, IDT all setup properly. This was where I got to when I decided that it was time to start coding my kernel properly. I could write it all in asm, but this is not going to be easy and it would take 10 times as long as it will to do it in C, all I need is some fundamental info on using C for OS coding.

With printf() I am wondering how I deal with multiple unknown amounts of args in C.
Tom

Re:printf(char*, ...)

Post by Tom »

Look at my printf in FritzOS, it was just released, it might help some, because it's not done yet...
you should look at the string handler in my printf...it can handle infinite string printing ( infinite %s's like this:

printf( "%s", "%s %s", "hello", "the end" ); like that, but it' can't handle infinte chars or numbers )
Tim

Re:printf(char*, ...)

Post by Tim »

Tom: interesting idea -- but don't use your "infinite string" idea if you want to claim C compatibility. Otherwise, something like this would break:

Code: Select all

int main(void)
{
    char percent_s[10];

    /* This is a portable C program which will work under any OS */
    printf("Please type a percent sign and a letter: (e.g. %%s)\n");
    gets(percent_s);
    printf("You typed: %s\n", percent_s);
}
Output:
[tt]Please type a percent sign and a letter:
%s
You typed: shioshgldfhd
!!!ERROR!!!
Page fault
DEATH
BLUE SCEEEN!!!!!!"1111[/tt]
Tom

Re:printf(char*, ...)

Post by Tom »

I tested my code every way...I don't see a error in it, or I don't under stand what you mean.

If you look at my code, it doesn't depend on how big the string is...
Tom

Re:printf(char*, ...)

Post by Tom »

in my code if you want "%s" on the screen it's %%s
Tim

Re:printf(char*, ...)

Post by Tim »

My point is that everyone expects printf to be:

printf(string_containing_formatting_characters, some, strings, without, formatting, characters);

If you start putting formatting in the second parameter and beyond, you'll introduce unexpected behaviour. People don't expect these parameters to be treated specially.
Post Reply