Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > #defines

Reply
Thread Tools

#defines

 
 
raghu
Guest
Posts: n/a
 
      05-08-2007
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-08-2007
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.
 
Reply With Quote
 
 
 
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      05-08-2007

"raghu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Laurent Deniau
Guest
Posts: n/a
 
      05-08-2007
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.
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      05-08-2007
"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);
}




 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      05-08-2007
On May 7, 11:39 pm, raghu <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments