QP School

Full Version: Type casting with short int in C
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

#include <stdio.h>

int main() {
    int num1 = 200;
    int num2 = 100;

    // Type cast integers to short int before addition
    short int result = (short int)(num1 + num2);
    printf("Result: %d\n", result);

    return 0;
}

Explanation: 

In this example, we have two integers num1 and num2. Before performing the addition, we type-cast the result of num1 + num2 to a short int using (short int) to fit the result within the range of a short int.