conditional expression

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

    #1

    conditional expression

    consider the following program:

    #include <stdio.h>
    #include <stdlib.h>

    int
    main(void)
    {
    int i;
    double f;

    printf("size = %u", sizeof((0)?f:i) ); /* 0 is zero*/

    putchar('\n');
    exit(0);
    }

    output:
    size = 8

    since 'i' is supposed to be an "int", on my 32-bit system I expected
    the size to 4 bytes. Why does this happen?

  • Marc Boyer

    #2
    Re: conditional expression

    Le 06-09-2006, chandanlinster <chandanlinster @gmail.coma écrit :
    consider the following program:
    >
    #include <stdio.h>
    #include <stdlib.h>
    >
    int
    main(void)
    {
    int i;
    double f;
    >
    printf("size = %u", sizeof((0)?f:i) ); /* 0 is zero*/
    >
    putchar('\n');
    exit(0);
    }
    >
    output:
    size = 8
    >
    since 'i' is supposed to be an "int", on my 32-bit system I expected
    the size to 4 bytes. Why does this happen?
    Because 0?f:i is an expression, it must have a type, staticaly.
    In this case, the type is certainly 'double'. If you want details,
    you should have a look on the 'implicit conversion' chapter in
    a good book.

    Marc Boyer

    Comment

    • lovecreatesbea...@gmail.com

      #3
      Re: conditional expression


      chandanlinster wrote:
      consider the following program:
      >
      #include <stdio.h>
      #include <stdlib.h>
      >
      int
      main(void)
      {
      int i;
      double f;
      >
      printf("size = %u", sizeof((0)?f:i) ); /* 0 is zero*/
      >
      putchar('\n');
      exit(0);
      }
      >
      output:
      size = 8
      >
      since 'i' is supposed to be an "int", on my 32-bit system I expected
      the size to 4 bytes. Why does this happen?
      This is the same result on my computer. The operand of the sizeof is
      (0) ? f : i which has the type same as f. "The C programming language,
      2nd", sec 2.11 has an example on this.

      Comment

      • SM Ryan

        #4
        Re: conditional expression

        "chandanlinster " <chandanlinster @gmail.comwrote :
        # consider the following program:
        #
        # #include <stdio.h>
        # #include <stdlib.h>
        #
        # int
        # main(void)
        # {
        # int i;
        # double f;
        #
        # printf("size = %u", sizeof((0)?f:i) ); /* 0 is zero*/

        Even if the compiler optimise the expression, the type
        of the conditional is double and the integer value
        must be converted to double.

        --
        SM Ryan http://www.rawbw.com/~wyrmwif/
        GERBILS
        GERBILS
        GERBILS

        Comment

        • Peter Shaggy Haywood

          #5
          Re: conditional expression

          Groovy hepcat chandanlinster was jivin' on 6 Sep 2006 02:00:18 -0700
          in comp.lang.c.
          conditional expression's a cool scene! Dig it!
          > printf("size = %u", sizeof((0)?f:i) ); /* 0 is zero*/
          In addition to what else you've been told, this causes undefined
          behaviour. You have a %u conversion specifier with a size_t argument.
          size_t is an implementation defined unsigned integer type. Thus, it
          might be unsigned int, but it might not. It's entirely possible that
          it is some other unsigned integer type.
          The usual way of handling this is well known (See question 12.9b in
          the FAQ.), and has been posted in this newsgroup recently.

          --

          Dig the even newer still, yet more improved, sig!


          "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
          I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

          Comment

          Working...