Determine the output of this C code using post-increment operators. Go!

preview_player
Показать описание
Greetings C developers, here's a new challenge for you! Can you predict the output of this code snippet involving post-increment operators in function arguments? Watch the video and share your thoughts in the comments!
Рекомендации по теме
Комментарии
Автор

Need a hint? Think about how C evaluates function arguments and the order of operations. How does the post-increment operator affect the values passed to the function?

GoogleDevelopers
Автор

In this code, a++ is a post-increment, which means that the value of a is used in the expression and then incremented. The behavior of a++ in the same expression may not be defined according to the C standard, because you are modifying and accessing it non-sequentially.

pedroserer
Автор

func(a++, a++) results in undefined behaviour in C/C++.

volchikbratik
Автор

The answer depends on the compiler type and its settings as evaluation of increments in C standard is not specified and varies across different compilers and depends on their settings

SebastianTysler
Автор

It's UB, funny thing is clang and gcc do the oposite things

krzysztofpara
Автор

1 1
3
a=1 is passed in func, resulting in 1 1, but not affecting original variable a.
Then a++ is evaluated twice, resulting in 3.

joehuang
Автор

Neste código, a++ é um pós-incremento, que significa que o valor de a é usado na expressão e depois incrementado. O comportamento de a++ em uma mesma expressão pode ser não definido segundo o padrão C, porque você está modificando e acessando a de forma não sequencial.

pedroserer
Автор

a++ increments the variable after the Parameter assignment. So the output will be
1 2
3

To increase before pa we have to use ++a.

McFreezer
Автор

Function parameters are not evaluated in a defined order in C

MrVitalikcool
Автор

Answer with explanation:

Answer:
2 1
3

Explanation:
Arguments are prioritized from right to left in C. This.

First j then i will be processed.

Breakdown:
In the main, a = 1

When the function is called,
j is assigned to 1.

Then a (not j) gets incremented to 2. So at this point, j = 1, a = 2

Because of the argument's right to left processing rule, now I gets assigned with current value of a. Which is 2. So at this point i = 2

Then a gets incremented to 3 (because of (a++). So at this point, i = 2, j = 1, a = 3

The print statement of the function gets executed (i = 2, j = 1)

The func returns to main then a printed with current value of 3.

Tricky huh! Like it.

saifmahmud
Автор

This code snippet explores the behavior of the post-increment operator (a++) and the order of evaluation in C. When func(a++, a++) is called, the value of a is passed and then incremented, but the order in which this happens is not guaranteed by the C language. This can result in different values being printed depending on the compiler. After the function call, a ends up being incremented twice, so it becomes 3. The exact output may vary, making this an interesting demonstration of C’s evaluation order.

Frontend-dd
Автор

Isn't this the same one from yesterday but with the humiliating bug in the code fixed?

FabsPro
Автор

I am doing js, but I think it will be 1 2
3
I am not sure if there will be errors or not.

ScriptSavvy-dwrk
Автор

I =1, j = 2 and next a = 3 all are printed

n-ethene