C Programming - .h and .c file Question

Started by
3 comments, last by ultramailman 10 years, 1 month ago

Hello,

So I am just messing around using separate files to keep the main.c file not so cluttered up. It work's but I thought that it wouldn't. Maybe I am misunderstanding how it works, but for some reason, I did not #include the "Employee_def.c" file, yet the main.c file still knows what that function is, I thought I'd have to #include the Employee_def.c file.

Also, I am getting a warning in the main.c file on the line I call the Employee_Initializer function that states "Implicit declaration of function "Employee_Initializer" is invalid in C99". Still works, but not sure what that warning is trying to say.

ANY help with an explanation or protips would be greatly appreciated! biggrin.png

main.c


#include <stdio.h>
#include "Employee.h"

////////////////////////////////////////////////////////////////////
//
//
//   MAIN
//
//
////////////////////////////////////////////////////////////////////

int main(int argc, const char * argv[])
{
    Employee emp;
    Employee * emp_ptr = &emp;
    Employee_Initializer(emp_ptr);
    
    return 0;
}

Employee.h


typedef struct {
    int age;
    int id;
    char sex;
    float pay_rate;
    float vac_hours;
    float sick_hours;
    float personal_hours;
    char first_name[20];
    char last_name[20];
    char job_title[25];
    char address[55];
} Employee;

Employee_def.c


#include <stdio.h>
#include "Employee.h"

// Employee Inializer
void Employee_Initializer(Employee * emp) {
    emp->pay_rate = 10.25;
    emp->vac_hours = 0;
    emp->sick_hours = 0;
    emp->personal_hours = 0;
    emp->age = 0;
    
    do {
        printf("Enter New Employee's Age: ");
        scanf("%d", &emp->age);
    } while(emp->age < 18);
    
    printf("Age: %d\n", emp->age);
}
Advertisement

The warning is because main.c doesn't know about Employee_Initializer(Employee*). Add "void Employee_Initializer(Employee*);" to the end of Employee.h and it should be fixed.

Older versions of C automatically assumed any undeclared function you call would exist in some other translation unit (roughly, any other .c file you link in to your program), though for various reasons this can be dangerous (the function may not have the signature or calling convention that the compiler assumed it would have). That's why it works: it's terrible, horrible, dangerous practice, but it's legal in ANSI C.

C99 deprecated this feature, hence the warning. It still works in many compilers with loose warning levels for backwards-compatibility, but it was deprecated for a reason. Don't rely on it.

Sean Middleditch – Game Systems Engineer – Join my team!

1) Maybe I am misunderstanding how it works, but for some reason, I did not #include the "Employee_def.c" file, yet the main.c file still knows what that function is, I thought I'd have to #include the Employee_def.c file

The compiler works by first compiling the individual source(.c) files. It creates intermediate object(.o) files. Then the linker ties together all the object(.o) files to create the final executable. So when you #include "Employee.h" you are telling the compiler/linker "this source file needs to be linked with the .o file generated by Employee.c".

2)Also, I am getting a warning in the main.c file on the line I call the Employee_Initializer function that states "Implicit declaration of function "Employee_Initializer" is invalid in C99". Still works, but not sure what that warning is trying to say.

You didn't declare the function in your header file. The compiler allows it, but it's very dangerous!

Protip: Include Guards are important, use them!


So when you #include "Employee.h" you are telling the compiler/linker "this source file needs to be linked with the .o file generated by Employee.c".

Not exactly. #include is literally copying the contents of Employee.h and pasting it in the file that invokes it, it doesn't tell the compiler or the linker anything.

Using your example, your main.c should be:


#include <stdio.h>
 
// forward declaration, so that the compiler is informed that this function is defined somewhere.
void Employee_Initializer(Employee * emp);
 
int main(int argc, const char * argv[])
{
    Employee emp;
    Employee * emp_ptr = &emp;
    // since the function is declared up there, it is ok to call it.
    Employee_Initializer(emp_ptr);
 
    return 0;
}

When you compile, you compile each .c file separately, producing a .o file.

When compiling main.o, it will see that forward declaration, so it will know that Employee_Initializer is either not defined yet, or it is in another .o file, and it won't throw any errors at you.

(for example: cc -c main.c -o main.o -std=c99)

Then you compile Employee_def.c and get Employee_def.o.

(for example: cc -c Employee_def.c -o Employee_def.o -std=c99)

Then you link main.o and Employee_def.o together, and the linker should be able to find Employee_Initializer in Employee_def.o.

(for example: cc main.o Employee_def.o a.out)

But what if you have more than one forward declaration? What if you have hundreds of lines of these forward declarations? It's tedious to copy and paste that to every file that needs them, so that's why #include is used. It allows you to paste as much info as you want from another file, without making your own file looking cluttered with forward declarations.

This topic is closed to new replies.

Advertisement