Program2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Meenu

    #1

    Program2

    The output of the following program confuses me

    void main()
    {
    int x=10,y=20,z=5,i ;
    i=x<y<z;
    printf("i=%d\n" ,i);
    }

  • Maw

    #2
    Re: Program2

    y<z is 0
    x<0 is 1
    i=x

    Comment

    • Maw

      #3
      Re: Program2

      *the other way*

      Comment

      • junky_fellow@yahoo.co.in

        #4
        Re: Program2



        Meenu wrote:[color=blue]
        > The output of the following program confuses me
        >
        > void main()[/color]
        This is wrong. Use
        int main(void)
        [color=blue]
        > {
        > int x=10,y=20,z=5,i ;
        > i=x<y<z;
        > printf("i=%d\n" ,i);
        > }[/color]

        i=x<y<z is equivalent to
        i = ((x<y)<z); <--- x<y is True
        i = ( 1 < z) <--- 1<y is True.
        i = 1

        Comment

        • CBFalconer

          #5
          Re: Program2

          Maw wrote:[color=blue]
          >
          > y<z is 0
          > x<0 is 1
          > i=x[/color]

          That's nice. On what do you base that assertion? See below.

          --
          "If you want to post a followup via groups.google.c om, don't use
          the broken "Reply" link at the bottom of the article. Click on
          "show options" at the top of the article, then click on the
          "Reply" at the bottom of the article headers." - Keith Thompson


          Comment

          • Martin Ambuhl

            #6
            Re: Program2

            Meenu wrote:[color=blue]
            > The output of the following program confuses me[/color]

            [1]
            [color=blue]
            > void main()
            > {
            > int x=10,y=20,z=5,i ;
            > i=x<y<z;
            > printf("i=%d\n" ,i);
            > }
            >[/color]

            In both this and your other posting

            [2]
            [color=blue]
            > main()
            > {
            > char str[]="part-time musicians are semiconductors" ;
            > int a=5;
            > printf(a>10?"%5 0s":"%s",str) ;
            > }[/color]

            You are missing needed headers, since printf is a variadic function.
            #include <stdio.h>

            The 'void' return type for main() in [1] is wrong. main returns an int.
            In [2], your main does return an int implicitly in C89 (or C90), where a
            function that has no named returned type is assumed to return an int.
            However, (a) implicit int is removed from the current standard, so [2]
            is wrong under the current standard. Further, in C89 (and C90) in which
            implicit int is allowed, main() should explicitly return a value.

            In [2], there is no way to predict the output. To have a portable C
            program, the last line of output to a stream must end in an end-of-line
            ('\n') character.

            Now tell me that you're a troll and that I took the bait.

            Comment

            • Keith Thompson

              #7
              Re: Program2

              "Maw" <martinaw@gmail .com> writes:[color=blue]
              > *the other way*[/color]

              You've posted twice on this thread without providing any context or
              attributions. I have no idea what you mean by "the other way".

              The broken groups.google.c om interface is partly responsible for this,
              but only partly. It *is* possible to use groups.google.c om properly.

              The following has been posted here hundreds of times. Read it. Pay
              attention to it.

              If you want to post a followup via groups.google.c om, don't use
              the broken "Reply" link at the bottom of the article. Click on
              "show options" at the top of the article, then click on the
              "Reply" at the bottom of the article headers.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              Working...