@rahulsharma
file1.c
#include<stdio.h>
extern int i;// defintion provided in this file itself
extern int j; // definition provided in file2
void next()
{
++i;
other();
}
int main()
{
++i;
printf("%d\n",j);
printf("%d\n",i);
next();
}
int i=3;
// end of file1.c
file2.c
extern int i; // declaration of i as extern
int j=10; // j defined in file2 here which was declared as extern in file 1
void other()
{
++i;
printf("%d\n",i);
}
// end of file2.c
compile both file together
as
rahul@rahul:~gcc file1.c file2.c
rahul@rahul:~./a.out
you will get the required output
On Fri, Nov 16, 2012 at 8:27 AM, Neeraj Gangwar <[email protected]>wrote:
> That's why you are getting the error. You have to compile both the files
> together. Search on google. I don't use dev c++.
>
> *Neeraj Gangwar*
> B.Tech. IV Year
> Electronics and Communication IDD
> Indian Institute of Technology Roorkee
> Contact No. : +91 9897073730
>
>
> On Thu, Nov 15, 2012 at 11:32 PM, rahul sharma <[email protected]>wrote:
>
>> No...individually...dev cpp..how to compile both together???
>>
>>
>> On Thu, Nov 15, 2012 at 9:26 PM, Neeraj Gangwar
>> <[email protected]>wrote:
>>
>>> Which compiler are you using ? Are you compiling both the files together
>>> ?
>>>
>>> *Neeraj Gangwar*
>>> B.Tech. IV Year
>>> Electronics and Communication IDD
>>> Indian Institute of Technology Roorkee
>>> Contact No. : +91 9897073730
>>>
>>>
>>>
>>> On Thu, Nov 15, 2012 at 9:10 PM, rahul sharma
>>> <[email protected]>wrote:
>>>
>>>> but how can i use extern..if i simply declare a variable in file1 as
>>>> int j and try to use in file2 with extern then it shows that j nit
>>>> defined..how cum file2 knows in which file j is defined....for e.g if i use
>>>> extern in file it means that this variable/fxn is defined somewhr else.then
>>>> what are those files in which it searches this variable definition..i m
>>>> getting error....plese give me 2 files in which one files defines variable
>>>> and other uses using extern.....its not working for me....
>>>>
>>>> On Thu, Nov 15, 2012 at 12:08 PM, Rahul Kumar Dubey
>>>> <[email protected]>wrote:
>>>>
>>>>> @rahul it will compile perfectly well . note that you have declared j
>>>>> in file 1 as extern and used it and have not provided its definition any
>>>>> where
>>>>> so getting compile error.
>>>>> as far as functions are concerned they are external by defaullt as
>>>>> specified by @shobhit
>>>>>
>>>>> i am attaching your corrected code which runs fine ...
>>>>> file1.c
>>>>>
>>>>>
>>>>> #include<stdio.h>
>>>>> extern int i;
>>>>> //extern int j; // provide a declaration for this
>>>>> void next(void);
>>>>>
>>>>> int main()
>>>>> {
>>>>> ++i;
>>>>> printf("%d\n",i);
>>>>>
>>>>> next();
>>>>> getchar();
>>>>> }
>>>>> int i=3;
>>>>> void next()
>>>>> {
>>>>> ++i;
>>>>> printf("%d\n",i);
>>>>> //printf("%d",j); // since no defintion provided so getting error
>>>>> other();
>>>>> }
>>>>>
>>>>> file2.c
>>>>>
>>>>>
>>>>> extern int i;
>>>>> void other()
>>>>> {
>>>>> ++i;
>>>>> printf("%d\n",i);
>>>>> }
>>>>>
>>>>> if you want to use j u need to provide defintion either in file 1 or
>>>>> file 2
>>>>> output:
>>>>> 4
>>>>> 5
>>>>> 6
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Wed, Oct 24, 2012 at 10:56 PM, rahul sharma <
>>>>> [email protected]> wrote:
>>>>>
>>>>>> can nyone provide me dummy code of how exactly to use extern in c..
>>>>>> in dev environment
>>>>>>
>>>>>> when i declare int i in one fyl
>>>>>> and try use use with extern int i in another then it doesnt
>>>>>> compile..plz coment
>>>>>>
>>>>>>
>>>>>> On Wed, Oct 24, 2012 at 9:58 PM, rahul sharma <
>>>>>> [email protected]> wrote:
>>>>>>
>>>>>>> Then why its not running?
>>>>>>>
>>>>>>>
>>>>>>> On Wed, Oct 24, 2012 at 6:50 PM, SHOBHIT GUPTA <
>>>>>>> [email protected]> wrote:
>>>>>>>
>>>>>>>> http://www.geeksforgeeks.org/archives/840
>>>>>>>>
>>>>>>>> By default, the declaration and definition of a C function have
>>>>>>>> “extern” prepended with them. It means even though we don’t use extern
>>>>>>>> with
>>>>>>>> the declaration/definition of C functions, it is present there. For
>>>>>>>> example, when we write.
>>>>>>>>
>>>>>>>> int foo(int arg1, char arg2);
>>>>>>>>
>>>>>>>> There’s an extern present in the beginning which is hidden and the
>>>>>>>> compiler treats it as below.
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> extern int foo(int arg1, char arg2);
>>>>>>>>
>>>>>>>>
>>>>>>>> On Wed, Oct 24, 2012 at 4:40 PM, rahul sharma <
>>>>>>>> [email protected]> wrote:
>>>>>>>>
>>>>>>>>> Pleaase reply with sol as asp
>>>>>>>>>
>>>>>>>>> Fille 1:
>>>>>>>>> #include<stdio.h>
>>>>>>>>> extern int i;
>>>>>>>>>
>>>>>>>>> extern int j;
>>>>>>>>> void next(void);
>>>>>>>>> int main()
>>>>>>>>> {
>>>>>>>>> ++i;
>>>>>>>>> printf("%d",i);
>>>>>>>>> next();
>>>>>>>>> getchar();
>>>>>>>>> }
>>>>>>>>> int i=3;
>>>>>>>>> void next()
>>>>>>>>> {
>>>>>>>>> ++i;
>>>>>>>>> printf("%d",i);
>>>>>>>>> printf("%d",j);
>>>>>>>>> other();
>>>>>>>>> }
>>>>>>>>> File 2:
>>>>>>>>> extern int i;
>>>>>>>>>
>>>>>>>>> void other()
>>>>>>>>> {
>>>>>>>>> ++i;
>>>>>>>>> printf("%d",i)'
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> How cum file 1 knows what is other();as we havnet define with
>>>>>>>>> extern void other();
>>>>>>>>> it should be error????
>>>>>>>>> but when i include the statemetn extern void other,then also it
>>>>>>>>> shows??
>>>>>>>>> pls provide me o/p of this questiona nd also tell how use use
>>>>>>>>> variable of one file in other as simply writing extern in a is not
>>>>>>>>> accesing
>>>>>>>>> global a of other file....
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>>> Groups "Algorithm Geeks" group.
>>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>>> To unsubscribe from this group, send email to
>>>>>>>>> [email protected].
>>>>>>>>> For more options, visit this group at
>>>>>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>> Groups "Algorithm Geeks" group.
>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>> To unsubscribe from this group, send email to
>>>>>>>> [email protected].
>>>>>>>> For more options, visit this group at
>>>>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Algorithm Geeks" group.
>>>>>> To post to this group, send email to [email protected].
>>>>>> To unsubscribe from this group, send email to
>>>>>> [email protected].
>>>>>> For more options, visit this group at
>>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> *RAHUL KUMAR DUBEY*
>>>>> *BTech-3rd year *
>>>>> *Computer Science &Engineering *
>>>>> *Motilal Nehru National Institute Of Technology*
>>>>> *Allahabad[211004],UP.*
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Algorithm Geeks" group.
>>>>> To post to this group, send email to [email protected].
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected].
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected].
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
*RAHUL KUMAR DUBEY*
*BTech-3rd year *
*Computer Science &Engineering *
*Motilal Nehru National Institute Of Technology*
*Allahabad[211004],UP.*
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.