TITLE: Unspecified versus undefined behavior PROBLEM: COATES@EUROPA.UMUC.EDU (Ell) What's the diff between "undefined" and "unspecified"? RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 19 Apr 95 The C and C++ standards define four categories of system behavior with regard to a source-code program. 1. Required behavior. The system must respond in a defined way. Example: If x and y have type int and their sum is in the range -INT_MAX...+INT_MAX, the result of evaluating x+y must be equal to the sum using ordinary arithmetic. 2. Implementation-defined behavior. The system is allowed to implement any of a number of behaviors, but must document what the behavior is. Example: The result of x%y where x and y have integral type and y is negative may be either positive or negative, but the system must document which it will be. 3. Unspecified behavior. The system is allowed to implement any of a number of behaviors, but doesn't have to tell you which it implements, or even be consistent within the same program or function. Example: The arguments to a function must all be evaluated before the function is called, but they may be evaluated in any order. The system need not be consistent in choosing an order, and need not tell you how to predict the order chosen. 4. Undefined behavior. The standard places no requirements whatever on the system. Literally any behavior is permitted (by the standard, if not by customers). Example: If you dereference a null pointer, you might get the value zero, or an arbitrary value, or a program abort, or a signal of some kind, or some sort of exception, or the implementation might send email to your boss about your careless programming habits. Or all of the above. Or something else entirely.