Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   #defines (http://www.velocityreviews.com/forums/t504884-defines.html)

raghu 05-08-2007 06:39 AM

#defines
 
Hello Everyone,

I am using macro functions in my project the code is
working correctly, But I have a doubt wheather the macro functions
creates a context switching as in the case of normal functions. For
example see the code below:

#define funct(dest, src) {\
for( i = 0; i< 10; i++ )\
dest += (src + i);\
}

main()
{

int i, k;

funct(k , 10);

printf(" %d", k);
}

In this case 'int i' was not declared in funct but it works. And if in
main() 'int i' was not declared and declared in funct defination also
it works. Is there any context switch happening.

Thanks in advance

Bye
Raghu


Ian Collins 05-08-2007 06:52 AM

Re: #defines
 
raghu wrote:
> Hello Everyone,
>
> I am using macro functions in my project the code is
> working correctly, But I have a doubt wheather the macro functions
> creates a context switching as in the case of normal functions. For
> example see the code below:
>

Macros are simple text substitution.

Functions DO NOT cause a context switch, I think you are getting your
terminology in a muddle.

--
Ian Collins.

Fred Kleinschmidt 05-08-2007 02:19 PM

Re: #defines
 

"raghu" <ragavakumar@gmail.com> wrote in message
news:1178606376.895789.150110@e65g2000hsc.googlegr oups.com...
> Hello Everyone,
>
> I am using macro functions in my project the code is
> working correctly, But I have a doubt wheather the macro functions
> creates a context switching as in the case of normal functions. For
> example see the code below:
>
> #define funct(dest, src) {\
> for( i = 0; i< 10; i++ )\
> dest += (src + i);\
> }
>
> main()
> {
>
> int i, k;
>
> funct(k , 10);
>
> printf(" %d", k);
> }
>
> In this case 'int i' was not declared in funct but it works. And if in
> main() 'int i' was not declared and declared in funct defination also
> it works. Is there any context switch happening.
>


No, it does NOT work. The statement
dest += (src + i);
which, when you call funct(k, 10) becomes
k += (10 + i);
can produce any arbitrary result, since k is not initialized.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing



Laurent Deniau 05-08-2007 04:30 PM

Re: #defines
 
CBFalconer wrote:
> raghu wrote:
>> I am using macro functions in my project the code is working
>> correctly, But I have a doubt wheather the macro functions
>> creates a context switching as in the case of normal functions.
>> For example see the code below:
>>
>> #define funct(dest, src) {\
>> for( i = 0; i< 10; i++ )\
>> dest += (src + i);\
>> }
>>
>> main() {
>> int i, k;
>>
>> funct(k , 10);
>> printf(" %d", k);
>> }
>>
>> In this case 'int i' was not declared in funct but it works. And if in
>> main() 'int i' was not declared and declared in funct defination also
>> it works. Is there any context switch happening.

>
> The code is illegal. Your "funct(k, 10)" defines a nested
> function, which is not allowed in C.


funct defines a compound statement, not a function.

a+, ld.

osmium 05-08-2007 08:15 PM

Re: #defines
 
"raghu" writres:

> I am using macro functions in my project the code is
> working correctly, But I have a doubt wheather the macro functions
> creates a context switching as in the case of normal functions. For
> example see the code below:
>
> #define funct(dest, src) {\
> for( i = 0; i< 10; i++ )\
> dest += (src + i);\
> }
>
> main()
> {
>
> int i, k;
>
> funct(k , 10);
>
> printf(" %d", k);
> }
>
> In this case 'int i' was not declared in funct but it works. And if in
> main() 'int i' was not declared and declared in funct defination also
> it works. Is there any context switch happening.


I don't know what *you* mean by context switching.

But I would start by cleaning this up so it looked something like this. Get
rid of misleading names, make an attempt to help the user of the macro do
his thing.
------------------
#include <stdio.h>

/*--------------
compound statement.
Precondtions: an int named i exisits.
post condtions: result is contained in parameter a
side effects: i is modified

i=b-1
a = a + sigma b+i
i= 0
---------------------*/
#define SUM1(a, b) \
{ \
for(i=0; i<b; i++) \
a+= b+i; \
}
//===============
main()
{
int i;
int k=0;

SUM1(k, 10);

printf(" %d", k);
}





J. J. Farrell 05-08-2007 08:20 PM

Re: #defines
 
On May 7, 11:39 pm, raghu <ragavaku...@gmail.com> wrote:
>
> I am using macro functions in my project the code is
> working correctly, But I have a doubt wheather the macro functions
> creates a context switching as in the case of normal functions. For
> example see the code below:
>
> #define funct(dest, src) {\
> for( i = 0; i< 10; i++ )\
> dest += (src + i);\
>
> }
>
> main()
> {
>
> int i, k;
>
> funct(k , 10);
>
> printf(" %d", k);
>
> }
>
> In this case 'int i' was not declared in funct but it works. And if in
> main() 'int i' was not declared and declared in funct defination also
> it works. Is there any context switch happening.


Macros do a simple text substitution very early in the compilation,
long before the compiler is thinking about things like "functions". By
the time the main part of the compiler starts work on this code,
funct(k, 10) has been replaced by the expansion of the macro.

You're confused about terminology. I don't know what you're trying to
ask about when you use the term "context switch", but it doesn't
appear to have anything to do with what most people mean by that term.
Are you perhaps asking about the scope of variables? If so, that's
again answered if you remember that macros are implemented as text
substitution very early in the compilation.



All times are GMT. The time now is 10:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.