Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the behaviour of if(++i) ++i defined?

Tags:

c

Suppose I have this code:

int main(void)
{
    int i = rand();
    if (++i) ++i;
    return i;
}

Is the behaviour defined here? I know that i = ++i is undefined and the second line in main contains a similar thing. The rand() call is there to stop the compiler from optimising out what I think is the offending line.

like image 324
Paul Logue Avatar asked Feb 20 '18 16:02

Paul Logue


People also ask

What is a behavior defined as?

1 : the manner in which a person acts Students are rewarded for good behavior. 2 : the whole activity of something and especially a living being Scientists observed the elephant's behavior. behavior. noun. be·​hav·​ior.

What is behaviour example?

The definition of behavior is the way a person or thing acts or reacts. A child throwing a tantrum is an example of bad behavior. The actions of chimps studied by scientists are an example of behaviors. The way a person behaves or acts; conduct; manners.

What type of behavior C is undefined?

According to the C standards, signed integer overflow is undefined behaviour too. A few compilers may trap the overflow condition when compiled with some trap handling options, while a few compilers simply ignore the overflow conditions (assuming that the overflow will never happen) and generate the code accordingly.

What is behaviour and types of behaviour?

Combining history and function implies the existence of seven types of behaviour production systems in human brains responsible for reflexive, instinctual, exploratory, driven, emotional, playful and planned behaviour.


1 Answers

From C standard

The following are the sequence points described in:-

.... Between the evaluation of a full expression and the next full expression to be evaluated. The following are full expressions: an initializer that is not part of a compound literal (6.7.9); the expression in an expression statement (6.8.3); the controlling expression of a selection statement (if or switch) (6.8.4); the controlling expression of a while or do statement (6.8.5); each of the (optional) expressions of a for statement (6.8.5.3); the (optional) expression in a return statement.

So yes this is valid. I am just showing you the standard quote so that you feel satisfied that rule is stated somewhere.

like image 170
user2736738 Avatar answered Sep 28 '22 06:09

user2736738