Handle Mixed-Type Arithmetic in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to handle mixed-type arithmetic operations in C programming. The lab covers the following steps:

Firstly, you will learn how to declare and initialize integer and floating-point variables, and print their values using the appropriate format specifiers. Then, you will explore performing arithmetic operations that combine different numeric types, demonstrating type conversion and mixed arithmetic. Finally, you will print and verify the results of these mixed-type arithmetic operations.

By the end of this lab, you will have a solid understanding of how to effectively manage and manipulate mixed-type data in C, a fundamental skill for building robust and versatile programs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435189{{"`Handle Mixed-Type Arithmetic in C`"}} c/variables -.-> lab-435189{{"`Handle Mixed-Type Arithmetic in C`"}} c/data_types -.-> lab-435189{{"`Handle Mixed-Type Arithmetic in C`"}} c/operators -.-> lab-435189{{"`Handle Mixed-Type Arithmetic in C`"}} c/math_functions -.-> lab-435189{{"`Handle Mixed-Type Arithmetic in C`"}} end

Declare Integer and Floating-Point Variables

In this step, you'll learn how to declare and initialize different types of numeric variables in C programming. We'll focus on creating integer and floating-point variables to prepare for mixed-type arithmetic operations.

First, let's create a new C source file in the project directory:

cd ~/project
nano mixed_arithmetic.c

Now, add the following code to declare variables:

#include <stdio.h>

int main() {
    // Declare integer variables
    int whole_number1 = 10;
    int whole_number2 = 5;

    // Declare floating-point variables
    float decimal_number1 = 7.5;
    float decimal_number2 = 3.2;

    // Print the declared variables
    printf("Integer variables:\n");
    printf("whole_number1 = %d\n", whole_number1);
    printf("whole_number2 = %d\n", whole_number2);

    printf("\nFloating-point variables:\n");
    printf("decimal_number1 = %f\n", decimal_number1);
    printf("decimal_number2 = %f\n", decimal_number2);

    return 0;
}

Let's compile and run the program:

gcc mixed_arithmetic.c -o mixed_arithmetic
./mixed_arithmetic

Example output:

Integer variables:
whole_number1 = 10
whole_number2 = 5

Floating-point variables:
decimal_number1 = 7.500000
decimal_number2 = 3.200000

In this code, we've demonstrated:

  • Declaring integer variables using the int type
  • Declaring floating-point variables using the float type
  • Initializing variables with specific values
  • Using printf() to print variable values with appropriate format specifiers
  • The difference between integer and floating-point number representations

Perform Arithmetic Combining Different Types

In this step, you'll learn how to perform arithmetic operations that combine different numeric types in C, demonstrating type conversion and mixed arithmetic.

Open the previous file to continue our example:

cd ~/project
nano mixed_arithmetic.c

Replace the previous main() function with the following code:

#include <stdio.h>

int main() {
    // Integer variables
    int whole_number1 = 10;
    int whole_number2 = 5;

    // Floating-point variables
    float decimal_number1 = 7.5;
    float decimal_number2 = 3.2;

    // Mixed-type arithmetic operations
    int int_result = whole_number1 + whole_number2;
    float float_result = decimal_number1 + decimal_number2;

    // Mixed-type addition (integer + float)
    float mixed_addition = whole_number1 + decimal_number1;

    // Mixed-type multiplication
    float mixed_multiplication = whole_number2 * decimal_number2;

    // Demonstrating type conversion
    printf("Integer Addition: %d + %d = %d\n", whole_number1, whole_number2, int_result);
    printf("Float Addition: %.1f + %.1f = %.1f\n", decimal_number1, decimal_number2, float_result);
    printf("Mixed Addition: %d + %.1f = %.1f\n", whole_number1, decimal_number1, mixed_addition);
    printf("Mixed Multiplication: %d * %.1f = %.1f\n", whole_number2, decimal_number2, mixed_multiplication);

    return 0;
}

Compile and run the program:

gcc mixed_arithmetic.c -o mixed_arithmetic
./mixed_arithmetic

Example output:

Integer Addition: 10 + 5 = 15
Float Addition: 7.5 + 3.2 = 10.7
Mixed Addition: 10 + 7.5 = 17.5
Mixed Multiplication: 5 * 3.2 = 16.0

Key points in this example:

  • Performing arithmetic with same-type variables (int-int, float-float)
  • Demonstrating automatic type conversion in mixed-type operations
  • C automatically converts integers to floating-point numbers when needed
  • Using format specifiers to control decimal place display

Print and Verify the Results

In this final step, you'll learn how to format and verify the results of mixed-type arithmetic operations in C, focusing on precise output and result validation.

Open the previous file to continue our example:

cd ~/project
nano mixed_arithmetic.c

Update the main() function with more detailed output and result verification:

#include <stdio.h>
#include <math.h>

int main() {
    // Integer variables
    int whole_number1 = 10;
    int whole_number2 = 5;

    // Floating-point variables
    float decimal_number1 = 7.5;
    float decimal_number2 = 3.2;

    // Mixed-type arithmetic operations
    float mixed_addition = whole_number1 + decimal_number1;
    float mixed_multiplication = whole_number2 * decimal_number2;
    float mixed_division = decimal_number1 / whole_number2;

    // Detailed result printing with formatting
    printf("Arithmetic Operation Results:\n");
    printf("1. Mixed Addition:        %d + %.1f = %.2f\n", whole_number1, decimal_number1, mixed_addition);
    printf("2. Mixed Multiplication:  %d * %.1f = %.2f\n", whole_number2, decimal_number2, mixed_multiplication);
    printf("3. Mixed Division:        %.1f / %d = %.2f\n", decimal_number1, whole_number2, mixed_division);

    // Result verification
    printf("\nResult Verification:\n");
    printf("Mixed Addition Verification:       %.2f == %.2f\n",
           mixed_addition, (float)whole_number1 + decimal_number1);
    printf("Mixed Multiplication Verification: %.2f == %.2f\n",
           mixed_multiplication, (float)whole_number2 * decimal_number2);
    printf("Mixed Division Verification:       %.2f == %.2f\n",
           mixed_division, decimal_number1 / whole_number2);

    return 0;
}

Compile and run the program:

gcc mixed_arithmetic.c -o mixed_arithmetic
./mixed_arithmetic

Example output:

Arithmetic Operation Results:
1. Mixed Addition:        10 + 7.5 = 17.50
2. Mixed Multiplication:  5 * 3.2 = 16.00
3. Mixed Division:        7.5 / 5 = 1.50

Result Verification:
Mixed Addition Verification:       17.50 == 17.50
Mixed Multiplication Verification: 16.00 == 16.00
Mixed Division Verification:       1.50 == 1.50

Key points in this example:

  • Using different format specifiers for precise output
  • Demonstrating mixed-type arithmetic with addition, multiplication, and division
  • Performing inline result verification
  • Showing type conversion in arithmetic operations

Summary

In this lab, you learned how to declare and initialize different types of numeric variables in C programming, including integer and floating-point variables. You then explored performing arithmetic operations that combine these different numeric types, demonstrating type conversion and mixed arithmetic. Finally, you printed and verified the results of these mixed-type arithmetic operations.

The key learning points from this lab include understanding the differences between integer and floating-point number representations, using appropriate format specifiers in printf() statements, and the behavior of C's type conversion and mixed arithmetic when working with variables of different numeric types.

Other C Tutorials you may like