Assigning double constant to integer types

Discussion in 'C Programming' started by dev, Apr 17, 2009.

  1. dev

    dev Guest

    when i do int x=2.2;

    Is this behaviour undefined.?
    K & R says that it may draw a warning but it is not illegal....

    now one more point--
    Can a legal behaviour be undefined?
    or vice versa,I mean that can a undefined behavior be illegal?
    dev, Apr 17, 2009
    1. Advertisements

  2. This is completely legal:
    2.2 is implicitly converted to int
    Use -Wconversion in gcc to catch these type of conversions.
    Michael Tsang, Apr 17, 2009
    1. Advertisements

  3. dev

    Eric Sosman Guest

    No.[*] It declares a variable of type `int' named `x',
    and initializes it with the value two.

    [*] Not from the available context, that is. With enough
    additional stuff the behavior could be made undefined: For
    example, by putting the line at file scope in one translation
    unit, and putting `extern double x;' in another.
    Yes, legal behavior can be undefined. That is, many
    jurisdictions will not impose legal penalties upon you if
    you write code that exhibits undefined behavior. The C
    Standard itself does not have the force of law.

    (In other words, you need to decide what you mean by
    "legal behaviour." Once you've decided that, your question
    will probably answer itself.)
    Eric Sosman, Apr 17, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.