Faz Hassan
Faz Hassan

Reputation: 33

Evaluation of an Expression in C Without an Explicit Type

Consider the following question from a previous paper on C:

Show step-by-step how the following C expression is evaluated, giving the value of the expression and the type of that value.

9 / 3 / 2 * 6 + 2 * 1.5

Is the type a float or a double? Could someone please clarify? (Note the lack of an explicit type in the question.)

Upvotes: 2

Views: 247

Answers (4)

slevy1
slevy1

Reputation: 3832

This problem may also be resolved by utilizing an approach based on reflection. One may compare the type of the expression with a float and then with a double by using __builtin_types_compatible_p(). This function determines whether two types are the same. For this purpose, __typeof__ can handily retrieve the type of the expression and pass it to the built-in function, as follows:

#include <stdio.h>

int main() {
    if (__builtin_types_compatible_p(__typeof__(9 / 3 / 2 * 6 + 2 * 1.5), float)) {
        puts("float");
    } else if (__builtin_types_compatible_p(__typeof__(9 / 3 / 2 * 6 + 2 * 1.5), double)) {
        puts("double");
    }
    return (0);
}

See demo.

When the types are the same, the function returns 1; see more here.

Note: __typeof__ does not return a string but a system type.

Instead of providing the entire mathematical expression to typeof, one may simply pass 1.5 since the question really turns on what data type represents this floating-point value.

See related information: Syntax and Sample Usage of _Generic in C11.

Upvotes: 1

kyriakosSt
kyriakosSt

Reputation: 1772

It is a double.

As mentioned in Kernighan & Ritchie's "The C programming language":

Floating-point constants contain a decimal point (123.4) or an exponent (1e-2) or both; their type is double, unless suffixed.

A quick way to find out for yourself would also be to write that code: printf("%d %d\n", sizeof(1.5)==sizeof(double), sizeof(1.5)==sizeof(float)); which will output 1 0 indicating that 1.5 is a double.

Edit: Of course, type sizes in C are system dependent, so that code should not be entirely trusted.

Upvotes: 2

Mikhail Romanko
Mikhail Romanko

Reputation: 396

According to the usual arithmetic conversions (6.3.1.8 Usual arithmetic conversions)

Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.

the answer:

  1. 9 / 3 = 3 (int)
  2. 3 / 2 = 1 (int)
  3. 1 * 6 = 6 (int)
  4. 2 * 1.5 = 3.0 (double)
  5. 6 + 3.0 = 9.0 (double)

Upvotes: 12

syntagma
syntagma

Reputation: 24304

In your expression 1.5 is a double (in order for the compiler to treat it a a float it should have an f suffix: 1.5f). All of the other numbers are of type int.

In order to show how the 9 / 3 / 2 * 6 + 2 * 1.5 expression will be evaluated, look at the C Operator Precedence for reference (both priority and associativity). Note that 2 * 1.5 will be of type double (and so the type of the whole expression).

Upvotes: 5

Related Questions