Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #17878 > unrolled thread
| Started by | ashu <ashutosh.ghildiyal@gmail.com> |
|---|---|
| First post | 2012-02-24 02:36 -0800 |
| Last post | 2012-02-25 10:44 -0500 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.c
macro prototype ashu <ashutosh.ghildiyal@gmail.com> - 2012-02-24 02:36 -0800
Re: macro prototype Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-24 03:34 -0800
Re: macro prototype Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-24 11:38 +0000
Re: macro prototype Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-24 07:49 -0500
Re: macro prototype ashu <ashutosh.ghildiyal@gmail.com> - 2012-02-24 08:31 -0800
Re: macro prototype Ya Shou <shouyatf@gmail.com> - 2012-02-25 05:52 -0800
Re: macro prototype pete <pfiland@mindspring.com> - 2012-02-25 10:44 -0500
| From | ashu <ashutosh.ghildiyal@gmail.com> |
|---|---|
| Date | 2012-02-24 02:36 -0800 |
| Subject | macro prototype |
| Message-ID | <8eb7078a-779c-4baa-92aa-0bacac4885ff@r1g2000yqk.googlegroups.com> |
Hi all, I want to use a macro which I want to define in a .c(lets call it macro.c) file instead of a .h(macro.h) file because it uses some local variables which are defined in the .c file. However I want this macro to be visible to other files which will include the corresponding .h(macro.h) file. The problem here is since the macro is defined in the macro.c file and there is no way I can declare a prototype of the macro in the macro.h file, this macro cannot be invoked by other files since the other files simply include the macro.h file. Is there anyway I can get around this problem ? regards Ashu
[toc] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2012-02-24 03:34 -0800 |
| Message-ID | <08e1b176-7b4b-4893-82f8-0db45ac74855@w19g2000vbe.googlegroups.com> |
| In reply to | #17878 |
On Feb 24, 10:36 am, ashu <ashutosh.ghildi...@gmail.com> wrote: > I want to use a macro which I want to define in a .c(lets call it > macro.c) file instead of a .h(macro.h) file because it uses some local > variables which are defined in the .c file. you cannot access local variables (I guessing you mean file scope?) using a macro used in another .c file (technical term "Translation Unit") > However I want this macro > to be visible to other files which will include the > corresponding .h(macro.h) file. > > The problem here is since the macro is defined in the macro.c file and > there is no way I can declare a prototype of the macro in the macro.h > file, since there is no such thing as a macro-prototype > this macro cannot be invoked by other files since the other > files simply include the macro.h file. > > Is there anyway I can get around this problem ? no. Not as stated you have a fundamental misconception about what macros are. macros (in C) do a simple textual substitution. Why can't your macro be a function? Or could your macro call a function that does the real work. please don't say "efficiency"...
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-02-24 11:38 +0000 |
| Message-ID | <0.c9835e5db82122f8ffdd.20120224113840GMT.87zkc8jwan.fsf@bsb.me.uk> |
| In reply to | #17878 |
ashu <ashutosh.ghildiyal@gmail.com> writes: > Hi all, > I want to use a macro which I want to define in a .c(lets call it > macro.c) file instead of a .h(macro.h) file because it uses some local > variables which are defined in the .c file. However I want this macro > to be visible to other files which will include the > corresponding .h(macro.h) file. > > The problem here is since the macro is defined in the macro.c file and > there is no way I can declare a prototype of the macro in the macro.h > file, this macro cannot be invoked by other files since the other > files simply include the macro.h file. > > Is there anyway I can get around this problem ? No. Even if you could solve the specific problem of the macro, you would still have a problem with the variable. Macros work by textual replacement, so a macro that uses a variable that is local to one file (in C terms it has internal linkage) will expand to code that references that variable where it is unavailable. Do you know you must use a macro? I'd just use a function. If that is really overkill, give the variable in question external linkage and declare it (extern ...) in the .h file along with the macro that uses it. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-02-24 07:49 -0500 |
| Message-ID | <ji812b$jgq$1@dont-email.me> |
| In reply to | #17878 |
On 2/24/2012 5:36 AM, ashu wrote:
> Hi all,
> I want to use a macro which I want to define in a .c(lets call it
> macro.c) file instead of a .h(macro.h) file because it uses some local
> variables which are defined in the .c file. However I want this macro
> to be visible to other files which will include the
> corresponding .h(macro.h) file.
>
> The problem here is since the macro is defined in the macro.c file and
> there is no way I can declare a prototype of the macro in the macro.h
> file, this macro cannot be invoked by other files since the other
> files simply include the macro.h file.
>
> Is there anyway I can get around this problem ?
Not in the way you describe. Macros are not "declared" and
do not have "prototypes;" they are defined (and perhaps undefined)
and have definitions. Also, macros disappear at an early stage of
compilation and are replaced by their expansions; there is no way
for a macro to remain "visible" after it has been replaced.
If you want the macro's expansion to refer to variables that
live in a particular file, you can write the "macro.h" file like
extern int variable;
#define MACRO(x) ( variable += (x) )
... after which any source file that includes "macro.h" can write
MACRO(millicent) or MACRO(42) or whatever. Meanwhile, the file
that actually defines the variable should do
#include "macro.h"
int variable;
... and can, itself, also write MACRO(f(x)) if it wants.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | ashu <ashutosh.ghildiyal@gmail.com> |
|---|---|
| Date | 2012-02-24 08:31 -0800 |
| Message-ID | <1745393f-1d6f-4463-9a7f-fb1bdfc8aca4@l16g2000vbl.googlegroups.com> |
| In reply to | #17883 |
On Feb 24, 1:49 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote: > On 2/24/2012 5:36 AM, ashu wrote: > > > Hi all, > > I want to use a macro which I want to define in a .c(lets call it > > macro.c) file instead of a .h(macro.h) file because it uses some local > > variables which are defined in the .c file. However I want this macro > > to be visible to other files which will include the > > corresponding .h(macro.h) file. > > > The problem here is since the macro is defined in the macro.c file and > > there is no way I can declare a prototype of the macro in the macro.h > > file, this macro cannot be invoked by other files since the other > > files simply include the macro.h file. > > > Is there anyway I can get around this problem ? > > Not in the way you describe. Macros are not "declared" and > do not have "prototypes;" they are defined (and perhaps undefined) > and have definitions. Also, macros disappear at an early stage of > compilation and are replaced by their expansions; there is no way > for a macro to remain "visible" after it has been replaced. > > If you want the macro's expansion to refer to variables that > live in a particular file, you can write the "macro.h" file like > > extern int variable; > #define MACRO(x) ( variable += (x) ) > > ... after which any source file that includes "macro.h" can write > MACRO(millicent) or MACRO(42) or whatever. Meanwhile, the file > that actually defines the variable should do > > #include "macro.h" > int variable; > > ... and can, itself, also write MACRO(f(x)) if it wants. > > -- > Eric Sosman > esos...@ieee-dot-org.invalid Thank you Nick Eric and Ben. It gave me lot of clarity! regards ashu
[toc] | [prev] | [next] | [standalone]
| From | Ya Shou <shouyatf@gmail.com> |
|---|---|
| Date | 2012-02-25 05:52 -0800 |
| Message-ID | <04ed151e-92b7-44b5-a43f-c9b11a082230@t15g2000yqi.googlegroups.com> |
| In reply to | #17878 |
On Feb 24, 6:36 pm, ashu <ashutosh.ghildi...@gmail.com> wrote: > Hi all, > I want to use a macro which I want to define in a .c(lets call it > macro.c) file instead of a .h(macro.h) file because it uses some local > variables which are defined in the .c file. However I want this macro > to be visible to other files which will include the > corresponding .h(macro.h) file. > > The problem here is since the macro is defined in the macro.c file and > there is no way I can declare a prototype of the macro in the macro.h > file, this macro cannot be invoked by other files since the other > files simply include the macro.h file. > > Is there anyway I can get around this problem ? > > regards > Ashu Why don't you separate, by moving, the macro definitions from the source into a header file? You can refer them by including and defining the variables in the source. And thus the your problem should be solved. By the way, please mind the macro guard in order to avoid the macro- multiple-definition problem. -- Shou Ya.
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-02-25 10:44 -0500 |
| Message-ID | <4F4901E7.5DFD@mindspring.com> |
| In reply to | #17878 |
ashu wrote: > > Hi all, > I want to use a macro which I want to define in a .c(lets call it > macro.c) file instead of a .h(macro.h) file because it uses some local > variables which are defined in the .c file. However I want this macro > to be visible to other files which will include the > corresponding .h(macro.h) file. > > The problem here is since the macro is defined in the macro.c file and > there is no way I can declare a prototype of the macro in the macro.h > file, this macro cannot be invoked by other files since the other > files simply include the macro.h file. > > Is there anyway I can get around this problem ? Declare the local variables as "extern" in the macro.h file and define the macro in macro.h also. -- pete
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web