Cannot figure out this bug "error: conflicting types"

This is a discussion on Cannot figure out this bug "error: conflicting types" within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to work on a "simple" problem from K&R book. The excercise is as under: Rewrite the ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    Cannot figure out this bug "error: conflicting types"

    Hi,

    I am trying to work on a "simple" problem from K&R book. The excercise is as under:

    Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
    My attempt:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
     int main(void) {
    	float fahr;
    	float collector;
    	int lower = 0;
    	int upper = 300;
    	int step = 20;
    	fahr = lower;
    	while (fahr <= upper)
    	{
    		collector = FtoC (fahr); 
    		printf ("\n F is %3.0f & C is %6.1f", fahr, collector);
    		fahr = fahr + step;
    	}
    	return 0;
    }
    
    float FtoC (float n)
    {
    	float c;
    	c = (5.0 / 9.0) * (n - 32.0);
    	return c;
    
    }
    As far as my knowledge goes, there isn't any bug in this code but compiler is returning this error & warning. I've no idea WHY? Kindly help.

    /src/celsius_converter.c: In function ‘main’:
    ../src/celsius_converter.c:23:3: warning: implicit declaration of function ‘FtoC’ [-Wimplicit-function-declaration]
    ../src/celsius_converter.c: At top level:
    ../src/celsius_converter.c:30:7: error: conflicting types for ‘FtoC’
    ../src/celsius_converter.c:23:15: note: previous implicit declaration of ‘FtoC’ was here
    make: *** [src/celsius_converter.o] Error 1

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you have to forward declare FtoC();

    put
    Code:
    float FtoC (float n);
    before main().

    otherwise the compiler thinks FtoC is a function that returns int.

    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Yes the above reply is correct. On a more general level, u need to either define the function before you use it or declare it before using it in main and define it after main.
    Last edited by livin; 08-27-2012 at 05:22 PM.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks! That helped & thanks for giving reasons behind it too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 08-09-2012, 01:48 PM
  2. Getting "incompatible types in assignment error."
    By mgracecar in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 06:38 PM
  3. Please help! I get a "conflicting types for" error.
    By hotshotennis in forum C Programming
    Replies: 7
    Last Post: 02-24-2012, 07:31 AM
  4. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21