473,226 Members | 1,528 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,226 software developers and data experts.

printf( )

int a=10;
printf("%d")
the output is 10

int a=10,b=20;
printf("%d ")
the output is 20
how did it return the last defined variable?
why does printf( ) works from right to left?

Jul 12 '06 #1
8 2122
sarma wrote:
int a=10;
printf("%d")
Undefined behaviour (expected argument value missing).
[Some compilers - eg gcc - can spot this.]
the output is 10
Sheer accident of implementation. (The assigned value
has probably leaked into the argument register/location
which printf looks at for the first %-inserted value.)
int a=10,b=20;
printf("%d ")

the output is 20
Sheer accident with same possible explanation.
how did it return the last defined variable?
Coincidence.
why does printf( ) works from right to left?
Why is the moon made of green fleas?

--
Chris "the mice ate the cheese" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Jul 12 '06 #2
sarma wrote:
int a=10;
printf("%d")
the output is 10

This is undefined behavior. Trying to define the behavior of undefined
behavior is pointless. There's nothing for you to figure out.

Learn the language properly, from the beginning. Random questions about
deliberately broken code will tell you little. It wastes your time and
ours.

Brian
Jul 12 '06 #3

int a,b,c;
printf ("%u %u %u \n",&a,&b,&c);
can you predict the result. Assume thet the first variable would be
stored at an address 65540

please don't overlook this as a coincidence or an accident

May be you could find an answer my previous question

Jul 13 '06 #4
"sarma" <sa********@gmail.comwrote:
int a,b,c;
printf ("%u %u %u \n",&a,&b,&c);
can you predict the result.
No. Nobody can.
Assume thet the first variable would be stored at an address 65540
That is an amazing assumption.
please don't overlook this as a coincidence or an accident
That sentence does does not seem to have a meaningful connection to the
rest of your post.

Richard
Jul 13 '06 #5
jjf

sarma wrote:
int a,b,c;
printf ("%u %u %u \n",&a,&b,&c);
can you predict the result.
No-one can. The behaviour's undefined since you're passing pointers to
int and telling printf() it's getting unsigned integers. Even if you
fix that, it can't be predicted in general. Someone who knows intimate
details of the compiler you are using, in the mode you are using it, on
the OS it is running under, on the processor it is running on may be
able to; but that's not topical here.
please don't overlook this as a coincidence or an accident
What do you mean?
May be you could find an answer my previous question
What do you mean? You got two full, complete, and correct answers to
your previous question.

Jul 13 '06 #6
On 12 Jul 2006 23:42:33 -0700, "sarma" <sa********@gmail.comwrote:
>
int a,b,c;
printf ("%u %u %u \n",&a,&b,&c);
can you predict the result. Assume thet the first variable would be
stored at an address 65540

please don't overlook this as a coincidence or an accident
What, exactly, are you doing? It looks like you're writing random code
just to see what happens. If that's the case, please stop wasting our
time.
>
May be you could find an answer my previous question
Your previous question was already answered.

--
Al Balmer
Sun City, AZ
Jul 13 '06 #7

sarma wrote:
int a,b,c;
printf ("%u %u %u \n",&a,&b,&c);
can you predict the result.
The behavior is undefined, therefore the result is unpredictable.

Had you written

printf("%p %p %p\n", (void*) &a, (void*) &b, (void*) &c);

then the behavior would no longer be undefined, but you would need to
specify exactly which compiler and architecture were being targeted,
since the result will vary between platforms.
Assume thet the first variable would be
stored at an address 65540

please don't overlook this as a coincidence or an accident

May be you could find an answer my previous question
Your previous question was answered. The answer was that you invoked
undefined behavior. Unless you're interested in mapping out exactly
what actions a specific compiler takes as a result of undefined
behavior (a fool's errand IMO since that can vary based on any number
of conditions, but whatever), that's as much of an answer as you need.

Jul 13 '06 #8
"John Bode" <jo*******@my-deja.comwrites:
sarma wrote:
>int a,b,c;
printf ("%u %u %u \n",&a,&b,&c);
can you predict the result.

The behavior is undefined, therefore the result is unpredictable.

Had you written

printf("%p %p %p\n", (void*) &a, (void*) &b, (void*) &c);

then the behavior would no longer be undefined, but you would need to
specify exactly which compiler and architecture were being targeted,
since the result will vary between platforms.
Knowing the compiler would only tell you how the pointer value is
converted to a sequence of characters; it won't tell you what the
specific values are. For example, the objects a, b, and c could be
allocated in any order, and may or may not be adjacent; the
implementation is under no obligation to document this.

--
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.
Jul 13 '06 #9

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

Similar topics

11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
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. */
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
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
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
19
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement,...
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' ?
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.