13 Replies - 865 Views - Last Post: 05 February 2013 - 06:09 AM Rate Topic: ***-- 3 Votes

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

printf Queries

Posted 27 January 2013 - 01:41 PM

While I know printf() returns the number of characters it prints, I am having problem with the & (or any other character in fact) in this code:

printf("%d",printf("%d %d",2,2) & printf("%d %d", 2, 2));


How does it interact to provide the output?
Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: printf Queries

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: printf Queries

Posted 27 January 2013 - 02:27 PM

The & is a bit wise and operator. For instance,

Say the first printf returns a 2 and the second returns a 2

Then the code execution results to

printf("%d",2 & 2);



2 & 2 = 0010 & 0010 = 0010 = 2

printf("%d", 2);


This post has been edited by jjl: 27 January 2013 - 02:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: printf Queries

Posted 27 January 2013 - 02:27 PM

It's the bitwise AND operator.
Was This Post Helpful? 1
  • +
  • -

#4 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: printf Queries

Posted 27 January 2013 - 09:22 PM

thanks, but how does it explain the output 2 22 23 ?
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: printf Queries

Posted 27 January 2013 - 10:34 PM

The two inner printf's will print first, the outer printf will print the bitwise AND of the return values of the inner printfs.
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,420
  • Joined: 05-May 12

Re: printf Queries

Posted 28 January 2013 - 06:22 AM

On the side, I hope you get out of that lecturer's class soon. Your time is better spent learning bigger concepts in the wide world of programming and software design than "parlour tricks". On the plus side, at least he's not teaching you how to count bits in an integer (yet). I hate bit counting hacks.
Was This Post Helpful? 2
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,288
  • Joined: 25-December 09

Re: printf Queries

Posted 28 January 2013 - 07:06 AM

I also suggest you seriously think about filing a complaint with the school administration. This "instructor" seems to be spending far too much time on these "parlor tricks" instead of actually teaching programming. My suspicion is that he/she is very incompetent and should be replaced. Just showing a few "tricks" is one thing but actually grading you on doing these tricks is really ridiculous. He/she should be concentrating more on the fundamentals instead of these "tricks". In my opinion time would be much better spent, and knowledge actually transferred by sticking with generally accepted principals.

Remember it is you and your classmates that will be suffering when you actually try to use your freshly learned "tricks" in the real world. Most employers will not tolerate the constructs you have posted in your last couple of topics.


Jim
Was This Post Helpful? 3
  • +
  • -

#8 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: printf Queries

Posted 28 January 2013 - 08:36 AM

When would you ever apply this in the real world of programming? Showing a few tricks and oddities is ok... but really he should be giving you tricks on how to make your code more efficient (once past the basics...)
Was This Post Helpful? 0
  • +
  • -

#9 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: printf Queries

Posted 28 January 2013 - 12:02 PM

You all are most correct. I think it's high time we talk to the administration about this. Thanks anyways. Now I know these are useless stuff.
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: printf Queries

Posted 28 January 2013 - 07:22 PM

Quote

When would you ever apply this in the real world of programming? Showing a few tricks and oddities is ok... but really he should be giving you tricks on how to make your code more efficient (once past the basics...)


I think this assignement is completely fair game and it did it's purpose, it made Jeet think critically about return values and operators.
Was This Post Helpful? 0
  • +
  • -

#11 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: printf Queries

Posted 28 January 2013 - 11:59 PM

View Postjjl, on 29 January 2013 - 07:52 AM, said:

Quote

When would you ever apply this in the real world of programming? Showing a few tricks and oddities is ok... but really he should be giving you tricks on how to make your code more efficient (once past the basics...)


I think this assignement is completely fair game and it did it's purpose, it made Jeet think critically about return values and operators.


My thinking tells me the output should be 22 22 2 and yet the output is 22 22 3. Now the & operator makes 2 & 2 -> 2 so where does the 3 come from? Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: printf Queries

Posted 29 January 2013 - 01:59 AM

Quote

My thinking tells me the output should be 22 22 2 and yet the output is 22 22 3. Now the & operator makes 2 & 2 -> 2 so where does the 3 come from? Thanks.


The inner printf's
,printf("%d %d",2,2) & printf("%d %d", 2, 2)



are printing two integers and a space, which is a total of 3 elements. Therefore, a 3 is returned from both printfs.
Was This Post Helpful? 2
  • +
  • -

#13 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: printf Queries

Posted 29 January 2013 - 08:28 AM

Thanks. So the space also counts.
Was This Post Helpful? 0
  • +
  • -

#14 vikky007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-February 13

Re: printf Queries

Posted 05 February 2013 - 06:09 AM

View PostJeet.in, on 27 January 2013 - 01:41 PM, said:

While I know printf() returns the number of characters it prints, I am having problem with the & (or any other character in fact) in this code:

printf("%d",printf("%d %d",2,2) & printf("%d %d", 2, 2));


How does it interact to provide the output?
Thanks.


Actually printf return integer like:

int i = printf("%d %d",2,2);
//i=3 bcoz there is a space in %d.

Now the statement becomes

printf("%d",3 & 3);


which gives 3.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1