ARM Technical Support Knowledge Articles

CONFLICTING FUNCTION TYPE AND RETURN TYPE

Applies to: General Topics

Answer


Information in this article applies to:


QUESTION

I have written the following code:

char foo(void)
{
  int x = 0xABCD;
  return x;
}

The returned value is truncated. Why is there no warning given for this situation?

ANSWER

The ANSI C Standard, section 6.6.6.4, states:

"If a return statement with an expression is executed, the value of the expression is returned to the caller as the value of the function call expression. If the expression has a type different from that of the function in which it appears, it is converted as if it were assigned to an object of that type."

So, rather than generating a warning, the int in the example above is simply converted to a char.

MORE INFORMATION

Refer to section 6.6.6.4 - 'The return statement' in the ANSI C Standard (ANSI/ISO 9899-1990).

Article last edited on: 2005-07-19 10:23:07

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential