From the course: Advanced C Programming: Optimize Performance and Efficiency

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Solution: Specifying a cast

Solution: Specifying a cast

(upbeat music) - [Instructor] For my solution I specified an integer variable a at line five and fetched input at lines seven and eight. In line nine, I use the printf statement with the %.1f placeholder that displays a floating point value to one digit after the decimal. Variable a is typecast in the printf statement to a float again at line nine. If you remove the float the result is unpredictable. On my system, I saw 0.0, although that's not a guarantee. The only way to display an integer by using a floating point format is to cast the variable as shown here at line nine. Your code can be subtly different of course but casting an int variable as a float is the proper solution.

Contents