Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > can i use + operator in Printf functions in C language?

Reply
Thread Tools

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

 
 
sabarish
Guest
Posts: n/a
 
      11-13-2005
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

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      11-13-2005
In article <(E-Mail Removed) .com>,
sabarish <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      11-13-2005
On 12 Nov 2005 23:14:24 -0800, in comp.lang.c , "sabarish"
<(E-Mail Removed)> 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 =----
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      11-13-2005
Mark McIntyre wrote:

> On 12 Nov 2005 23:14:24 -0800, in comp.lang.c , "sabarish"
> <(E-Mail Removed)> 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 .

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-13-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
how can i make functions which behaves like printf Kaush C Programming 5 02-02-2004 10:54 AM



Advertisments