Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you explain the Output?

Tags:

c++

c

What should be the output of the following code and why? I am little bit confused.

int a =10;
printf("%d %d %d",a,a=a+10,a);
like image 996
user327742 Avatar asked Apr 29 '10 09:04

user327742


People also ask

What is the explanation of output?

: the amount produced by a person in a given time. d. : power or energy produced or delivered by a machine or system (as for storage or for conversion in kind or in characteristics)

How do you explain output devices?

An output device is any piece of computer hardware equipment which converts information into a human-perceptible form or, historically, into a physical machine-readable form for use with other non-computerized equipment. It can be text, graphics, tactile, audio, or video.

How do you explain input and output?

Input and output, or I/O is the communication between an information processing system, such as a computer, and the outside world, possibly a human or another information processing system. Inputs are the signals or data received by the system and outputs are the signals or data sent from it.


2 Answers

The output is indeterminate, because a=a+10 is a side-effect, and the compiler is free to evaluate it before or after any of the other parameters.

EDIT: As David points out, the behaviour is actually undefined, which means all bets are off and you should never write such code. In practice, the compiler will almost always do something plausible and unpredictable, maybe even differing between debug and optimised builds. I don't think a sperm whale is a likely outcome. Petunias? Perhaps.

like image 89
Marcelo Cantos Avatar answered Oct 11 '22 13:10

Marcelo Cantos


The order of evaluation for a, b, and c in a function call f(a,b,c) is unspecified.

Read about sequence points to get a better idea: (The undefined behavior in this particular case is not due to sequence points. Thanks to @stusmith for pointing that out)

A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation.

Sequence points also come into play when the same variable is modified more than once. An often-cited example is the expression i=i++, which both assigns i to itself and increments i; what is the final value of i? Language definitions might specify one of the possible behaviors or simply say the behavior is undefined. In C and C++, evaluating such an expression yields undefined behavior.

like image 31
Amarghosh Avatar answered Oct 11 '22 12:10

Amarghosh