Problem with extern variable

This is a discussion on Problem with extern variable within the C Programming forums, part of the General Programming Boards category; <header.h> Code: #ifndef HEADER_H #define HEADER_H extern int x; #endif <main.c> Code: #include "header.h" int main() { int x = ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    Problem with extern variable

    <header.h>
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    extern int x;
    #endif
    <main.c>
    Code:
    #include "header.h"
    
    int main()
    {
      int x = 5;
      printf("x=%d\n", x);
      func(x);
      printf("x=%d\n", x);
      return 0;
    }
    <func.c>
    Code:
    #include "header.h"
    
    void func(int y)
    {
      y=9;
      x = 5;
    }
    % gcc main.c func.c
    Undefined first referenced
    symbol in file
    x /var/tmp//ccQGVCcI.o
    ld: fatal: Symbol referencing errors. No output written to a.out

    Why? func.c #includes header.h which declares x! Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why? func.c #includes header.h which declares x!
    Nice try, but it doesn't work that way. extern doesn't magically give other functions special access rights to a local variable of another function. You basically need to define a global x in one of your source files (like main.c), and extern tells another source file (like func.c) that x actually exists elsewhere so that the code will compile.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    OK, I have amended my programs:

    <header.h>
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    extern int x;
    #endif
    <main.c>
    Code:
    #include "header.h"
    int x = 5;
    int main()
    {
      printf("x=%d\n", x);
      func();
      printf("x=%d\n", x);
      return 0;
    }
    <func.c>
    Code:
    #include "header.h"
    
    void func(void)
    {
      x = 15;
    }
    % gcc main.c func.c
    % a.out
    x=5
    x=5

    Why hasn't the second printf prints out x=15? Isn't x now a global variable that everyone can change?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    First of all, that code won't compile as it's shown since main.c doesn't see any declaration of func(). Second of all, x would print out 15 the second time, so there must be some other problem (that isn't shown) in your code.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    thx. it is working now. I seemed to forget to remove an obsolete func.o.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with initializing tm * variable.
    By PaulBlay in forum C Programming
    Replies: 2
    Last Post: 04-21-2009, 08:08 AM
  2. extern variable assignment
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 06:41 AM
  3. Strange variable problem
    By Mithoric in forum Windows Programming
    Replies: 9
    Last Post: 06-19-2004, 03:34 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 03:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21