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 - C Tutorial
From the course: Advanced C Programming: Optimize Performance and Efficiency
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.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Set up an external variable4m 16s
-
(Locked)
Typecast variables4m 46s
-
(Locked)
Challenge: Specifying a cast26s
-
(Locked)
Solution: Specifying a cast51s
-
(Locked)
Take advantage of static variables3m 20s
-
(Locked)
Challenge: Setting up a static variable23s
-
(Locked)
Solution: Setting up a static variable54s
-
(Locked)
-
-
-