Why and when evaluates from left to right | SoloLearn: Learn to code for FREE!

0

Why and when evaluates from left to right

#include<stdio.h> void test_function(int x, int y, int z) {    printf("The value of x: %d\n", x);    printf("The value of y: %d\n", y);    printf("The value of z: %d\n", z); } main() {    int a = 10;     test_function(a++, a++, a++); } Output The value of x: 12 The value of y: 11 The value of z: 10

2/18/2020 5:32:30 PM

Lakshmi Reddy Sanikommu

2 Answers

New Answer

+1

The result is undefined by Standards and defined by Compiler implementation. The expression has "sequence point" issue which happen when a variable is accessed and modified more than once in the same expression. The output can be x = 10, y = 11, 12. Try it out on a different compiler. The order of evaluation of expression is undefined in C for most cases except in very few cases like evaluation of logical operators. How the parameters are passed to the functions is defined by compiler implementers not by Standards. Traditionally C used "C calling convention" which passed parameters from right to left on to the function stack. Modern compilers may try to pass as many parameters they can through registers. Note that order of passing parameters and order of evaluation of expression are two different things

0

But in which cases our left to right evaluation occurs if the compiler of c has a ability to evaluate from left to right if need ?