Implicit declaration of function dtostrf. Please advise.

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wrote this program, but compiling it gives the following warning: Implicit declaration of function dtostrf. If I ignore the warning, the display always shows zero after decimal, even if I put an arbitrary value. 

 

For example, when it should be showing 15.2, it shows 15.0.

 

Please advise. Thank you!

#include <avr/io.h>             // special function registers
#include <stdio.h>              // sprintf()
#define F_CPU 4000000           // 4MHz RC
#include <util/delay.h>         // _delay_xx()
#include "lcd.h"                // Fleury LCD library


int readADC(char channel)


{
	ADMUX = (3 << REFS0)|(1<<ADLAR)|(channel << MUX0);    // VREF=2.56V, 8-bit, PA0. PA1
	_delay_us(10);              // allow multiplexer to settle
	ADCSRA |= (1<<ADSC);        // Start Conversion
	while (ADCSRA & (1<<ADSC)); // wait for completion
	return ADCH;                // 8-bit result because we use ADLAR

}

int main(void)

{
	char channel, din[16], dout[16], finalTIN[5], finalTOUT[5], degree = 0xDF; 
	int k, m, ind = 0, oud = 0, find = 0, foud = 0;
	float tempIN, tempOUT; 
	ADCSRA = (1<<ADEN)|(7 << ADPS0);        //div128

	lcd_init(LCD_DISP_ON);
	

		
		lcd_clrscr();
		lcd_puts("READING...");
		

		
		
		while (1) {
			
		ind = 0, oud = 0, find = 0, foud = 0;
			
			for (k=1; k<11; k++)
			{
			channel = 0;
			ind = readADC(channel); 
			_delay_ms(500);
			find = find + ind;		
			}
			
			for (m=1; m<11; m++)
			{
			channel = 1;
			oud = readADC(channel);
			_delay_ms(500);
			foud = foud + oud;
			}
			
			lcd_clrscr();
			
			tempIN = find/10;
			dtostrf(tempIN, 4, 1, finalTIN);
			sprintf(din, "ROOM TEMP:%s%cC", finalTIN, degree);
			
			lcd_gotoxy(0,0);                                             
			lcd_puts(din);
			
		
			tempOUT = foud/10  //even if I write foud = 152, the output is 15.0 always.
			dtostrf(tempOUT, 4, 1, finalTOUT);		
			sprintf(dout,"AQUARIUM:%s%cC", finalTOUT, degree);    
			lcd_gotoxy(0,1);
			lcd_puts(dout);
		
		
			
	
		
	}
}

 

This topic has a solution.

Last Edited: Wed. Jun 24, 2015 - 04:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what do you think the warning means?

" Implicit declaration of function dtostrf"

 

why are you ignoring this warning?

 

were should this function be comming from?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Umm..does it mean it is not expressed directly in terms of independent variables?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The 'C' programming language requires that everything must be declared before it is used.

 

If you don't explicitly provide such a declaration, then the compiler will assume a default - or implicit - declaration.

 

The warning tells you that this has happened.

 

The way to fix it is to provide the necessary explicit declaration - in this case, by #including the appropriate header file:

 

http://www.nongnu.org/avr-libc/u... - pay attention to the note about libm.a.

 

http://www.atmel.com/webdoc/AVRL...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it means that it cannot find the function in your code.

 

so either you have forgotten to add a include to the file that holds the function, or the function is not publicly declared in any of the the header files that you have included.

 

thinking about this ( I might be wrong though) if the function does not exist you will also get an error message that the function does not exist besides the warning, so that would mean that the function is in your code somewere, but is not declared.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As awneil said, you must add the appropriate header file.
If you do not, it will assume that every argument is an int. This may work with some functions but it will never work with a float function.
I would ALWAYS treat "implicit declaration" warnings as a serious ERROR. If you get one, Google "dtosrtf" and you should get the appropriate library manual page.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

meslomp wrote:
it means that it cannot find the function in your code.

To be precise, it means that the compiler cannot find a declaration of the function in your code.

 

david.prentice wrote:
Google "dtosrtf" and you should get the appropriate library manual page.

Which is exactly how I found the the manual pages that I linked!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What they are all trying to say in a very round about way is that you need:

#include <stdlib.h>

at the top of that.

 

(because it's the file with the declaration of dtostrf())

 

I'll leave others to comment on whether this program even needs to use float in the first place ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you!! Thanks a bunch! You amazingly knowledgeable people made my day! After including stdlib and math headers and linking the libm.a in the GNU C library, the program works like a charm!

I had to add another modification, as well. Who knew, in C language, intiger divided by integer results in a rounded off integer! No floating point! Unless of course, you specify the two initial integers are floating points!

For example, int a=152, b=10;
float c;
c=a/b;
printf c;

Result: 15.000000

But,

float a=152, float b=10;
float c;
c=a/b;
printf c;

Result: 15.200000

Not sure why, after the modification, every 5 seconds, the LCD would go blank and initialise. That is, it completely ignores the while loop and starts all over again, until I replaced int main(void) with int main().

Is there a difference between the two?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bagho wrote:
Who knew,

Anyone who read their 'C' textbook?

 

cheeky

 

http://blog.antronics.co.uk/2011...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Haha of course! I meant it as an expression. :P

I got myself an Arduino UNO today, as sir David Prentice suggested me. Some chinese or Taiwanese made clone this is. Original one was too pricey for me right now. Hope this would work alike the original thingy.

http://www.ebay.in/itm/171791478539

Last Edited: Wed. Jun 24, 2015 - 07:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

 

bagho wrote:

Who knew,

 

Anyone who read their 'C' textbook?

 

I liked the the answer, a polite version of RTFM. Everybody who writes C should have a copy of the New Testament (K & R, second edition).

This error sometimes happens even if you've included the right header, but in the wrong order

Jerry