QP School

Full Version: User input and short int conversion 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() {
    short int userInput;

    // Prompt the user to enter a short int value
    printf("Enter a short int value: ");
    scanf("%hd", &userInput);

    // Perform a simple calculation and display the result
    short int result = userInput * 2;
    printf("Result: %d\n", result);

    return 0;
}

Explanation: 

In this example, the user is prompted to enter a short int value. The scanf function is used to read the user input as a short int, and it is stored in the userInput variable. Then, we perform a simple calculation (multiplication by 2) using the short int value and display the result using printf.