By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

can i use + operator in Printf functions in C language?

P: n/a
hi friend,

is it possible to use the code like below
i=4,j=3,k=9;
printf("%d"+printf("%d%d",i,j,k));
if it works how it is possible.....here what is the role of + ?
plz help me
thanks

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
sabarish <su******@gmail.com> wrote:
is it possible to use the code like below
i=4,j=3,k=9;
printf("%d"+printf("%d%d",i,j,k));
if it works how it is possible.....here what is the role of + ?


printf() returns the number of characters transmitted (or a negative
value if there is an error.) Either way, a number.

"%d" is a character string, which devolves to a pointer to a character.

A pointer to a character plus a number results in a new pointer
that many places further advanced in the object.

As i and j are single digits, printf("%d%d", i, j) is going to
print two characters; the k argument will be ignored because there
is no corresponding format element. The return value will thus be the
number 2 for these particular values.

2 characters further along from the beginning of "%d" is going to be
a pointer to the \0 that terminates the %d string.

printf() with an empty string (that just has the terminator) will result
in no characters being printed.

So, the end result would be to print 43, ignore the k, and do nothing
for the outer printf().
--
Programming is what happens while you're busy making other plans.
Nov 15 '05 #2

P: n/a
On 12 Nov 2005 23:14:24 -0800, in comp.lang.c , "sabarish"
<su******@gmail.com> wrote:
hi friend,

is it possible to use the code like below
i=4,j=3,k=9;
printf("%d"+printf("%d%d",i,j,k));
if it works
In this specific case, by chance it works, because "%d"+2 = '\0' and
so nothing is printed by the outer printf.

If j had been 12, it would have been "%d"+3 which would point to
memory you don't own, and your programme might have crashed.
how it is possible.....here what is the role of + ?


It adds the numeric result of the rightmost printf onto the address of
the "%d", ie it causes the leftmost printf to print a string whose
address is x bytes fiurther along in memory. Chances are that this
memory doesn't belong to you.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #3

P: n/a
Mark McIntyre wrote:
On 12 Nov 2005 23:14:24 -0800, in comp.lang.c , "sabarish"
<su******@gmail.com> wrote:
hi friend,

is it possible to use the code like below
i=4,j=3,k=9;
printf("%d"+printf("%d%d",i,j,k));
if it works


In this specific case, by chance it works, because "%d"+2 = '\0' and
so nothing is printed by the outer printf.

If j had been 12, it would have been "%d"+3 which would point to
memory you don't own, and your programme might have crashed.


Another possibility is that stdout is closed, or has some other error,
in which case the first printf() will return 0, so the second one will
cause undefined behaviour because there isn't an argument to go
with the %d .

Nov 15 '05 #4

P: n/a
Old Wolf wrote:
Another possibility is that stdout is closed, or has some other error,
in which case the first printf() will return 0,


[#3] The printf function returns the number of characters
transmitted, or a negative value if an output or encoding
error occurred.

--
pete
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.