Thread: extern varibles

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    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?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: i dont get it

    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  4. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM