user11300: I have a project which uses a DS18B20 temperature sensor and an LCD with an I2C interface on it. I have connected the sensor to my micro-controller (the DS18B20 has a 12-bit digital output) to the ATmega16 PD17, but as I try to print my values on the terminal the sensor output a really high value in decimal. Here is part of the code (and yes everything is working fine, this is a home-made Arduino):
DDRD &= 0x00;
PORTD |=  0x00;    
ds18b20_init(&PORTD, 20, 30, DS18B20_12BIT_RES); 
int temperature_sensor_output =  (float)ds18b20_temperature(&PORTD);
printf("Temperature: %d  ", temperature_sensor_output);
delay_ms(500);

ds18b20_init is the Dallas function for initializing the module. ds18b20_temperature returns a float value of the temperature
From this code it just prints:

Temperature: 51603

If I change %d to %f it prints: 

Temperature: without any value

I do not understand where I may have missed some things.

user3917: Without more detail about the system, I won't understand what printf() does; in non-embedded systems,  printf() writes to stdout by default, but Arduino systems typically don't use such a device.
That aside, %d is the proper format code for printing an integer value, and %f is proper for a float or double.  If you attempt to print an integer value like temperature_sensor_output under a %f format code, either the compiler will complain or the integer pattern of bits (plus perhaps two additional random bytes) will be interpreted as if it is a float or double pattern of bits.  No type cast occurs within printf().  If you want to use a %f format code, pair it up with a float-type variable.
If, as you say, ds18b20_temperature() returns a float, then the type cast  (float)ds18b20_temperature(&PORTD) will have no effect.  Generally, casts convert data from one form to another, or in some cases declare that data has a given form.  Going from float to float doesn't change anything. 
In a C statement like a=b, if b has a type that's different from that of a, the b value will be cast to the type of a.  Thus, the float value on the right hand side of 
int temperature_sensor_output =  (float)ds18b20_temperature(&PORTD);

will be converted to an integer value and stored into the integer variable temperature_sensor_output.

11300: <p>I have a project which uses a <a href="https://www.maximintegrated.com/en/products/analog/sensors-and-sensor-interface/DS18B20.html" rel="nofollow noreferrer">DS18B20</a> temperature sensor and an LCD with an I2C interface on it. I have connected the sensor to my micro-controller (the DS18B20 has a 12-bit digital output) to the ATmega16 PD17, but as I try to print my values on the terminal the sensor output a really high value in decimal. Here is part of the code (and yes everything is working fine, this is a home-made Arduino):</p>

<pre><code>DDRD &amp;= 0x00;
PORTD |=  0x00;    
ds18b20_init(&amp;PORTD, 20, 30, DS18B20_12BIT_RES); 
int temperature_sensor_output =  (float)ds18b20_temperature(&amp;PORTD);
printf("Temperature: %d  ", temperature_sensor_output);
delay_ms(500);
</code></pre>

<p>ds18b20_init is the Dallas function for initializing the module. ds18b20_temperature returns a float value of the temperature</p>

<p>From this code it just prints:</p>

<blockquote>
  <p>Temperature: 51603</p>
</blockquote>

<p>If I change %d to %f it prints: </p>

<blockquote>
  <p>Temperature: without any value</p>
</blockquote>

<p>I do not understand where I may have missed some things.</p>
 3917: <p>Without more detail about the system, I won't understand what <code>printf()</code> does; in non-embedded systems,  <code>printf()</code> writes to <code>stdout</code> by default, but Arduino systems typically don't use such a device.</p>

<p>That aside, <code>%d</code> is the proper format code for printing an integer value, and <code>%f</code> is proper for a float or double.  If you attempt to print an integer value like <code>temperature_sensor_output</code> under a <code>%f</code> format code, either the compiler will complain or the integer pattern of bits (plus perhaps two additional random bytes) will be interpreted as if it is a float or double pattern of bits.  No type cast occurs within <code>printf()</code>.  If you want to use a <code>%f</code> format code, pair it up with a float-type variable.</p>

<p>If, as you say, <code>ds18b20_temperature()</code> returns a float, then the type cast  <code>(float)ds18b20_temperature(&amp;PORTD)</code> will have no effect.  Generally, casts convert data from one form to another, or in some cases declare that data has a given form.  Going from float to float doesn't change anything. </p>

<p>In a C statement like <code>a=b</code>, if <code>b</code> has a type that's different from that of <code>a</code>, the <code>b</code> value will be cast to the type of <code>a</code>.  Thus, the float value on the right hand side of </p>

<pre><code>int temperature_sensor_output =  (float)ds18b20_temperature(&amp;PORTD);
</code></pre>

<p>will be converted to an integer value and stored into the integer variable <code>temperature_sensor_output</code>.</p>
