1
int i=9999;  

char c=i;  

gives no overflow warning, While

char c=9999;  

gives,

warning C4305 initializing truncation from int to char

why no overflow warning when converting int to char?

8
  • 3
    @pmg: No, they're both initializations. Commented May 11, 2013 at 18:08
  • You're right @KeithThompson, thank you. I deleted the wrong comment. Commented May 11, 2013 at 18:35
  • @pmg That comment of yours confused me so much that I had to post it in a separate question.This is what you had said--It's not the same thing. The first is an assignment, the second is an initialization.And here's the link stackoverflow.com/questions/16500028/… Commented May 12, 2013 at 3:31
  • @Rüppell'sVulture: sorry about that. Commented May 12, 2013 at 8:19
  • @pmg Actually,StackOverflow plebs like me don't expect ANY mistakes from the aces like you,with 25k+ reputation..LOL.. Commented May 12, 2013 at 9:21

2 Answers 2

9

You'll get warning C4244 when compiling with /W4 (which you should always do).

warning C4244: 'initializing' : conversion from 'int' to 'char', possible loss of data

Sign up to request clarification or add additional context in comments.

2 Comments

:( I forgot to enable the highest warning. Thanks
Note that that warning doesn't take into account the fact that the value of i happens to be 9999. The compiler would probably issue the same warning for int i = 42; char c = i;
1

Whether any code construct produces a warning is up to the cleverness of the compiler and the choices made by its authors.

char c=9999;

9999 is a constant expression. The compiler can determine, just by analyzing the declaration with no additional context, that it's going to overflow. (Presumably plain char is signed; if it's unsigned, the conversion is well defined -- but a compiler could still choose to warn about it.)

int i=9999;  

char c=i;

This has the same semantics, but for a compiler to warn about the initialization of c, it would have to know that i has the value 9999 (or at least a value outside the range of char) when it analyzes that declaration. Suppose you instead wrote:

int i = 9999;
i = 42;
char c = i;

Then clearly no warning would be necessary or appropriate.

As James McNellis's answer indicates, a suffiently clever compiler can warn about either case if it performs additional analysis of what's going to happen during the execution of the program. For some compiler, it helps to enable optimization, since the analysis required to optimize code (without breaking it) can also reveal this kind of potential run-time error.

I'll note that this is an answer to the question you asked: why is there no warning. The answer you accepted is to the implied question: "I want a warning here; how can I enable it?". I'm not complaining, just observing.

1 Comment

+1. pmg too admitted the comment about one being an initialization and other being an assignment was wrong.Now the whole thing is very clear.Thanks for the time it took to come up with 2 fine answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.