How to import functions from another C file?

Asked

Viewed 8,881 times

5

In Java if we create other classes, for example: classe1 and classe2, if I want to call classe1 within the classe2 (because they are in separate files) I use import classe1.

I made this code in C, it’s an old-fashioned game that I have to utilize some computational intelligence resources. I always like to separate my codes with business rules (rg) and ui (user interface) ie, "words, questions" go in a "file" and the "code" goes in another "file".

How do I call another file inside another C file, like I mentioned in Java? To use other file functions...

Código em C

  • 1

    C has no classes. No hierarchy above. No questions there. No business rule described. All that is described is meaningless and in that form there is no way to answer that question. You need to put something more contextualized, really show what you’re doing, and what problem you want to solve.

  • It is because I am now starting the code... The code is to make a game of worth using Computational Intelligence. I know that C has no classes, I say call the "files. c" in the file "main. c". In the case the question there is " //PANEL - MENU printf(" nEscolha:"); printf(" n(1)- Player x Player"); printf(" n(2)- Player x PC"); printf(" n(3)- PC x PC");" ... I don’t want to put the scanf in main. c, I want to create another "file. c" put the code in, only the main. c will call this file to display the choices.

  • I think it’s best to post a question when you have something more concrete and clear, in the current form there is no way to answer the question.

  • I improved the question @bigown, understand now?

  • 1

    Improved. Prefer to put the code to make it easier for others who will answer you. Images are not useful. In fact it would be good to put the code of the other file also, indicating this clearly.

  • Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to do this. You would help the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site.

Show 1 more comment

3 answers

6

Unlike Java in C, the declaration is usually separated from the definition of functions. It is common for the definition to go in a file .c and the statement stays in a file called header .h. But that’s not mandatory.

Contrary to what many people believe the prototype (function statement) is necessary only when it will use in external file - the intention here - or when there are cyclical references between functions. The way used in the question is totally disposable if changing the order of functions.

When going to use other file functions the common is to use the #include that .h to include definitions and the compiler to know how to treat that.

All codes in general .c need to be compiled. This is done manually. They also need to be linked together somehow, whether during the compilation or later, when you already have something pre-compiled.

The question does not give enough information to give a more detailed answer, but it would be basically what you have done with language components:

#include <seuOutroArquivo.h>

Of course, you don’t always have to do this. It depends on the context, the specific case you’re making. To decide the right way you have to learn the whole, understand the philosophy of language.

In this file would have something like that (I’ll guess since the question does not help):

int algumaRegraDeNegocio(int);

And then in a file .c would have something like this:

int algumaRegraDeNegocio(int valor) {
    //faz alguma coisa aqui
    return 1;
}

I put in the Github for future reference.

Obviously this is a huge simplification.

To better understand the functioning of header.

Behold: What is the difference between declaration and definition?

Note that there is a very large distance between Java and C. If you try to play in C what you did in Java will go very wrong. I say that because it sounds like you’re trying to use one another’s philosophy.

0

Here is an example of the example file. c que Voce ira compile (/home/Andre/Workspace/c/example. c)

#include </home/andre/workspace/c/andruida.c>
int main(){
printa(100);
printo();
return 0;
}

Below is a file described in the above file include (/home/Andre/Workspace/c/andruida. c)

#include <stdio.h>

int printa(int l){
    while (l--)printf("PRINTA\t");
    printf("\n");
    return 0;
}

int printo(){
    printf("\n\n\nPRINTO\n");
    return 0;
}

How I use gcc in shell:

gcc -o exemplo exemplo.c

Note that I didn’t even have to mention andruida. h

  • This example is not good because in general it does not put executable code in files . h.

0

Simple example of file organization of a project in the C language. (Function names are only illustrative, not a scheme to be followed!)

File .... /game/main. c

#include "rg/rg.h" // regras de negocios
#include "ui/ui.h" // interface com usuario

// constantes visiveis apenas neste arquivo fonte
enum { N_THINGS = 100, N_OTHER_THINGS = 42 };

// funcoes locais a este arquivo fonte
static void funcLocal_01(void);
static void funcLocal_02(void);

// variaveis globais visiveis apenas neste arquivo fonte
static int nThings;
static char nameThings[N_THINGS];

int main(void)
{
   nThings = 10;
   abcd = 0; // esta' no "modulo" rg

   if (xyz == 123) // esta' no "modulo" ui
   {
      // bla bla bla
      // bla bla bla
   }

   func_ui_001();
   func_rg_xyz();

   funcLocal_01();

   func_rg_xxx();
   func_ui_zzz();

   funcLocal_02();
}

void funcLocal_01(void)
{
   // bla bla bla
   // bla bla bla
}

void funcLocal_02(void)
{
   // bla bla bla
   // bla bla bla
}

File .... /game/rg/rg. h

extern int abcd;

extern void func_rg_xyz(void);
extern void func_rg_xxx(void);

File .... /game/rg/rg. c

// variavel exportada
int abcd = 1;

// variaveis locais a este arquivo fonte
static int xyz;
static char array[54];

// funcoes locais este arquivo fonte
static void func1(void);
static void func2(void);

void func_rg_xyz(void)
{
   // bla bla bla
   func1();
   // bla bla bla
}

void func_rg_xxx(void)
{
   // bla bla bla
   func2();
   // bla bla bla
}

void func1(void)
{
   // bla bla bla
   // bla bla bla
}

void func2(void)
{
   // bla bla bla
   // bla bla bla
}

File .... /game/ui/ui. h

extern int xyz;

extern void func_ui_001(void);
extern void func_ui_zzz(void);

File .... /game/ui/ui. c

int xyz = 49;

void func_ui_001(void)
{
   // bla bla bla
   // bla bla bla
}

void func_ui_zzz(void)
{
   // bla bla bla
   // bla bla bla
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.