Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Monday, March 8, 2010

    Operators in Preprocessor:

    * The 2 operators used in the preprocessor are

    1. #
    2. ##

    1.# operator:

    * If the macros are present with in the quoted strings it is not replaced by the #defined macro.
    * But if the macroname is preceded by a # then the macro is expanded into a quoted string with the parameter replaced by the actual argument
    * Eg:
    #include
    #define toprint(expr) printf(#expr "=%d\n",expr)
    int main()
    {
    int x=10,y=5;
    toprint(x/y);
    return 0;
    }
    when toprint(x/y);is invoked the macro is expanded in to
    printf("x/y" "=%d\n",x/y);
    and the strings are concatenated.so the statement becomes
    printf("x/y=%d\n",x/y);
    The output of the above program is x/y=2
    * In the above example instead of #expr if only expr is present like
    #define toprint(expr) printf("expr=%d\n",expr) then the output is expr=2

    2.## operator:

    * The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion.
    * If a parameter in the repacement text is adjacent to a ##, the parameter is replaced by the actual argument,the ## and surrounding white space are removed,and the result is rescanned
    * Example The macro Join concatenates its two arguments
    #define Join(front,back) front##back
    now Join(hello,100) resuts hello100.

    NULL Directive

    * #
    * A preprocessor line of the form # has no effect
    * if the line has only #symbol the preprocessor ignores the line

    0 comments:

    Post a Comment