Sunday, August 2, 2009

Int, float or char?

Can someone please tell me what type of expression this is? int, float or char,,,,,beginner here.....





C+N*X?


Thanks for any help.

Int, float or char?
The operation will fail. C does not "auto-convert" data types according to which is the most apparent one.
Reply:It's going to vary between compilers. All of them will perform N * X first, most likely promoting the int to a float, or throwing an error saying you have to specifically cast one or the other. The remaining operation would get promoted to int or float (depending which one was selected or explicitly cast), since char is the lower type (or again throw an error telling you to explicitly cast one of the variables).
Reply:for arithmetic, they all get promoted to the most complex type





int N;


char C;


float X;





C+N*X; the compiler does the arithmetic using floats. most compilers also take the 'char' as a signed char (-128 thru 127)
Reply:Well, if I am not mistaken, this operation will fail, because a 'char' value cannot be used in calculations. Now, if not for that, I would say the result would be a float, as when doing calculations, the results are typically cast to the most flexible type. In other words, if the calc could result in a decimal, the result woud have to be a float.
Reply:It' would a float.





N * X ==%26gt; int * float = float


C + (N * X) ==%26gt; char + float = float


No comments:

Post a Comment