Internals of the printf function?


 
Thread Tools Search this Thread
Top Forums Programming Internals of the printf function?
# 1  
Old 05-01-2010
Internals of the printf function?

hey all, im a new programmer.

i was wondering how you would go about writing the printf function yourself? it is my understanding that when you call printf you are calling an already written function and just providing an argument? if this is the case, is it possible to write that function yourself with just the bare primitives of the language? i only want to know because i am curious about how things like that work.

---------- Post updated 05-01-10 at 01:19 AM ---------- Previous update was 04-30-10 at 10:05 PM ----------

just to update, i've researched that the gcc compiler is written mostly in C, and so it's likely that the code for all these built-in c functions are written in C as well. so i take that to mean i could somehow write a printf-like function.

i suppose my best bet would be to look at the gcc source and just find it. however, not being a good programmer yet ( i only started just last week), i have no idea where i would find it Smilie. if anyone could point me in the right direction, or explain how i might write it, i would be very happy Smilie
# 2  
Old 05-01-2010
try here:
GNU C Library - GNU Project - Free Software Foundation (FSF)

Note that GNU source is a big project with lots of interdependent modules.

FYI - the UNIX kernel, most UNIX tools are written in C, the cc compiler is written in C, so is the C runtime (like printf).
# 3  
Old 05-03-2010
Se 'man stdarg' for info on how it handles varying numbers of arguments.
# 4  
Old 05-12-2010
About printf() itself ...

Ya why not you can definitely write your own versions. Now a small correction , its the stdio library under the glib which defines this printf() and not the gcc. The archieve file /usr/lib/libc.a contains an object (printf.o) inside its archive which has the exact function definition for the printf() function; which is normally linked statically into your program at the link time (again not by 'gcc' directly but the 'ld' itself).

The prototype of printf() looks like the following:

Code:
extern int printf (const char *__restrict __format, ...);

Its taking one const string and a variable number of arguments.

Use varg_ macros to built incremently your own version of printf() library.
Smilie

Last edited by Praveen_218; 05-14-2010 at 04:05 AM..
# 5  
Old 05-12-2010
Quote:
Now a small correction , its the stdio library under the glib which defines this printf() and not the gcc.
There is no stdio library under glib. In fact there is nothing called glib! There is a header called <stdio.h> which is defined by ISO C, IEEE 1003.1 by incorporation, SUS3 by incorporation, etc. Note that there is no requirement for <stdio.h> to actually be a file - hence the term "header" instead of "header file".
# 6  
Old 05-13-2010
Thanks for the correction ...

Quote:
Originally Posted by fpmurphy
There is no stdio library under glib. In fact there is nothing called glib! There is a header called <stdio.h> which is defined by ISO C, IEEE 1003.1 by incorporation, SUS3 by incorporation, etc. Note that there is no requirement for <stdio.h> to actually be a file - hence the term "header" instead of "header file".
Ya, stdio.h is just a header. However, as of glib reference, I expected that its the usual reference to the set of c-libraries available under /usr/lib/* . Please re-confirm me, if its not.

Its a fact that gcc/g++ is capable of attaching stdio.h headers, by default -if not specified and is no longer a requirement for a source code to explicitly add it. I, however, thought that its a feature implemented only under gcc and didn't know that its now a standard proposed.

Thanks.
# 7  
Old 05-13-2010
Quote:
Originally Posted by Praveen_218
Its a fact that gcc/g++ is capable of attaching stdio.h headers, by default -if not specified and is no longer a requirement for a source code to explicitly add it.
Unless this is an extremely recent feature, I think there's some confusion there, I've never heard of gcc including stdio.h by default anywhere. In fact I often need to add it to code where people didn't bother, since this causes not just compiler warnings but runtime segmentation faults on systems where int and void * are different sizes.

It definitely links stdio by default, but so do nearly all C linkers, and that's not the same thing as including the header.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

How to switch from SVR4/BSD internals to Linux internals?

Hello, Long-time Unix hacker here - I've worked on four variants of the kernel prior to the introduction of Linux. In my spare time, I've written Linux (Ubuntu) device drivers, kernel modules, cross-compiled, and built the kernel. I'd like to do Linux internals/device drivers as a day job,... (1 Reply)
Discussion started by: OriginalVersion
1 Replies

2. UNIX for Advanced & Expert Users

GDB Breakpoint Internals

When we put a breakpoint using gcc then what all things happen internally and how the gdb using break is able to pause the execution of process( instead of killing it ) and later on resume the process execution? (0 Replies)
Discussion started by: rupeshkp728
0 Replies

3. Programming

How access a specific memory portion through printf() function????

Hi friends, Hope everyone is doing well. Please have a look at this simple program, you will figure out what I want. #include <stdio.h> int main() { printf("Enter an integer!\n"); scanf( "%d", 134511890 ); // Valid address on my computer printf( "%d\n", ???? ); return 0; } ... (3 Replies)
Discussion started by: gabam
3 Replies

4. UNIX for Dummies Questions & Answers

printf function

#include<stdio.h> int counter; int fibonacci(int n) { counter += 1; if ( n <= 2 ) return 1; else return fibonacci(n-1) + fibonacci(n-2); } int main(void) { int i; int sum ; for( i = 1 ; i<= 10; i++) { counter = 0; sum... (1 Reply)
Discussion started by: vincent__tse
1 Replies

5. Programming

Need more info on internals of c compilers

Hello Gurus, i am ok with the concepts of c language but i would like to know more about the internals of c with respect to the compilers what happens when we say gcc filename.c the a.out will get created(what actaully compiler does to the code inaddition to generating object code) ... (5 Replies)
Discussion started by: MrUser
5 Replies

6. Linux

Kernel internals for ARM

Hi, Does anybody have a good pointer on Linux kernel internals for ARM architecture? I can locate plenty for x86 but since ARM is RISC I think there would be subtle changes. So if somebody has a knowledge of good document on Linux Kernel internals for ARM or even a comparative study of kernel on... (0 Replies)
Discussion started by: Rakesh Ranjan
0 Replies

7. Programming

why printf() function don't go work?

I use FreeBSD,and use signal,like follows: signal(SIGHUP,sig_hup); signal(SIGIO,sig_io); when I run call following code,it can run,but I find a puzzled question,it should print some information,such as printf("execute main()") will print execute main(),but in fact,printf fuction print... (2 Replies)
Discussion started by: konvalo
2 Replies

8. Shell Programming and Scripting

How to print a % within a printf() function using awk

Here is the code I'm using { printf("%11d %4.2f\% %4.2f\%\n", $1,$2,$3); } I want the output to look something like 1235415234 12.24% 52.46% Instead it looks something like 319203842 42.27\%4.2f\% How do I just print a "%" without awk or printf thinking I'm trying to do... (1 Reply)
Discussion started by: Awanka
1 Replies

9. HP-UX

HP-UX Internals Book

. (2 Replies)
Discussion started by: Driver
2 Replies

10. Filesystems, Disks and Memory

on unix internals

will anybody tell me how can i access all the fields of process table .if there is any structure and a system call please specify . (1 Reply)
Discussion started by: vish_shan
1 Replies
Login or Register to Ask a Question