#defines

R

raghu

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
 
I

Ian Collins

raghu said:
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.
 
F

Fred Kleinschmidt

raghu said:
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.
 
L

Laurent Deniau

CBFalconer said:
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.
 
O

osmium

"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. J. Farrell

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top