David542
David542

Reputation: 110163

Converting to a larger integer type

I know this isn't the best question to ask on StackOverflow, but I'm looking for the term that is used to describe when converting to a larger numeric type to handle overflow. A basic example would be:

# int is 32 here
int a = 2147000000;
int b = 10000000;
printf ("The sum of a and b is: %ld", (long)a+b);

What is the term for 'converting to a wider type', whether done explicitly or implicitly?

Upvotes: 1

Views: 507

Answers (2)

chux
chux

Reputation: 153457

What is the term for 'converting to a wider type', whether done explicitly or implicitly?

I recommend widening - effectively just what OP uses in the question.

C spec uses wide, wider, widest to describe going from some type to one of greater rank and range as in int to long long. (narrow, ... to go the other way). This also applies to float, double, long double.

int and long may have the same width though so (long)a+b does not certainly prevent overflow. Using long long or intmax_t is more likely to provided extended range.

// printf ("The sum of a and b is: %ld", (long)a+b);
printf ("The sum of a and b is: %jd", (intmax_t)a+b);

See also @Eric Postpischil

Upvotes: 1

4386427
4386427

Reputation: 44274

From the standard (draft N1570):

6.5.4 Cast operators .... Preceding an expression by a parenthesized type name converts the value of the expression to the named type. This construction is called a cast.

So the standard uses the term: convert

So using a cast you convert an int to a long

The standard also often uses "conversion", like conversion from int to long

Upvotes: 1

Related Questions