Exforsys
+ Reply to Thread
Results 1 to 4 of 4

New Notation

This is a discussion on New Notation within the C and C++ forums, part of the Programming Talk category; I know that # is used for preprocessor directive in C programming language. But recently I saw in my friend’s ...

  1. #1
    cyrus is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    128

    New Notation

    I know that # is used for preprocessor directive in C programming language. But recently I saw in my friend’s code a new operator with notation as ## but I could not understand what it meant and the usage of the same. Can some one share your experience of this operator ## which would help all members to learn about this new notation.


  2. #2
    Mukhtar Ahmad is offline Junior Member Array
    Join Date
    May 2007
    Answers
    12
    The # and ## operators are used with the #define macro. Using # causes the first argument after the # to be returned as a string in quotes. Using ## concatenates what's before the ## with what's after it.
    --------------------------------------------------------
    #define to_string( s ) # s

    will make the compiler turn this command

    cout << to_string( Hello World! ) << endl;

    into cout << "Hello World!" << endl;
    ---------------------------------------------
    Here is an example of the ## command:

    #define concatenate( x, y ) x ## y
    ...
    int xy = 10;

    cout << concatenate( x, y ) << endl;

    into

    cout << xy << endl;


  3. #3
    cyrus is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    128
    It was a great explanation on the notation #. Can you give some example in C programming language of this notation #. This would help me to learn more deep about this notation usage.


  4. #4
    Mukhtar Ahmad is offline Junior Member Array
    Join Date
    May 2007
    Answers
    12
    #define to_string( s ) # s
    #include <iostream.h>
    void main(void){

    cout << to_string( string wiout quotes ) << endl;

    // cout requires quoted string and using macro to_string will return the same

    }

    ------------------------------------------------
    #define concatenate( x, y ) x ## y

    #include <iostream.h>
    void main(void){
    in ab = 20;

    cout << "The your age is " << concatenate(a,b)<<endl;

    // ab is variable and u want to print is value
    //the concatinate macro just do the same and result will be ab which is a
    //varibale declared by you as int
    // remember output by cancatinate is not a quoted string and will not be
    // string in any case it will be just concatinated input values

    if any confusion email me in detail at maksunn@gmail.com
    }


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...