C question: assignment statement (output) URGENT but probably not too hard

Hi,
Can someone explain to me how I would figure out  what the output of this program segment to be:

{int i= 1
      j= 2
      if (i=j) {printf ("true: %d %d\n", i,j;}
          else printf ("false: %d %d\n", i,j;}

Also can you explain or give hints on what  %d %d means?

Thanks so much.

Angela
collegegirlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
%d is placeholder for integer variable. printf function sets i and j variables instead of %d placeholders. See details in printf function specification.
Code fragment has number of errors, it should look like this:

int i= 1;
j= 2;

if (i==j)
{
    printf ("true: %d %d\n", i,j);
}
else
{
      else printf ("false: %d %d\n", i,j);
}

In this case it prints false. If you write if ( i = j ), program prints false, because it is executed by such way:

if ( i = j ) ...
is egual to
i = j; if ( i != 0 )

0
AlexFMCommented:
Correction:
int i= 1, j= 2;
0
imladrisCommented:
No; assuming the syntax errors are corrected

if(i=j)

will evaluate to true. "i=j" evaluates to 2 (this is what enables you to write things like "b=i=j;"). And since 2 is non-zero it will be true.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

imladrisCommented:
Even, in retrospect, with Alex's concept:

i = j; if ( i != 0 )

it will evaluate to true, since i will be 2, which *is* not equal to 0.
0
AlexFMCommented:
Right, it will print true in any case. Side effect is that in the end of this code fragment i is equal to 2.
0
collegegirlAuthor Commented:
Hi,
Can you explain how you get the output to equal true. I had it equally false. How do you get the i to ever equal 2?

Thanks so much for the help so far. I really appreciate it :)

Angela
0
AlexFMCommented:
I don't beleive you can get true in this code. Please show your running code - code fragment shown in your post is not compiled. We can explain how it works if we will see real code which can be executed. About using = instead of == in if statement:

int i= 1;
j= 2;

if ( i=j )
{
    printf ("true: %d %d\n", i,j);
}
else
{
      else printf ("false: %d %d\n", i,j);
}

Suppose you run this program. if ( i = j ) is executed by the following way (described shortly by imladris):

j value is written to i. Result of this operation is evaluated in if statement. If result is not equal to 0, this is evalueated as true. 0 is false. In this case result is 2 (new i value), it is evaluated as true, and true branch is executed. This code runs like:

i = j; if ( i != 0 )

Right syntax: if ( i == j ) is executed exactly as expected.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Theory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.