Home > Back-end >  How to divide my code into 2 c files where I would process data in one of them, and run everything b
How to divide my code into 2 c files where I would process data in one of them, and run everything b

Time:06-13

This program as you can see is kind of "Student Management System". I couldn't divide my code into 2 c files as "main.c" and "process.c". I need to process the data in the "process.s" and run the code by makefile. Can anyone give me a hint how to divide them and how to create a header file for them. I left main() in the main.c and put everything else in the process.c. In the header file of main.h, I initialized the functions and struct, but it seems to be incorrect.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct Person{
    char name[20];
    char phone[15];
    struct Person* next;
}* head;

void Enroll(char* name, char* phone){
    struct Person* person = (struct Person*) malloc(sizeof(struct Person));
    strcpy(person->name, name);
    strcpy(person->phone, phone);
    person->next = NULL;

    if (head == NULL){
            head = person;
    }else{
            person->next = head;
            head = person;
    }
}

void List(){
    struct Person* temp = head;
    printf("---List---");
    while(temp =! NULL){
            printf("%s      %s", temp->name, temp->phone);
            temp = temp->next;
    }
}

void Search(char* name){
    struct Person* temp = head;
    while(temp != NULL){
            if (strcpy(temp->name, name) != 0){
                    printf("%s      %s", temp->name, temp->phone);
                    return;
            }
            temp = temp->next;
    }
    printf("%s Could not find\n", name);
}


void Delete(char* name){
    struct Student* temp1 = head;
    struct Student* temp2 = head;
    while(temp1 != NULL){
            if (strcpy(temp1->name, name) != 0){
                    if (temp1 == temp2){
                            head = head->next;
                            free(temp1);
                    }else{
                            temp2->next = temp1->next;
                            free(temp1);
                    }
                    printf("%s Delete Success", name);
                    return;
            }
            temp2 = temp1;
            temp1 = temp1->next;
    }
    printf("%s Could not find", name);
}

int main(){
    head = NULL;
    char name[20];
    char phone[15];
    int select;

    printf("Welcome to the Enrollment Management System!\n");
    printf("[1] Enroll      [2] List        [3] Search      [4] Delete      [5]Quit\nSelection: ");
    scanf("%d", &select);
    while(select != 5){
            if (select == 1){
                    printf("Name: ");
                    scanf("%s", name);
                    printf("Phone: ");
                    scanf("%s", phone);
                    Enroll(name, phone);
            }
            if (select == 2){
                    List();
            }
            if (select == 3){
                    printf("Name: ");
                    scanf("%s", name);
                    Search(name);
            }
            if (select == 4){
                    printf("Name: ");
                    scanf("%s", name);
                    Delete(name);
            }
    }
}

CodePudding user response:

You could create a header ".h" file on the same directory and you could add the instruction on the top of the main.c like #include "mylib.h"

If the header file is included with <>, the compiler will search a determined directory path of header files, if the header file is included with the "" compiler will look in the same directory as the main.c file.

CodePudding user response:

The answer posted by Ugur A is correct. When your compiler preprocesses your c file, it normally just merges all the code of the .h to the c file, replacing the #include statement with it. So that's how it happens.

In addition to that, if you wanted to split your code into two c files, you can do it like-

foo.c:

int foo() {
    // do something
}

foobar.c:

int foo();

int main() {
    foo();
}

Particularly, you'd have to declare all the functions in your file containing main().

And when compiling-

gcc foo.c foobar.c
  •  Tags:  
  • c
  • Related