Library Calls: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
No edit summary
m (Bot: Replace deprecated source tag with syntaxhighlight)
Line 20:
Let us have a look at the above code fragment, again. You could just as well write:
 
<sourcesyntaxhighlight lang="c">
int printf(const char * format, ...);
char * msg = "World";
printf("Hello %s!\n", msg);
</syntaxhighlight>
</source>
 
The header ''<stdio.h>'', in this case, does serve no other purpose than to tell the compiler that there is a function ''printf()'' that takes a parameter list consisting of at least a ''const char'' pointer, and returns ''int''. Nothing else is necessary, and mere convenience so you don't have to write your own declarations or ''#include'' each function individually.
Line 74:
So you provide a lightweight <tt>printf()</tt> for kernel space. You don't even have to bother to call it <tt>kprintf()</tt> or something as your kernel binary will never be '''linked''' with userspace code. Using some preprocessor ''#ifdef'' magic, you can even use the very same ''<stdio.h>'' header file as for userspace code, reducing redundancy and potential error sources: The preprocessor symbol <tt>__STDC_HOSTED__</tt> (with two leading and trailing underscores that the Wiki stubbornly interprets as boldface markup) is undefined when you set ''-ffreestanding''...
 
<sourcesyntaxhighlight lang="c">
#ifdef __STDC_HOSTED__
// Userspace declarations
Line 80:
// Kernelspace declarations
#endif
</syntaxhighlight>
</source>
 
[[Category:Standard Libraries]]