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

nerdy_me

nerdy_me

@nerdy-me-Xm58E6 Oct 25, 2024
Though single "#" is used to concatenate two strings even in d macros..i.e.
try in dev compiler ::
#include<cstdio>
#include<conio.h>
#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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • nerdy_me

    nerdy_me

    @nerdy-me-Xm58E6 May 20, 2011

    Though single "#" is used to concatenate two strings even in d macros..i.e.
    try in dev compiler ::
    #include<cstdio>
    #include<conio.h>
    #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

    @synergynext-FdHLd3 May 21, 2011

    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

    @nerdy-me-Xm58E6 May 22, 2011

    This question was asked by external in viva.I was searching its answer but couldn't find.😐😐
  • nerdy_me

    nerdy_me

    @nerdy-me-Xm58E6 May 22, 2011

    I don't know about "##" .Is it signifies something in C language or it has no use?
  • vik001ind

    vik001ind

    @vik001ind-rOaCSy May 22, 2011

    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<stdio.h>
    #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<stdio.h>
    #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

    @vinsocorp-4fscJ8 May 23, 2011

    ## 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........