473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

printf argument

Hello,

Supposed sizeof(short) == 2 and sizeof(int) == 4 and argument passing in
a stack.

When doing:

int i1; int i2;
printf("%d %d", i1, i2); // OK. 2 arg of size 4

int i; short s;
printf("%d %d", i, s); // 1 arg of size 2 and one of 4
This works but why ?

How printf can correctly unwind the arguments from the stack ? That is,
how can it knows it has to get 4 bytes for the first arg and 2 for the
second.

I thought it was based on the format but both are '%d' in this case ?

thanks,
john

Mar 6 '06 #1
2 4553
john <jo*******@nospam.teleme.com> writes:
Supposed sizeof(short) == 2 and sizeof(int) == 4 and argument passing
in a stack.

When doing:

int i1; int i2;
printf("%d %d", i1, i2); // OK. 2 arg of size 4

int i; short s;
printf("%d %d", i, s); // 1 arg of size 2 and one of 4
This works but why ?

How printf can correctly unwind the arguments from the stack ? That
is, how can it knows it has to get 4 bytes for the first arg and 2 for
the second.

I thought it was based on the format but both are '%d' in this case ?


Both arguments are passed as int (which is why printf doesn't have a
format for short). Arguments to printf (after the format string)
undergo the "default argument promotions"; types shorter than int are
promoted to int or unsigned int, and float is promoted to double.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 6 '06 #2
john <jo*******@nospam.teleme.com> wrote:
Supposed sizeof(short) == 2 and sizeof(int) == 4 and argument passing in
a stack.

When doing:

int i1; int i2;
printf("%d %d", i1, i2); // OK. 2 arg of size 4
Yes (assuming your suppositions), but...
int i; short s;
printf("%d %d", i, s); // 1 arg of size 2 and one of 4
....no.
This works but why ?

How printf can correctly unwind the arguments from the stack ? That is,
how can it knows it has to get 4 bytes for the first arg and 2 for the
second.


It doesn't. For variable argument lists, any of the variable arguments
which is narrower than an int, for integer types, or a double, for FP
types, gets widened to int (unsigned where appropriate) resp. double
before being passed to the function. printf() gets an int.
This rule also applies to functions without a prototype in place (i.e.,
with an old-style declaration, or with no declaration), but in genereal
you shouldn't write any functions without a prototype, these days.

Richard
Mar 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sam Wilson [Bentley] | last post by:
If you pass a C++ object by value as an argument to a function which has a variable-length argument list (...), the MSVC7 C++ compiler does not call the object's copy constructor and will not...
8
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
11
by: rahul8143 | last post by:
Does printf output depends on compilers or operating systems? I have one program int main() { int i=5; printf("%d %d %d",i++,i++,i++); return 0; } Output of this program on DOS (TC) 7 6 5...
4
by: pai | last post by:
Hi , Can any one tell me how this statement of printf is behaving . how the last digit is printed int a=2,b=4,c=7; printf("%d",printf("%d %d:",a,b)); //answer to this was 2 4:3
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
34
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
7
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
11
by: Googy | last post by:
Hi friends!! As we know that the input parameters in a function is fixed when the function is defined but how does printf processes variable number of input arguments ? For example: 1....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.