user2998260
user2998260

Reputation: 3

C Programming: warning:conflicting types?

I'm getting an error when I compile my main.c The error says:

functions.c:27:6: warning: conflicting types for 'secondfunction' [enabled by default]
functions.c:23:2: note previous implicit declaration of 'secondfunction' was here

(My program has a file called main.c, header.h, and functions.c)

main.c:

#include "header.h" /*Includes the header file*/

int main(void) /*Starts the main function.*/
{
    course();
    firstfunction(filename,fileextension,fp,MAX_SIZE);
    /*
    thirdfunction();
    fourthfunction();
    fifthfunction();
    sixthfunction();
    */
    return 0;
} /*Ends the main function.*/

header.h:

#include <stdio.h>
#include <string.h>
#include "functions.c"

#define MAX_SIZE 50

struct records
    {
    double euid;
    char firstname[MAX_SIZE];
    char lastname[MAX_SIZE];
    float gpa;
    };

void course(void);
void firstfunction(char filename[],char fileextension[],FILE* fp,int SIZE);
void secondfunction(FILE* fp);

struct records people;
FILE* fp;
char filename[MAX_SIZE],fileextension[MAX_SIZE];

functions.c:

void course(void)
{
    printf("\n\n\n\n\nDepartment: CSCE\nCourse number: 1030\nProgram number: Homework 3\nName: xxxxxxxxxxxxxx\nEUID: xxxxxxxxxxxxx\nEmail: xxxxxxxxx\n\n\n\n\n");
}

void firstfunction(char filename[],char fileextension[],FILE* fp,int SIZE)
{
    printf("Please enter the file name (ex:filename)\n"); /*Asks for the name and the extension*/
    scanf("%s",filename);

    printf("Please enter the file extension (ex:.txt)\n");
    scanf("%s",fileextension);

    strcat(filename,fileextension); /*Puts them together with strcat*/
    printf("%s\n",filename);

    fp=fopen(filename,"r");
    secondfunction(fp);
    fclose(fp);
} /*End of the first function*/

void secondfunction(FILE* fp)
{

} /*End of the second function.*/

Thanks for the help guys!! I don't think it's the variables being passed to the function I think it's the fact that I'm calling a function from within a function. How do I fix this?

Upvotes: 0

Views: 2129

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263237

You declare both functions in header.h, which is great.

But in functions.c, when you call secondfunction from within firstfunction, no declaration or definition of secondfunction is yet visible.

You should add

#include "header.h"

to the top of functions.c. You should also add include guards to header.h so that you don't run into problems if it's included more than once:

#ifndef H_HEADER
#define H_HEADER
/* contents of header.h */
#endif

And as Mario's answer points out, you shouldn't include functions.c from anywhere. Except in unusual cases, only .h files should be #included.

Each function should be declared in a .h file, and defined in a .c file.

Each file that needs to refer to a given function should have a #include directive for the header that declares it.

(main needn't be declared in a header, since it's not normally called.)

As for the confusing "conflicting types" message, that's because of an obsolescent feature of C. In older versions of the language (prior to the 1999 standard), a call to an undeclared function would result in an implicit declaration of that function, and the compiler would assume that it returns a result of type int. Even if you're using a compiler that still does this (many still do by default), you should ensure that each function you call has a visible declaration.

Upvotes: 0

Mario
Mario

Reputation: 66

remove this line #include "functions.c" from header.h and include header.h in functions.c

Upvotes: 2

zubergu
zubergu

Reputation: 3706

You make use of secondfunction() in functions.c before you declare or define this function. So compiler treats her as it usually does with unknown functions: assumes it takes variable number of int arguments and returns int.

When it next finds your definition - it yells about differences because your function returns void and takes FILE* as an argument.

Move your secondfunction() definition before firstfunction() and it will work. But what you should really do is read on what to put in header files. In your case put all functions declarations in header file, and problem will be gone, if you include header file on top of your functions.c

Upvotes: 0

Related Questions