?
Solved

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

Posted on 2004-11-12
7
Medium Priority
?
194 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:collegegirl
  • 4
  • 2
7 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 1000 total points
ID: 12566352
%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
 
LVL 48

Expert Comment

by:AlexFM
ID: 12566362
Correction:
int i= 1, j= 2;
0
 
LVL 16

Accepted Solution

by:
imladris earned 1000 total points
ID: 12566890
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:imladris
ID: 12566906
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 12566969
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
 

Author Comment

by:collegegirl
ID: 12570563
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 12572389
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question