Assigning double constant to integer types

D

dev

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?
 
M

Michael Tsang

dev said:
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?

This is completely legal:
2.2 is implicitly converted to int
Use -Wconversion in gcc to catch these type of conversions.
 
E

Eric Sosman

dev said:
when i do int x=2.2;

Is this behaviour undefined.?

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.
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?

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.)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top