Please explain the o/p

K

Kenny McCormack

Program:

#include <stdio.h>

int main(void)
{
int indiarocks = 2, j = 3;
char webeingood[80];

sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
j = 27;
puts(webeingood);
}

O/P is 5. Why?
 
R

Richard Heathfield

Kenny said:
Program:

#include <stdio.h>

int main(void)
{
int indiarocks = 2, j = 3;
char webeingood[80];

sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
j = 27;
puts(webeingood);
}

O/P is 5. Why?

At the sprintf, indiarocks + j evaluates to 2 + 3, which is 5, so the
webeingood array is used for storing the string "5".

The fact that you change j after this is utterly irrelevant. sprintf simply
builds a string; it doesn't create a dynamic binding between the string and
the objects whose values were used to build that string.
 
E

Eric Sosman

Kenny said:
Program:

#include <stdio.h>

int main(void)
{
int indiarocks = 2, j = 3;
char webeingood[80];

sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
j = 27;
puts(webeingood);
}

O/P is 5. Why?

Because you're using a C implementation that's broken.
The output should have been "indiarocks + j = 5\n"; since
that's not what you got, complain to the C vendor.

Oh, that *is* what you got? Well, why didn't you say
so? If you suffer a bad fall and fear you've broken your
right arm, do you tell the doctor it's your left great toe
that hurts? If you ever expect to get anywhere in writing,
reading, or using computer programs, Kenny, you need to
develop the ability to observe in detail and describe with
accuracy.

Now to your question -- the one you didn't quite ask, but
should have. The sprintf() call fills the initial portion
of webeingood[] with a string of characters determined by the
values of the sprintf() arguments at the time of the call.
At that time, indiarocks is equal to 2 and j is equal to 3,
so the third sprintf() argument is equal to 5 and sprintf()
places "indiarocks + j = 5" in webeingood[].

Subsequent changes to the value of j -- or of indiarocks,
for that matter -- don't change what's already been put into
webeingood[]. Like any other variable, webingood[] retains
the last value stored until you store a new one. (If you
write your weight on a slip of paper and then go on a three-
week feeding binge and gain twenty kilos, does the number
on the paper change automatically?) When you arrive at the
puts() call, webeingood[] still holds the string that sprintf()
deposited in it, and puts() outputs that string followed by a
newline.

Variables in C are not like "variables" in mathematics,
nor like "formulas" in spreadsheets. They're more like pieces
of paper: you can write things on them and read them back again,
you can erase what's written and write something new, but the
paper doesn't self-erase or self-rewrite.

(Pedantry warnings: Yes, I know about "volatile." Yes,
I know about "storage duration." Yes, I know Kenny should
return a value from main(), lest his implementation not yet
conform to C99. All such matters -- well, maybe except the
last -- are beyond Kenny's current knowledge, and anyone who
carps on them is doing him no favors.)
 
R

Richard Heathfield

Eric said:
If you ever expect to get anywhere in writing,
reading, or using computer programs, Kenny, you need to
develop the ability to observe in detail and describe with
accuracy.

I'm stuffed, then. I didn't notice that either.
 
J

Joe Wright

Kenny said:
Program:

#include <stdio.h>

int main(void)
{
int indiarocks = 2, j = 3;
char webeingood[80];

sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
j = 27;
puts(webeingood);
}

O/P is 5. Why?
Because 2 plus 3 equals 5. 'j = 27;' has no effect on webeingood.
 
E

Emmanuel Delahaye

Kenny McCormack wrote on 30/07/05 :
int indiarocks = 2, j = 3;
sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
O/P is 5. Why?

Because 'indiarocks + j' (that is 2 + 3) evaluates to 5. What's your
point exactly ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
J

James McIninch

2+3=5


<posted & mailed>

Kenny said:
Program:

#include <stdio.h>

int main(void)
{
int indiarocks = 2, j = 3;
char webeingood[80];

sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
j = 27;
puts(webeingood);
}

O/P is 5. Why?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top