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
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
is both definition and declaration, but
is just a declaration -- it says that a1 is of type int but memory for it is allocated elsewhere.
Note that
If you define a1 in bothe file1.c and file2.c, e.g.
then the linker does complain
If you compile bothe files in the same compilation unit, e.g.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)