0

i am begginer to C ,I am trying to create tow files f1.c to input numbers and a second one f2.c to calculate their square and call them from main.c and finally a header.h to collect them f1.c code :

#include <stdio.h>
#include <math.h>
#include "header.h"

void f1()
{

int n,monTableau[n],i;

printf("Enter la taiile de vecteur n = : ");
scanf("%d", &n);  

for (i=0;i<n;i++){
printf("\n Enter an la valeur i : ");
scanf("%d", &monTableau[i]);  
};

}

f2.c code :

#include <stdio.h>
#include <math.h>
#include "header.h"

void f2()
{

int n,i,monTableau[n];
float resultas[n];


for (i=0;i<n;i++){

resultas[i] = sqrt( monTableau[i] );
};

for (i=0;i<n;i++){
printf("\n la racine de i eme valeur de tableau : %f ",resultas[i]);
};
}

main.c code :

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

void main()
{

f1();
f2();

}

header.h code :

void f1();
void f2();

and the error I got when i compile it in the terminal using this command:

gcc f1.c f2.c main.c -o final

is

/usr/bin/ld: /tmp/cc4kRvmX.o: in function `f2':
f2.c:(.text+0x1a8): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
kaylum
  • 13,155
  • 2
  • 21
  • 31
  • 1
    Missing maths lib: `gcc f1.c f2.c main.c -o final -lm` – kaylum Mar 06 '22 at 22:25
  • easiest way I could think of is to make n and monTableau global variables in main.c, and then f1 and f2 can operate on those functions. another way is passing the variables as parameters to the function – Adrian Costin Mar 06 '22 at 22:37
  • to whoever closed this question, it was valid. the guy asked how to pass parameters between two different functions. just because he forgot about including the math library and that was the first error he got does not mean that he should get his question closed. shame on you, when I saw it earlier I thought of answering (did not see that it was closed, which is my bad but still), and now that I have my explanation prepared i see that it is closed. this site is going downhill badly. – Adrian Costin Mar 06 '22 at 23:13

0 Answers0