Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This consists of two Visual Studio Code files, in C. 

This one, called qdriver.c

<pre>#include <stdio.h>  // printf

void temperature_convertor(float fahrenheit);

int main(void)
{
	// test case 1
	temperature_convertor(0);
	printf("\n");

	// test case 2
	temperature_convertor(20);
	printf("\n");

	// test case 3
	temperature_convertor(300);
	printf("\n");

	// test case 4
	temperature_convertor(-20);
	printf("\n");

	// test case 5
	temperature_convertor(137);
	printf("\n");

	// test case 6
	temperature_convertor(-300);
	printf("\n");

	return 0;
}


linked to this one, called q.c

#include <stdio.h>  
float fahrenheit;
void temperature_convertor(float fahrenheit)
{
	float C = fahrenheit - 32;
    float Ce = C * 5;
    float celsius =  Ce/9;
    float kelvin =  celsius + 273.15;
    printf("fahrenheit celsius  kelvin \n%f %f %f", fahrenheit, celsius, kelvin);
}


When I try running this, it comes out the error
conversion from ‘double’ to ‘float’ may change value
for the line
float kelvin =  celsius + 273.15;
. How do i fix this?

What I have tried:

I'm kind of lost, since I'm quite new to coding
Posted
Updated 25-Sep-21 4:52am

273.15 is a double value - so when you add it to a float value you get a double value (it always ends up with the "highest precision" type, so the float variable celcius is promoted to a double before the addition).
So when you try to store the result, you are storing a double value in a float variable, and the system is warning you that this could result in the loss of data.

The simplest fix is to use a float constant value instead by adding a "F" suffix:
C
float kelvin =  celsius + 273.15f;
 
Share this answer
 
v2
Comments
CPallini 25-Sep-21 10:49am    
5.
A simple alternative is using doubles instead of floats every where in your program. Don't use floats unless you have scarce resources available (e.g. cross-compiling for a microcontroller).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900