hey abt the one and only printf!!

M

maadhuu

hey,
i want to know the exact implementation of printf function...what i do
know is that the arguments are pushed into a stack from right to left and
the function is called and then some pointer is set up ....well, where and
how ????
also it will be helpful if u can enlightten me on how scanf works and what
should be the output of
scanf("%d %d",&a,"%f",&b,&c); a,b are ints , c is a float.
 
M

marbac

maadhuu said:
hey,
i want to know the exact implementation of printf function...what i do
know is that the arguments are pushed into a stack from right to left and
the function is called and then some pointer is set up ....well, where and
how ????
also it will be helpful if u can enlightten me on how scanf works and what
should be the output of
scanf("%d %d",&a,"%f",&b,&c); a,b are ints , c is a float.

An example howto implement a simple printf:

http://www.and.org/texts/simple_printf.c.html
 
K

Keith Thompson

maadhuu said:
i want to know the exact implementation of printf function...what i do
know is that the arguments are pushed into a stack from right to left and
the function is called and then some pointer is set up ....well, where and
how ????

All that is implementation-specific. Different implementations
(compilers and run-time libaries) will do things differently. As far
as the language is concerned, all you know, and all you really need to
know, is that it works.

If you want to know how one particular implementation does this, you
can generate an assembly listing, but whatever you learn from this
won't necessarily be generally applicable. (I'm not trying to
discourage you from looking into this, but implementation-specific
details are generally considered off-topic here.)
also it will be helpful if u can enlightten me on how scanf works and what
should be the output of
scanf("%d %d",&a,"%f",&b,&c); a,b are ints , c is a float.

Undefined behavior. The first argument to scanf() is the format
string, which controls the handling of the other arguments. You're
trying to give it a format string, a pointer to an int, *another*
format string, a pointer to an int, and a pointer to a float. Since
the expected types of the remaining arguments are determined by the
format string, not by the declaration of scanf(), a compiler might not
be able to diagnose the error; it's up to you to make sure the
arguments are correct.

You probably want something like this:

scanf("%d %d %f", &a, &b, &c);

Finally, some friendly advice. If you're going to post here, it's
best not to use abbreviations like "u" for "you". Proper
capitalization is also a good thing.
 
M

Malcolm

maadhuu said:
i want to know the exact implementation of printf function...what i do
know is that the arguments are pushed into a stack from right to left and
the function is called and then some pointer is set up ....well, where and
how ????
also it will be helpful if u can enlightten me on how scanf works and what
should be the output of
scanf("%d %d",&a,"%f",&b,&c); a,b are ints , c is a float.
C allows variadic functions (functions that take a variable number of
arguments). The facility isn't very much used, with the notable exception of
formatted IO for the printf/scanf family of functions.

If you want to write a variadic function in C, you declare the parmameter
list with a ...

eg

int maximum(int N, ...)

You then use the va_start, va_end, and va_arg macros to access the variable
list.

How does the compiler implement this? The answer varies depending on
platform, but typically arguments are passed on a stack, which also contains
the return address for the the calling function and local variables. So all
the macros do is provide memory indexes into the stack, and then extract the
data.
 
M

maadhuu

hey Mr Keith, thanx for this, and i shall pay heed to what u said.....i am
new to this groups, so if i offended u or something like that, sorry.
 
C

CBFalconer

maadhuu said:
hey Mr Keith, thanx for this, and i shall pay heed to what u said.....i am
new to this groups, so if i offended u or something like that, sorry.

Then why do you continue to use the stupid (and offensive)
abbreviation 'u', fail to use an uppercase 'I', fail to quote the
relavent context from whatever you are replying to, etc.?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top