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

S

sabarish

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
 
W

Walter Roberson

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().
 
M

Mark McIntyre

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.
 
O

Old Wolf

Mark said:
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 .
 
P

pete

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.
 

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
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top