Thread: Help With Extern?

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    2

    Help With Extern?

    I'm supposed to write a program in C that stores data in an array and finds the sum/average, max and min. Each of the functions are supposed to be a separate file and the array global but I'm not supposed to use the array as an argument.

    I have 3 functions written but I'm having trouble with the linking part (maybe I don't know if that's what its called) When I gcc main.c I get errors saying undefined reference to each function and my array is undeclared in the other functions. Is there a way to do this without using #include .h or .c? I was told to use extern but I'm confused on how that works.
    Can someone explain what I'm missing here?
    Thanks

    Here's what I have
    average.c
    Code:
    #include <stdio.h>
    
    
    
    void findAverage(int n)
    {
    
    
    int i=0;
    int sum=0;
    for(i=0; i<n ; i++)
    {
    
    
    
    
            sum = numbers[i] + sum;
    
    
    }
    
    
            printf("%s%d\n","The Sum is: ", sum);
            double average = sum/n;
            printf("%s%d\n", "The Average is: ", average);
    }
    max.c
    Code:
    #include <stdio.h>
    
    
    
    
    
    void findMax(int n) //n = size
    {
    
    
    
    
    
    
            int i;
            int max = -2147483647;
            for(i = 0; i<n; i++)
    {
            if(numbers[i]>max){
            max = numbers[i];
            }
            else{
            max = max;
            }
    }
    printf("%s%d\n", "The Maximum Value is: ", max);
    
    
    }
    min.c
    Code:
    #include <stdio.h>
    
    
    
    void findMin(int n) //n = size
    {
            int i;
            int min = 2147483647; 
            for(i = 0; i<n; i++)
    {
            if(numbers[i]<min){
            min = numbers[i];
            }
            else{
            min = min;
            }
    }
    printf("%s%d\n", "The Minimum Value is: ", min);
    
    
    }
    main.c

    Code:
    #include <stdio.h>//#include "average.c"
    //#include "min.c"
    //#include "max.c"
    
    
    int numbers[5] ={45, 53, 75, 36, 11};
    extern numbers[];
    
    
    
    
    
    
    
    
    
    
    
    
    int main()
    {
    findAverage(5);
    findMin(5);
    findMax(5);
    return 0;
    }
    ~

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Firstly, this is a bad use of extern. It would be better to pass the array to the functions:
    Code:
    void findMin(int *numbers, int n);
    But if you want to use extern, then the "extern" statement needs to go in the files that need access to main's "numbers" variable:
    Code:
    // Near the top of min.c, etc.
    extern int numbers[];
    Usually we just include .h files, not .c files, but main needs to see the prototypes of the functions it will call from those other .c files.
    You can just put the prototypes in main.c, or you could put them all in a single header or a separate header for each .c file.
    Include the header(s) in main.c.

    To compile all .c files together you can do
    gcc main.c min.c max.c average.c
    Or you can compile them separately to object files and then link them (which is what the above command does behind the scenes) :
    gcc -c main.c
    gcc -c min.c
    gcc -c max.c
    gcc -c average.c
    gcc main.o min.o max.o average.o
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    2

    Thank You

    I was so confused. Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-13-2020, 09:48 PM
  2. Replies: 9
    Last Post: 05-07-2017, 12:29 PM
  3. How to use extern...
    By KansaiRobot in forum C++ Programming
    Replies: 6
    Last Post: 02-16-2016, 01:59 AM
  4. Replies: 17
    Last Post: 12-15-2006, 11:02 AM
  5. using extern, please help
    By Stevo in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2004, 06:18 PM

Tags for this Thread