## operator in c language

Hello friends,
can anyone explain about the ##operator in c language and how it works??

Replies

  • Vishal Sharma
    Vishal Sharma
    madhuri thalluri
    Hello friends,
    can anyone explain about the ##operator in c language and how it works??
    I think you couldn't find any documentation regarding this as it is very poorly documented and hence you had to post a question on that.
    Actually, ## is called the token pasting operator and concatenates the characters defined around it (excluding the white spaces). It is generally used in your definition macro.
    ex:
    #define my_name v ##       i ##s        ##hal
    the above statement will define my_name as vishal, hope you understood what happened above.

    ## can also be used to do beautiful things like executing your code without actually typing "main" anywhere in the code. However, Internally ## creates a string main. Take a look at this example, try copying it in an editor and run it See what happens

    #define vishal(s,t,u,m,p,e,d) m##s##u##t
    #define freak vishal(a,n,i,m,a,t,e)
    #include
    
    int freak() {
        printf("hello world");
        getchar();
        return 0;
    }
    
    In the above code we define a function vishal with 7 parameters s,t,u,m,p,e,d whose task is to concatenate the parameters m,s,u,t. Now, in the second macro definition, we are calling the function vishal() as vishal(a,n,i,m,a,t,e), whose result is stored in a variable freak. When this executes, It concatenates the parameters m,s,u,t which are m,a,i,n in the calling function (line 2). So, freak now holds the string "main"
    And hence the code executes perfectly.

    Hope, I was clear.

You are reading an archived discussion.

Related Posts

i need guidelines or if possible a soure code for my final year project :luggage surface detection using wavelet and neural network. The software ti be use is Matlab. Thanks....
How the Zener works??? A P-N junction diode lets current flow in only one direction and needs to be installed in the correct direction. A zener is different than it....
Fork of buildroot for Raspberry Pi from heaven! 😁 If you don't know what is buildroot, read from here : https://buildroot.org/downloads/manual/manual.html & then use your knowledge for RPi-buildroot. Here's the...
Quote: This is the big year for crystals — but we're not talking about Crystal smartphonesor celebrity diamond rings. We're talking about the International Year of Crystallography, and the scientific...
Mexican scientists at the Centre for Research and Advanced Studies (CINVESTAV) are trying to build a prosthetic arm that can move when the human brain thinks about its motion. The...