OnSwipe redirect code

Sunday, May 6, 2007

Thinking C: Behaviour - - Undefined or Unspecified?

C has been my cherished programming language right from the time I was hooked up with computer engineering and I still love.

Now here is discussion and a concept that I recently got to know through a discussion in IRC. ( server: irc.freenode.net channel: ##C).

I was aware of the concept of sequnece points and undefined behaviour in C. But it was during this discussion that I got to know about the Unspecified Behaviour.

Generally both Unspecified and Undefined behaviour occur because of the side effects not being cleard off until a sequence point is reached.

In case of Undefined behaviour nothing is mentioned about that kind of situation in the C standards. The implementors are free to do anything for those situations. They can simply leave it unimplementd or they may just flash up an error or they may follow there own conventions regarding that. We ll, in no position, be able to predict the behaviour and hence the name.

Eg: int a[10],i;
/* The array a is filled with some values */
a[i]=i++;

In case of Unspecified Behaviour the C standards give certain options to handle the situation. The implementors are expected to follow any of those options. Unlike undefined behaviour they donot have complete freedom. They have freedom of choice. Hence the behaviour of a code being classified as Unspecified can be predicted on a particular compiler on a particular platform.

Eg: int a=5;
a=scanf("%d",&a) + a;

Here we are not sure whether the scanf is first executed or the second operand a is first evaluated. Hence the Unspecified behaviour.

No comments:

Post a Comment