# extern varibles

• 12-17-2002
manwhoonlyeats
extern varibles
Hi,
im having some trouble with this program that is supposed to find the prime factors of a number. More specificly im having truoble with the extern part. I must not understand the correct syntax of extern, but ive looked for helpfull tutorials and cant find any. Could someone explain to me the correct syntax of extern, and tell me what is wrong with my program.

Thanks

Code:

```#include <stdio.h> #include <math.h> extern int primefactors[100],index=0; void factortree(int number); int isprime(int prime); int main(void) { int number2factor; printf("Input a number to factor:"); scanf("%d",&number2factor); factortree(number2factor); printf("\nPrime Factors of %d are\n",number2factor); for(;index>=0;index--)         (index==0) ? printf("primefactors[index]") : printf("primefactors[index] * "); getchar(); return 0; } void factortree(int number) { int i,factor; if (number<0)         number*=-1; if (number==1)         return; for (i=(int)sqrt(number);i>=2; i--) { if ((number % i) == 0) {         factor=number/i;         printf("a factor of %d is: %d * %d\n",number,factor,i); } if (isprime(i)) {              primefactors[index]=i;         index++; } else {         printf("i=%d is not prime\n",i);         factortree(i); } if (isprime(factor)) {         primefactors[index]=i;         index++; } else {         printf("factor=%d is not prime\n",factor);         factortree(factor); } } } int isprime(int prime) { int i; if (prime==1 || prime==2)         return 1; for (i=2; i<sqrt(prime); i++) {         if ((prime % i) == 0)                 return 0; } return 1; }```
• 12-17-2002
quzah
The keyword extern states that this variable exists in another file and that it has been declared there, and this is really just a "pointer" (not in the C sense of the word, but in the literal sense) to that location. It means "Hey, this variable isn't in this file. It's elsewhere, and this is so we can reference that variable."

Quzah.
• 12-17-2002
Dave_Sinkula
Where's the rest? If the variables primefactors and index are declared as external to this file, in what file are they defined?

If you've shown us as much as you've shown the compiler/linker, you're seeing the same complaint. Or if the variables primefactors and index are not intended to be defined in another file, don't tell the compiler to believe this (remove the keyword extern from the declaration).

-----

In factortree(), where do you assign factor before using it?

Since you postincrement index in factortree(), shouldn't you predecrement it to start the for loop in main()?

There are other problems still remaining, but HTH for the next round.
• 12-17-2002
manwhoonlyeats
i dont get it
so instead of declaring the variables extern what do i declare them so that every function in my file can access them? auto?
• 12-17-2002
quzah
Re: i dont get it
Quote:

Originally posted by manwhoonlyeats
so instead of declaring the variables extern what do i declare them so that every function in my file can access them? auto?
extern = "This variable to which you're referring is in an entirely different file. This is just a reference so you can access the variable which is in an entirely different file."

auto = the default assignment = no need for the acutal keyword = If outside of a function, it will be available to all functions which are declared after it. Otherwise, inside of a function, the variable is limited to the scope of the function.

Quzah.
• 12-17-2002
manwhoonlyeats
ok, whats wrong now?
Ok, i figured out what the mistakes were. Now the program works except it doesent work right. it finds the prime factors of 10 right (5*2). But when it tries to find some numbers, like 20 it repeats its self. Check out what i mean.
Whats wrong???
Also after that i would like to say 2 to the 3rd instead of 2*2*2 is there a way (in DOS) that i can actually raise one of the numbers? ie 3
2
except not so crappy
In Qbasic there is =).
Heres what i got now
Code:

```#include <stdio.h> #include <math.h> int primefactors[1000],index=0; void factortree(int number); int isprime(int prime); int main(void) { int number2factor; printf("Input a number to factor:"); scanf("%d",&number2factor); factortree(number2factor); printf("\nThe prime Factors of %d are:\n",number2factor); for(index--;index>=0;index--)         if (index==0)                 printf("%d", primefactors[index]);         else                 printf("%d * ",primefactors[index]); getchar(); return 0; } void factortree(int number) { int i,factor; if (number<0)         number*=-1; if (number==1)         return; for (i=(int)sqrt(number);i>=2; i--) { if (index>1000) { printf("Error: array too small!"); exit(1); } if ((number % i) == 0) {         factor=number/i;         if (isprime(i))         {                      if (i !=0) {                         primefactors[index]=i;                         index++;                 }         }                 else {                 factortree(i);         }                 if (isprime(factor)) {                 if (factor !=0) {                 primefactors[index]=factor;                 index++;         }         }                 else {                 factortree(factor);         } } } } int isprime(int prime) { int i; if (prime==1 || prime==2)         return 1; for (i=2; i<=sqrt(prime); i++) {         if ((prime % i) == 0)                 return 0; } return 1; }```