5 Replies - 604 Views - Last Post: 27 September 2015 - 09:14 PM Rate Topic: -----

#1 westin467   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?

Thanks in advance

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   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12617
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 westin467   User is offline

  • New D.I.C Head

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

Re: what are the values printed by printf()

Posted 27 September 2015 - 07:31 PM

View Postmacosxnerd101, 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
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 westin467   User is offline

  • New D.I.C Head

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

Re: what are the values printed by printf()

Posted 27 September 2015 - 08:30 PM

View PostKYA, 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
Was This Post Helpful? 0
  • +
  • -

#6 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • 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. ; )
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1