what is the use of "##" in c language?

Though single "#" is used to concatenate two strings even in d macros..i.e.
try in dev compiler ::
#include
#include
#define delhi(s1,s2) s1#s2
using namespace std;
main()
{
char sh[]=delhi("hello","ceans");
printf("%s",sh);
system("pause");
}
but I found on net that ## is used to concatenate two strings which is wrong as # is concatenating the strings?

Replies

  • nerdy_me
    nerdy_me
    Though single "#" is used to concatenate two strings even in d macros..i.e.
    try in dev compiler ::
    #include
    #include
    #define delhi(s1,s2) s1#s2
    using namespace std;
    main()
    {
    char sh[]=delhi("hello","ceans");
    printf("%s",sh);
    system("pause");
    }
    but I found on net that ## is used to concatenate two strings which is wrong as # is concatenating the strings?
  • synergynext
    synergynext
    the # symbol distinguishes the Pre-processor commands. for eg to include library, define a macro etc
    where did u see ##? kindly share the link
  • nerdy_me
    nerdy_me
    This question was asked by external in viva.I was searching its answer but couldn't find.😐😐
  • nerdy_me
    nerdy_me
    I don't know about "##" .Is it signifies something in C language or it has no use?
  • vik001ind
    vik001ind
    There is a serious flaw in your concept. When you run your program, the output is --> hello"ceans" & not helloceans.
    When we use a preprocessor directive like this one #define A a, every occurrence of 'A' is replaced with 'a' during preprocessing.
    The significance of # before the parameter is that when preprocessing occurs, it is expanded into a quoted string with the parameter replaced by actual argument.
    Try a simple example,
    #include
    #define print(s2) printf(#s2)
    void main()
    {
    print(hi);
    }
    
    It will print hi as print(hi) is preprocessed as printf("hi"). Similarly, delhi("hello","ceans") becomes "hello"""ceans"". The third & forth double quote gets nullified. Now, the combined expression is sh[]="hello"ceans"" that's why the output is hello"ceans".

    In case of ##, the situation is completely different. In this case the parameters are simply concatenated.

    Try this,
    #include
    #define F(a,b) a##b
    
    void main()
    {
          int x =2,xy=4;
           printf("%d",F(x,y));
    }
    
    The output will clarify the role of ##.
  • vinsocorp
    vinsocorp
    ## is used to concatenate as like
    ex: a=4,b=5,ab=6;
    a##b compiler read as ab..
    if you print the result its takes the varianle as ab;and its produces 6........

You are reading an archived discussion.

Related Posts

So i have a trojan that has hijacked my ip and maybe other things. Its a trojan.dropper so its automatically uploading viruses to my computer in the background. Ive tried...
Can you please suggest any books for making me write application program in micro-controller, As I am totally innocent about writing application programs I need to start from scratch, interested...
Scientists have used X-ray computed tomography to produce 3D images of a 49 million-year-old spider trapped inside an opaque piece of fossilised amber resin. Read more: Scientists get a 3D...
Quote: A new £5m initiative aims to explore how connecting everyday technologies such as cars and electricity meters to the internet could make them more useful. Endquote Read more: £5m...
Identify him ?which famous structure he designed? Image Upload !