#defines

Discussion in 'C Programming' started by raghu, May 8, 2007.

  1. raghu

    raghu Guest

    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
     
    raghu, May 8, 2007
    #1
    1. Advertising

  2. raghu

    Ian Collins Guest

    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.
     
    Ian Collins, May 8, 2007
    #2
    1. Advertising

  3. "raghu" <> wrote in message
    news:...
    > 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
     
    Fred Kleinschmidt, May 8, 2007
    #3
  4. 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.
     
    Laurent Deniau, May 8, 2007
    #4
  5. raghu

    osmium Guest

    "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);
    }
     
    osmium, May 8, 2007
    #5
  6. On May 7, 11:39 pm, 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.


    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.
     
    J. J. Farrell, May 8, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    3,456
    Brian Drummond
    Feb 24, 2006
  2. Rob R. Ainscough
    Replies:
    1
    Views:
    416
    =?Utf-8?B?RGVhc3Vu?=
    Jun 27, 2005
  3. John Leonard
    Replies:
    2
    Views:
    385
    Rick Genter
    Sep 28, 2003
  4. exquisitus
    Replies:
    12
    Views:
    22,703
    Tilman Bohn
    Feb 19, 2005
  5. theotyflos
    Replies:
    3
    Views:
    495
    Thomas Matthews
    Feb 19, 2004
Loading...

Share This Page