what are the values printed by printf()

Page 1 of 1

5 Replies - 604 Views - Last Post: 27 September 2015 - 09:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=382200&amp;s=41f45b33b3291c68a9a18bde26b48589&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 westin467

Reputation: 0
• Posts: 20
• Joined: 27-September 15

what are the values printed by printf()

Posted 27 September 2015 - 06:08 PM

Having trouble figuring out what this code means. This is my first programming course.

1) What are the values printed by the printf() function?
a)
```#include <stdio.h> int main() {
int i, j, k;
i = 2;
k = j = i -= 3;
printf("%d %d %d\n", i, j, k);
}

```

b )
```#include <stdio.h> int main() {
int i, j, k;
i = 7; j = 7; k = -10;
printf("%d", k > i > j);
i = 1; j = 2; k = 3;
printf("%d", i < j == j < k);

```

Solution:
1a)
2, 3 ,3

1b)

-10>7>7
1<2 = 2<3

am I on the right track here?

This post has been edited by macosxnerd101: 27 September 2015 - 06:10 PM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

Replies To: what are the values printed by printf()

#2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12617
• Posts: 45,776
• Joined: 27-December 08

Re: what are the values printed by printf()

Posted 27 September 2015 - 06:10 PM

Have you tried running your code? That's the best way to determine the output.

#3 westin467

Reputation: 0
• Posts: 20
• Joined: 27-September 15

Re: what are the values printed by printf()

Posted 27 September 2015 - 07:31 PM

macosxnerd101, on 27 September 2015 - 06:10 PM, said:

Have you tried running your code? That's the best way to determine the output.

yea, the first one i got -1, -1, -1 and the second one was 01. Not sure what that means

#4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

Re: what are the values printed by printf()

Posted 27 September 2015 - 07:57 PM

Is the purpose of the exercise to find out if your compiler implements operator order in a certain way? Off the top of my head, I have no idea if the -= operator takes precedence to the assignment operator on the same line. My gut reaction without diving into the spec is to say it's undefined behavior.

Interesting, for the first, gcc will execute -= first.
For the second, some well placed newline operators will illustrate that you got
0
1

All on the same line is terrible.

For the second, the interesting tidbit is that in the C family of languages booleans are true/false but are actually 1/0 values. Your code is evaluating a boolean expression that gets passed to printf and interpreted as an integer.

This post has been edited by KYA: 27 September 2015 - 07:59 PM

#5 westin467

Reputation: 0
• Posts: 20
• Joined: 27-September 15

Re: what are the values printed by printf()

Posted 27 September 2015 - 08:30 PM

KYA, on 27 September 2015 - 07:57 PM, said:

Is the purpose of the exercise to find out if your compiler implements operator order in a certain way? Off the top of my head, I have no idea if the -= operator takes precedence to the assignment operator on the same line. My gut reaction without diving into the spec is to say it's undefined behavior.

Interesting, for the first, gcc will execute -= first.
For the second, some well placed newline operators will illustrate that you got
0
1

All on the same line is terrible.

For the second, the interesting tidbit is that in the C family of languages booleans are true/false but are actually 1/0 values. Your code is evaluating a boolean expression that gets passed to printf and interpreted as an integer.

I belive the purpose of this question is to just give the values of printf.
Would that be correct then? a)-1, -1, -1 and b)01

#6 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

Re: what are the values printed by printf()

Posted 27 September 2015 - 09:14 PM

I believe the purpose of the exercise is for you to see if you understand why things that get printed are printed. Do you understand why?

KYA said:

Interesting, for the first, gcc will execute -= first.
Well, yes, and that shouldn't be a surprise. : P They will be executed right to left. -= will return lvalue reference of left hand side and that will be taken as an operand of the = and so on, and on. No UB there. Your gut reaction was probably about things like i = i -= 1. ; )