Really good problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    Really good problems

    I came across certain MCQs in some book n some of them are very interestingly unexplainable :-),... at least i couldnt understand them;plz if anyone has explaination for any of them, let me know.
    Code:
     
    void main()
    {
      printf("%c",'%d');
    }
    whatever you may write after %.. i mean only 1 more charachter, only % will be the output.. how come?
    Code:
     
    void main()
    {
      int i=1,,j=0,k=i--&&++j||--i;
      printf("%d%d%d%"i, j,k);
    }
    output for this is 0 1 1
    where as when i debugged it, value of i is coming as -1;.
    Code:
     
    void main()
    {
        printf("%d%d%d%",8);
    }
    output for this 8,0 ,garbage.. how come 0 and not garbage at second place?
    Code:
     
    void main()
    {
      int no=3;
      printf("%d%d",scanf("%c",&no),no); 
    }
    we have to enter a value here, and no matter what value we enter, output is 1 and 3. !!
    Code:
     
    void main()
    {
      printf("%d",5/0);
    }
    output to this is very surprisingly 5.. when i checked in help of turbo C, they had given u cant divide any integer by 0, else an error will be generated.
    but only a warning had come and answer was 0.
    Code:
     
    void main()
    {
    int i=0;
    printf("%d",5/i);
    }
    this gave runtime divide error!....
    ultimately we are carrying out same thing right?
    Code:
     
    void main()
    {
      printf("%d",010);
    }
    here the output comes as 8!.... so may be its taking some octet base because for 0101 the value came as 65... a bit explainable.. but for 1010 output comes as 1010 itself...
    Code:
    printf/*("sun*/ computer*/ education");*/
    comment is taken only till computer and if i remove * of computer it will be taken till education; but it takes the farthest one if m not wrong.
    Whats the logic behind this?
    Code:
    sizeof(3.14)
    comes as 8,..but that's the value for double and not for float...
    Code:
    sizeof(float)
    is 4, then how do we deciede which is float and double?
    Code:
     
    printf("%d",printf("sun"));
    output for this is sun and 3..
    I got how 3 came, because sun has 3 charachters in it and i had also done lot many changes in sun and accordingly got answers depending on length of word.. but why that happens.. after inner printf has printed the things; does it returns no. of charachters or sth?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    OK so ignoring that all you examples which include main have void main() which is undefined behaviour.

    Code:
     
      printf("%c",'%d');
    The fact you compiler compiles this line without a warning just shows your compiler up as not very good. You can not have a multi-byte character like this, your compiler is creating a single byte character using the first supplied character in your constant and then printing it in the normal way using %c.

    Code:
      int i=1,,j=0,k=i--&&++j||--i;
    The fact that this produces different results under different conditions almost certainly points to undefined behaviour and therefore analysis of it is pointless. Needless to say I would expect never to find something like this in a real life project.

    Code:
     
        printf("%d%d%d%",8);
    output for this 8,0 ,garbage.. how come 0 and not garbage at second place?

    The second value is garbage too, it just randomly has the value 0

    Code:
     
      printf("%d%d",scanf("%c",&no),no);
    You have provide a int * to scanf where you should have had a char *. However the order of function parameter evaluation is not guaranteed so it may have evaluated no before scanf("%c",&no) on your platform.

    Code:
     
     printf("%d",5/0);
    
    int i=0;
    printf("%d",5/i);
    These both give exceptions on my computer, divide by 0 is undefined behaviour so anything may happen.

    Code:
     
      printf("%d",010);
    The leading 0 means treat the value as octal (base 8) so 010 has a decimal value of 8 where as 10 has a decimal value of 10. Try compiling 090 you should get an error because 9 is not a valid octal digit.


    Code:
    printf/*("sun*/ computer*/ education");*/
    Whats the logic behind this? Who nows just don't nest comment markers.
    Code:
    sizeof(3.14)
    comes as 8,..but that's the value for double and not for float...

    Of course you provide a constant double, if you want to provide a constant float you should have put 3.14f

    Code:
     
    printf("%d",printf("sun"));
    You have 2 printfs, the outper printf prints the result of the inner printf. You have guess at the meaning of the return value of printf now go and read the documentation to see if you are right.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by Parul Bagadia
      Code:
      void main()
      {
        printf("%d",5/0);
      }
       
      void main()
      {
      int i=0;
      printf("%d",5/i);
      }
      In the first case, the divide-by-zero occurs at compile-time. Your compiler should have reported an error. In the second case, the divide-by-zero occurs at run-time. Whether or not you get a divide-by-zero exception depends on the capabilities of the target environment. A compiler with a really savvy static analyzer might be able to recognize the second divide-by-zero at compile-time.

      Most of these questions probe the implementation-dependent-behavior and undefined-behavior of your particular compiler. What's the point of that? I hope you are not intending to write programs that depend on the behavior you observed!

      No amount of observation can make undefined-behavior predictable. It can vary with the phase of the moon.

      Comment

      • Parul Bagadia
        New Member
        • Mar 2008
        • 188

        #4
        [quote=Banfa;345 1346]
        Code:
         
          printf("%d%d",scanf("%c",&no),no);
        You have provide a int * to scanf where you should have had a char *. However the order of function parameter evaluation is not guaranteed so it may have evaluated no before scanf("%c",&no) on your platform.

        Thanks for all ,but i didnt understand this one, how the output is 1 and 3 , am not understanding 1 part, it should give output as the no. accepted by user.,if am not wrong.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          scanf returns the number of items if converted, which in this case is 1, and printf prints that first.

          1 is the return value of scanf, 3 is the value of no when it was interpreted to be placed in the parameter list for the printf call.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Banfa
            scanf returns the number of items if converted, which in this case is 1, and printf prints that first.

            1 is the return value of scanf, 3 is the value of no when it was interpreted to be placed in the parameter list for the printf call.
            It's just indefined behaviour because you don't know the order of evaluation of the arguments of the printf function: scanf might or might not have scanned a value for the 'no' variable when the value of that variable is needed.

            kind regards,

            Jos

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              It is not undefined behaviour, true you do not know the order the parameters of the printf call are made in but there is a sequence point at the end of the call to scanf so all the side effects of the function call are resolved.

              The unknown parameter evaluation sequence just means that you can not know if printf will receive the value of no before or after the scanf call but when no is evaluated it will have 1 of these 2 values (note in the original example no is initialised).

              The behaviour is not undefined, it is very clearly defined, and specifically on a given platform will always give the same result (since parameter evaluation order rarely changes). That result is just not at all useful in a portable program.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                At least it is "unspecifie d behavior":

                Originally posted by Standard 3.19
                [#1] unspecified behavior
                behavior where this International Standard provides two or
                more possibilities and imposes no requirements on which is
                chosen in any instance
                but I think this particular case is still undefined behavior because of this:

                Originally posted by Standard 6.5.16
                [#4] The order of evaluation of the operands is unspecified.
                If an attempt is made to modify the result of an assignment
                operator or to access it after the next sequence point, the
                behavior is undefined.
                ... but I'll think about it a bit more (it is silly without doubt ;-)

                kind regards,

                Jos

                Comment

                Working...