-1

During compilation, when c code is converted into assembly language, does ternary operator converted into if-else block ?

anand
  • 267
  • 1
  • 3
  • 16
  • See this post: http://stackoverflow.com/questions/3565368/ternary-operator-vs-if-else – fileyfood500 May 21 '17 at 04:50
  • 2
    There are no "if-else" blocks in machine code, only conditional jumps. Ternary may compile into conditional jumps or conditional moves. `if` and `else` also may compile into conditional jumps or conditional moves. – Ben Voigt May 21 '17 at 04:50
  • No. Assembly doesn't have an if..else. – Ken White May 21 '17 at 04:50
  • 2
    Note that the ARM Thumb-2 instruction set has a construct that could be called an "if-then-else": https://community.arm.com/processors/b/blog/posts/condition-codes-3-conditional-execution-in-thumb-2 – Michael Burr May 21 '17 at 06:51
  • Did you try it with your hardware? – ThingyWotsit May 21 '17 at 07:32
  • In some cases, a compiler may produce clever code that eliminates any conditional branches, if the ternary expression can be converted into a sequence of math and/or bit oriented type instructions. – rcgldr May 21 '17 at 23:10

2 Answers2

1

Short answer: no.

Machine instructions (or assembly which, roughly speaking, is a higher level representation of machine instructions that a human can understand more easily) does not have an if/else construct. At most, there will be some form of conditional jump.

Practically, if/else constructs might initially be mapped to code which uses conditional jumps. And the working of the ternary ?: might, initially at least, be mapped the same way.

However, compilers do some fairly advanced optimisation of code. Depending on needs of the CPU, the optimiser might completely reorder or remove some blocks of code that have conditional jumps, and replace it with other code that produces the same results, using some other mechanism.

Peter
  • 35,646
  • 4
  • 32
  • 74
0

Answer is no if else block will be created in compiled c