Extern variable.


 
Thread Tools Search this Thread
Top Forums Programming Extern variable.
# 1  
Old 07-24-2008
Extern variable.

file1.c

Code:
int a1;
int main()
{
        a1 = 2;
        printf("\na1 = %d\n", a1);
        next();
        printf("\na1 = %d\n", a1);
        next1();
        printf("\na1 = %d\n", a1);
}

file2.c
Code:
#include <stdio.h>

int b1 = 0;
void next(void)
{
        char a1;

        a1 = 'a';
        b1 = 77;
}


file3.c

Code:
int a1;
void next1(void)
{
        float b1;

        b1 = 20.3;
        a1 = 15;
}


When i compile it using gcc -c and create separate respective object file
-and try to link them it dosent give any errors as to "multiple declaration of variable" as of "variable a" in this case.

-And it gets comiled and linked successfully.
-also the "third printf" in file1.c prints the value of a=15 which is updated by file3.c.
-whereas its nowhere specified in file3.c that variable "a is an exterm variable"
in that case file1.c would printf the value of a1=15;
-so.................???????????????????????


Regards
Tanvir


Moved to High Level Programming Forum. FPMurphy

Last edited by vino; 07-24-2008 at 06:36 AM.. Reason: added code tags
# 2  
Old 07-29-2008
So, the gcc linker appears not to insist on the extern qualifier.
It looks in fact like the compler and linker is happy that a1 is declared and tacitly assumes the extern and the definition -- recall the difference in C between definition of a variable, which tells the compiler to allocate memory for it, and the declaration, which tells the compiler what type it is. The distinction is apparent in global variables, where
Code:
int foo = 3;

is both definition and declaration, but
Code:
extern int foo;

is just a declaration -- it says that a1 is of type int but memory for it is allocated elsewhere.

Note that
  1. If you define a1 in bothe file1.c and file2.c, e.g.
    Code:
    int a1 = 2;

    then the linker does complain
  2. If you compile bothe files in the same compilation unit, e.g.
    Code:
    gcc -o file12.o -c file1.c file2.c

    then the compiler complains.

The a1 variable in file2.c is protected by the lexical scope of the function next and so is completely different to the global int a1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Extern keyward on function in C

I saw a header (.h) file with mixture of "regular" function declarations and other extern function declarations. As I was told all function declarations are implicitly external and the extern on functions declarations is superfluous. Here my focus is on function declaration, not variable yet. int... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Programming

C header file and extern

In the header file data.h i got: const char ack_msg = "ack: received your msg\n"; In the code file server.c i got: extern const char ack_msg; And else it is only used in a function call: user$ grep ack_msg *c *h server.c:extern const char ack_msg; server.c: n = write(clientsfd,... (5 Replies)
Discussion started by: tornow
5 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Programming

segmentation fault for extern

Why this is happening when both of them compiled together and run? I am getting segmentation fault SIGSEGV. File1.c: int arr; File2.c: extern int *arr; int main() { arr = 100; } (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. UNIX for Dummies Questions & Answers

fetchmail and forward to an extern address

Hi, I'm searching for an solution for the following problem. I want fetch some mails via pop3 from a@a.com with fetchmail. That works perfectly. Now any incoming mail should forwarded to b@b.com via smtp obv. But I don't know how to configure that. All online tutorials describe forwarding to... (0 Replies)
Discussion started by: mcW
0 Replies

9. Linux

Problem mounting extern hd (fedora 9)

Hi there, I'm having a bit of a strange problem which I would appreciate some help with. The Problem: I have two external hard drives, but I'm borrowing one off my parents to copy data too (one of mine, which is identical to theirs - WD MyBook 300g - is on its way out). Fedora 9 recognizes... (3 Replies)
Discussion started by: lasthidingplace
3 Replies

10. Programming

extern for functions

Hi, Please let me know if the extern keyword is necessary for using functions which is defined and declared in another file and and used in a different file where both these files are linked together. thanks (8 Replies)
Discussion started by: naan
8 Replies
Login or Register to Ask a Question