2. Write a simple program that converts one given data type to another using auto conversion and casting. Take the values from standard input.

C Program Code



#include  // Required for standard input/output functions like printf and scanf

int main() {
// Declare variables for different data types
int intValue;
float floatValue;
double doubleValue;

// --- Input from User ---
printf("Enter an integer value: ");
if (scanf("%d", &intValue) != 1) {
    printf("Invalid input. Please enter an integer.\n");
    return 1; // Indicate an error
}

printf("Enter a float value: ");
if (scanf("%f", &floatValue) != 1) {
    printf("Invalid input. Please enter a float.\n");
    return 1; // Indicate an error
}

printf("Enter a double value: ");
if (scanf("%lf", &doubleValue) != 1) { // Use %lf for double with scanf
    printf("Invalid input. Please enter a double.\n");
    return 1; // Indicate an error
}

printf("\n--- Automatic Type Conversion (Implicit Conversion) ---\n");
// int to float/double
float intToFloat = intValue; // intValue is automatically converted to float
printf("Integer %d implicitly converted to float: %.2f\n", intValue, intToFloat);

double intToDouble = intValue; // intValue is automatically converted to double
printf("Integer %d implicitly converted to double: %.2lf\n", intValue, intToDouble);

// float to double
double floatToDouble = floatValue; // floatValue is automatically converted to double
printf("Float %.2f implicitly converted to double: %.2lf\n", floatValue, floatToDouble);

// Note: Implicit conversion from larger to smaller type (e.g., float to int)
// might lose data or precision and often results in a warning.
// int floatToInt = floatValue; // This would be an implicit conversion with potential data loss
// printf("Float %.2f implicitly converted to int (potential data loss): %d\n", floatValue, floatToInt);


printf("\n--- Type Casting (Explicit Conversion) ---\n");
// float to int
int floatToIntCast = (int)floatValue; // floatValue is explicitly cast to int
printf("Float %.2f explicitly cast to int: %d (decimal part truncated)\n", floatValue, floatToIntCast);

// double to int
int doubleToIntCast = (int)doubleValue; // doubleValue is explicitly cast to int
printf("Double %.2lf explicitly cast to int: %d (decimal part truncated)\n", doubleValue, doubleToIntCast);

// double to float
float doubleToFloatCast = (float)doubleValue; // doubleValue is explicitly cast to float
printf("Double %.2lf explicitly cast to float: %.2f (potential precision loss)\n", doubleValue, doubleToFloatCast);

// int to char
char intToCharCast = (char)intValue; // intValue is explicitly cast to char (ASCII value)
printf("Integer %d explicitly cast to char: '%c' (ASCII value)\n", intValue, intToCharCast);

// char to int
char charVal = 'A';
int charToIntCast = (int)charVal; // charVal is explicitly cast to int (ASCII value)
printf("Character '%c' explicitly cast to int: %d (ASCII value)\n", charVal, charToIntCast);


// Example of arithmetic operation with mixed types leading to implicit conversion
printf("\n--- Mixed Type Arithmetic ---\n");
int num1 = 10;
float num2 = 3.5f;
double resultMixed = num1 + num2; // int num1 is implicitly converted to float, then result is float
printf("int + float (10 + 3.5f): %.2lf (result is double due to type promotion)\n", resultMixed);

// To force integer division even with float
int resultIntDiv = (int)(num1 / num2); // num1 is promoted to float, then division, then cast back to int
printf("int / float (10 / 3.5f) explicitly cast to int: %d\n", resultIntDiv);


return 0; // Indicate successful execution

}

Compilation and Execution (Command Prompt)



Navigate to your project folder
cd ProgrammingProjects/C_Programming/Week1

Compile the program
gcc type_conversion.c -o type_conversion

Run the program
./type_conversion

Sample Output (for input `10`, `3.14`, `123.456`)



Enter an integer value: 10
Enter a float value: 3.14
Enter a double value: 123.456

--- Automatic Type Conversion (Implicit Conversion) ---
Integer 10 implicitly converted to float: 10.00
Integer 10 implicitly converted to double: 10.00
Float 3.14 implicitly converted to double: 3.14

--- Type Casting (Explicit Conversion) ---
Float 3.14 explicitly cast to int: 3 (decimal part truncated)
Double 123.46 explicitly cast to int: 123 (decimal part truncated)
Double 123.46 explicitly cast to float: 123.46 (potential precision loss)
Integer 10 explicitly cast to char: '\n' (ASCII value)
Character 'A' explicitly cast to int: 65 (ASCII value)

--- Mixed Type Arithmetic ---
int + float (10 + 3.5f): 13.50 (result is double due to type promotion)
int / float (10 / 3.5f) explicitly cast to int: 2