From: Igor Tandetnik on 4 Feb 2010 16:37 Robby <Robby(a)discussions.microsoft.com> wrote: > ==========test.h > #define Z(a,b) y = a; \ > e = b; // << no back slah here to end macro, no ??? > > extern int y; > extern int e; > > ============test.c > #include <stdio.h> > #include "test_A.h" The header is called test.h, not test_A.h > int y; > int e; > > int main() > {Z(1,2); > return 0; > } > ============== > > This still gives errors. What errors? > But is it even possible to assign a value to > the extern variables while inside a macro? Macro is just textual substitution. When the macro is used, its body is expanded, parameters substituted, and the result compiled just as if you typed it in directly in place of the macro. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Tamas Demjen on 4 Feb 2010 17:02 Robby wrote: > ==========test.h > #define Z(a,b) y = a; \ > e = b; // << no back slah here to end macro, no ??? Backslash immediately followed by a newline means "my macro still continues". > This still gives errors. What errors exactly? It shouldn't. Except that you seem to have a space and a tab after the backslash. It doesn't work that way. Backslash- newline is what you want. > But is it even possible to assign a value to the > extern variables while inside a macro? There's nothing magical happening inside a macro. It's like cut and paste. Just select the definition of the macro in your imagination, and move it into the code where you're using it. That's it. Z(1,2); becomes y = 1; e = 2; Note: To be precise, macros work with C tokens, not with characters, but it's easier for you right now to think of a macro as a cut-and-paste in your editor. It's close enough. Tom
From: Tim Roberts on 5 Feb 2010 02:12 Robby <Robby(a)discussions.microsoft.com> wrote: > >aaahhhrrhg! clumsy me! That's not what I meant! ooofff! Here you go. > >==========test.h >#define Z(a,b) y = a; \ > e = b; // << no back slah here to end macro, no ??? > >extern int y; >extern int e; > >============test.c >#include <stdio.h> >#include "test_A.h" >int y; >int e; > >int main() >{Z(1,2); >return 0; >} >============== > >This still gives errors. But is it even possible to assign a value to the >extern variables while inside a macro? Or am I doing something that can't be >done in C ? Sure, it's possible, but it is an EXTREMELY bad idea. Why? Look at this statement: Z(1,2); From that statement, why would the reader ever suspect that this statement will be modifying global variables? As a general rule, preprocessor magic is almost always a bad idea. Macros are fine for constants and conversions, but anything more complicated than that is trouble. Use functions instead. -- Tim Roberts, timr(a)probo.com Providenza & Boekelheide, Inc.
First
|
Prev
|
Pages: 1 2 3 4 Prev: Why is reference from unused function required? Next: simple reference "for each" question |