error: conflicting types - Page 1
Forum section: electronics / general

author: arul
author_rank: Regular Contributor
timestamp: November 16, 2018, 03:27:08 pm
content: Hi,I create a one function.And called that function in main function likefunction_name(variable). I got some error like as "error: conflicting types". But when i called that function asreturn_type function_name(data_type variable), i got no error.Please let me know anyone.

author: sokoloff
author_rank: Super Contributor
timestamp: November 16, 2018, 03:32:49 pm
content: Add a line before main:void UART_initialize(); // Or whatever the function name was; I'm not going back to extract it from the *image* you postedto declare (but not define) the function.

author: sokoloff
author_rank: Super Contributor
timestamp: November 16, 2018, 03:33:43 pm
content: Or put the main function at the bottom of the file rather than the top. That's a fairly typical pattern as well.

author: mfro
author_rank: Regular Contributor
timestamp: November 16, 2018, 03:40:32 pm
content: Quote from: arul on November 16, 2018, 03:27:08 pmActually, you got a warning *and* an error. Which both should have told you what you have done wrong.Your main() (where you try to call UART_initialize()) is seen by the compiler *before* the called function which means that the called function and its return type is unknown at that place (you got a warning regarding that). C can deal with that, but will assume that the function will return an int by default. Later on, the compiler found the real function declaration, but you declared a void return type which generated the error regarding conflicting types.Either locate your UART_initialize() function before main() or add a function prototype there.QuoteCan you rephrase that? I don't get it.

author: sokoloff
author_rank: Super Contributor
timestamp: November 16, 2018, 03:48:04 pm
content: Agreeing with and expanding on mfro's answer: I recommend to developers that they enable whatever setting on their platform is "treat warnings as errors" and get into the habit of writing code that compiles without warnings.There are a few spurious warnings for sure, but in general, you'll save time and have more predictable progress if you treat warnings as errors and learn how to let the compiler know "yeah, don't worry; I got this" on the occasions you need to.

author: rstofer
author_rank: Super Contributor
timestamp: November 16, 2018, 03:52:21 pm
content: You need to READ the error messages, carefully...The first one (line 12), a warning about an implicit declaration means that, at the time the compiler encountered the line of code, the function had not been declared. OK, it isn't declared and it needs to be. Where is my declaration? Why isn't it declared before the compiler encountered the line of code? Scanning the file from top to bottom, I can see that my function hasn't been defined before it is used. Hm... Maybe if I move the function to the top? Maybe I should read up on function prototypes (but that takes a little more experience). BTW, this is a common problem for new C programmers. My function is right there! It's in the file, right there! But it needs to be declared BEFORE it is called. Separate files require .h files to provide prototypes. That's coming up next!Anyway, the compiler created a dummy declaration that had no return type. Just something to fill in the symbol table.Then when the compiler encountered the declaration returning 'void' (line 30), there was a conflict in return types.Modern compilers do give pretty good diagnostics.

author: sokoloff
author_rank: Super Contributor
timestamp: November 16, 2018, 03:59:24 pm
content: Quote from: rstofer on November 16, 2018, 03:52:21 pmMinor nit: the compiler created a dummy declaration with return type of int. (Doesn't change the message at all, which is spot-on.)

author: rstofer
author_rank: Super Contributor
timestamp: November 16, 2018, 05:40:44 pm
content: Quote from: sokoloff on November 16, 2018, 03:59:24 pmYes, it does!In the grand old days, we talked about 'top down' programming. It makes every bit of sense to put main() at the top of the file because it is the 'top' in 'top down'. By extension, lesser functions would be further down until you reached the lowest level function at the bottom of the file (assuming all code in one big file).It may make sense but that's not the way it works in C. We can sort of emulate the 'top down' style by using function prototypes above main() while leaving the actual code in some lower position in the file.This is a fairly decent description:https://en.wikipedia.org/wiki/Function_prototype

author: arul
author_rank: Regular Contributor
timestamp: November 17, 2018, 05:58:59 am
content: Thanks to all.Before that main function, i put my user defined function.After am successfully compiled.