Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator in C

Tags:

c

Why this program is giving unexpected numbers(ex: 2040866504, -786655336)?

#include <stdio.h> 
int main()
{
     int test = 0;
     float fvalue = 3.111f;
     printf("%d", test? fvalue : 0);

     return 0;
}

Why it is printing unexpected numbers instead of 0? should it supposed to do implicit typecast? This program is for learning purpose nothing serious.

like image 787
Tonmoy Avatar asked Sep 28 '13 05:09

Tonmoy


People also ask

What is ternary operator with example?

A ternary operator lets you assign one value to the variable if the condition is true, and another value if the condition is false. The if else block example from above could now be written as shown in the example below. var num = 4, msg = ""; msg = (num === 4) ?

Which is the ternary operator in?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

What is the syntax for C ternary operator from the list?

int a = 10, b = 20, c; if (a < b) { c = a; } else { c = b; } printf("%d", c); This example takes more than 10 lines, but that isn't necessary. You can write the above program in just 3 lines of code using a ternary operator.

Why is ternary operator important in C++?

The important use of a ternary operator decreases the number of lines of code and increases the performance of the application. Most of the research articles claimed that the expression result is faster than a statement result (conventional if-else condition). Ternary operator contains 3 expressions; Expression1, Expression2, and Expression3.

What is a ternary operator in JavaScript?

The testCondition is a boolean expression that results in either true or false. If the condition is The ternary operator takes 3 operands (condition, expression1 and expression2). Hence, the name ternary operator. In the above example, we have used a ternary operator that checks whether a user can vote or not based on the input value. Here,

How to use the ternary operator to return a number?

Then, the ternary operator is used to check if number is even or not. Since, 2 is even, the expression ( number % 2 == 0) returns true. We can also use ternary operator to return numbers, strings and characters. Instead of storing the return value in variable isEven, we can directly print the value returned by ternary operator as,

How to compare syntax of ternary operator with example?

If we compare syntax of ternary operator with above example, then − First, expression a > b is evaluated, which evaluates to Boolean false as value of variable 'a' is smaller than value of variable 'b'. Hence value of variable 'b' i.e. '20' is returned which becomes final result and gets assigned to variable 'max'.


2 Answers

Most likely, your platform passes floating point values in a floating point register and integer values in a different register (or on the stack). You told printf to look for an integer, so it's looking in the register integers are passed in (or on the stack). But you passed it a float, so the zero was placed in the floating point register that printf never looked at.

The ternary operator follows language rules to decide the type of its result. It can't sometimes be an integer and sometimes be a float. Those could be different sizes, stored in different places, and so on, which would make it impossible to generate sane code to handle both possible result types.

This is a guess. Perhaps something completely different is happening. Undefined behavior is undefined for a reason. These kinds of things can be impossible to predict and very difficult to understand without lots of experience and knowledge of platform and compiler details. Never let someone convince you that UB is okay or safe because it seems to work on their system.

like image 58
David Schwartz Avatar answered Sep 20 '22 05:09

David Schwartz


Because you are using %d for printing a float value. Use %f. Using %d to print a float value invokes undefined behavior.


EDIT: Regarding OP's comments;

Why it is printing random numbers instead of 0?

When you compile this code, compiler should give you a warning:

[Warning] format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat]

This warning is self explanatory that this line of code is invoking an undefined behavior. This is because, the conversion specification %d specifies that printf is to convert an int value from binary to a string of decimal digits, while %f does the same for a float value. On passing the fvalue compiler know that it is of float type but on the other hand it sees that printf expects an argument of type int. In such cases, sometimes it does what you expect, sometimes it does what I expect. Sometimes it does what nobody expects (Nice Comment by David Schwartz).
See the test cases 1 and 2. It is working fine with %f.

should it supposed to do implicit typecast?

No.

like image 36
haccks Avatar answered Sep 22 '22 05:09

haccks