LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2005, 09:07 PM   #1
bigapple
LQ Newbie
 
Registered: Oct 2004
Location: Hangzhou.China
Distribution: Suse 9.3 | redhat 9.0 | Solaris 10
Posts: 29

Rep: Reputation: 15
Questions about 'extra'!


There are 2 source code file:
def.c
Code:
/*just define the i*/

int i = 3;
print_i.c
Code:
/*display the i*/
#include<stdio.h>

void print_i(void){

            int i;
            printf("The number is %d\n",i);
}
main.c
Code:
#include<stdio.h>
int main(void){
         print_i();
         return 0;
}
As I konw there is something wrong in my code, the varible i is defined in the def.c ,and used in the print_i.c . So I must define the i with 'extern', it means I should use "extern int i" replace the "int i" in the print_i.c. But I haven't make this replacement, the code can run successful.

I want to know is the 'extern' be indispensable to this program?

Last edited by bigapple; 07-04-2005 at 09:13 PM.
 
Old 07-04-2005, 09:56 PM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
By declaring the i locally in the function in print_i.c, you're actually making a local variable distinct from the global one in the other file.

The solution is to include def.c in print_i.c and then not redeclare i in the print_i.c.

I don't think you need to use extern here at all.
 
Old 07-04-2005, 09:58 PM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
def.c
Code:
/* Just define the i */
int i = 3;
print_i.c
Code:
/*display the i*/
#include<stdio.h>
#include "def.c"

void print_i(void){
            printf("The number is %d\n",i);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extra processes Ion42 Linux - Software 4 07-24-2004 02:27 PM
Extra OS Suggestions? KungFuHamster General 10 09-02-2003 01:34 PM
Extra boot jcue Linux - Distributions 8 08-13-2003 11:29 AM
"Extra-newbie" Questions cHaOtiC Linux - Newbie 1 01-04-2003 04:30 PM
CD Extra thesynergy Linux - General 2 02-01-2002 01:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration