Strange GCC short int conversion warning

c, gcc, implicit-conversion

Solution

When you do arithmetic computations, the operands are subject to "the usual arithmetic conversions" (a superset of the "integer promotions" quoted in Acme's answer—he beat me to this but I'll go ahead and post anyway :-) ). These widen `short int` to plain `int`, so:

a + b

computes the same result as:

((int) a) + ((int) b)

The `return` statement must then narrow this `int` to a `short int`, and this is where gcc produces the warning.

Problem

I have a bit of C code, which goes exactly like this: ``` short int fun16(void){ short int a = 2; short int b = 2; return a+b; } ``` When I try to compile it with GCC, I get the warning: ``` warning: conversion to 'short int' from 'int' may alter its value [-Wconversion] return a+b; ^ ``` Though there is no visible conversion. Both operands are short and even the returning value is short as well. So, what's the catch?

Original source

Related problems