Practical Examples of C Data Types: Calculating Total Costs
Discover real-life examples of using various data types in C to calculate and output the total cost of multiple items. Learn how to effectively implement different data types in your programs to manage numerical values and improve your code's functionality.
C Data Types Examples
Real-Life Example
Here's a real-life example of using different data types to calculate and output the total cost of a number of items:
Example
// Create variables of different data types
int items = 50;
float cost_per_item = 9.99;
float total_cost = items * cost_per_item;
char currency = '$';
// Print variables
printf("Number of items: %d\n", items);
printf("Cost per item: %.2f %c\n", cost_per_item, currency);
printf("Total cost = %.2f %c\n", total_cost, currency);
Output Example
Output
Number of items: 50
Cost per item: 9.99 $
Total cost = 499.50 $
This example demonstrates how to use different data types to perform calculations and display the results in a clear format.