1

I am new to c language

I want to understand how to judge the data type of the output result when a math expression contains both float and int

E.g

float x = 2.5,y = 4.7;
int a = 7;

How to judge the output value type of x + a%3*(int)(x+y)%2/4;

essesoul
  • 21
  • 6
  • Usual arithmetic conversions in the accepted answer is a good place to start reading. – Retired Ninja Oct 21 '22 at 02:37
  • 1
    Integers may be implicitly converted to floats, but not the other way around. If **one** of the operands of a `+`, `-`, `/`, `*` is a float, the other operands, if integers, will be converted to floats and the result is a float. If **all** operands are integers, the operation and the result are integers. This is valid for all sub-expressions, but the type of the result is decided by the top-most operator (the last operation). In your ex., `x+y` is float, `(int)(x+y)%2/4` is int, `a%3*(int)(x+y)%2/4` also int, and the whole expression `x + ...` is float. Look up "implicit type conversion in C" – kikon Oct 21 '22 at 04:32

0 Answers0