Thread: printf behaviour

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    10

    printf behaviour

    Code:
    printf("\n%d",printf("%d %d",2,2)&printf("%d%d ",2,2));

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    understood..2 2222

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Now change the values in the printfs to 10s.... what do you get?

    But I'm just a bit curious... what's with all this screwball code you keep coming up with?

    Seriously... nobody in their right mind would ever use code like this in a real-world program...

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    actually i am solving some c questions which are usually asked in interviews.i am not doing those programs..the above qn is AMAZON written question.
    if u know the answer help to others..otherwise dont comment anyone...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The thing that worries me the most is that you believe there is a question in your first post.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by raju.b41 View Post
    actually i am solving some c questions which are usually asked in interviews.i am not doing those programs..the above qn is AMAZON written question.
    if u know the answer help to others..otherwise dont comment anyone...
    1) DO NOT EVER presume to tell me what to do.

    2) If you are getting questions like that in job interviews, you really don't want to work there. Any company producing code with stuff like that in it is probably producing crap software that you don't want your name connected to. One of the hardest lessons in technical trades is that 50% of your perceived "hirability" is entirely based upon reputation.

    3) It's "you" ... not "u"... I am a person not a letter from the alphabet.

    4) If you are getting questions like that and can't answer them on your own... maybe you don't deserve the job.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I dont really understand these question coming here, why cant you (the OP) try compile it to himself and find out the answer. The board isn't like compiler where you pump in some code and we compile and provide you the answer.

    On the other note, i do like this interview question. Specially the '&'.

    ssharish
    Last edited by ssharish2005; 07-16-2011 at 10:08 AM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ssharish2005
    I dont really understand these question coming here, why cant you (the OP) try compile it to himself and find out the answer.
    If the interview question is "what is the output of this line?", then simply compiling and running the code will not give a complete answer because of implementation defined behaviour. But...

    Quote Originally Posted by ssharish2005
    On the other note, i do like this interview question. Specially the '&'.
    We don't actually know what is the question.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >We don't actually know what is the question.
    Rather the code/statment i should have said.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yeah, every interview question should be "what is the result of this statement that is using undefined behavior?" How ridiculous.

    I'd like to see where/how this question is related to Amazon.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Interesting enough the question is:
    1. What is the output of this statement ?
    Printf(“%d”,printf(“%d %d”,2,2) & printf(“%d %d ”, 2, 2));
    a. 22222
    b. 22221
    c. It will give an error during compilation
    This supposedly is a Citrix question according to Amazon. I found a reference to it on Amazon but the actual question was here believe it or not.

    Still the OP could bother to actually post a question and even provide his attempt at coming up with why. I have heard of crazy interview questions before from my buddy who works in the field.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well since the format strings contain space, and the return result of printf() is normally the number of characters printed, the final value isn't going to be 2.

    In fact, the posted example for "citrix" is the result of 3 & 4, which is zero.

    Gotta love "clever" quiz questions falling into their own trap of being too clever for their own good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Salem
    Gotta love "clever" quiz questions falling into their own trap of being too clever for their own good.
    Haha...yep. I was just curious where the OP was getting this stuff and decided to share.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    I found a reference to it on Amazon but the actual question was here believe it or not.
    Oh my... It looks like the "I want a job I'm hopelessly underqualified for" crowd has their own website...

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    Well since the format strings contain space, and the return result of printf() is normally the number of characters printed, the final value isn't going to be 2.
    There is also a newline in the outer most printf, at an odd place, so what they really end up seeing is even more uncertain.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behaviour of printf after stdout redirection
    By printfede in forum C Programming
    Replies: 14
    Last Post: 03-02-2011, 07:25 AM
  2. Confused by behaviour
    By rocketman50 in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2010, 10:55 AM
  3. Strange behaviour of printf
    By pshirishreddy in forum C Programming
    Replies: 5
    Last Post: 08-29-2009, 11:46 PM
  4. Unexpected behaviour
    By fnoyan in forum C++ Programming
    Replies: 5
    Last Post: 03-05-2005, 09:45 AM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM