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

    nerdy_me

    Member

    Updated: Oct 25, 2024
    Views: 1.1K
    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?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • nerdy_me

    MemberMay 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?
    Are you sure? This action cannot be undone.
    Cancel
  • synergynext

    MemberMay 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
    Are you sure? This action cannot be undone.
    Cancel
  • nerdy_me

    MemberMay 22, 2011

    This question was asked by external in viva.I was searching its answer but couldn't find.😐😐
    Are you sure? This action cannot be undone.
    Cancel
  • nerdy_me

    MemberMay 22, 2011

    I don't know about "##" .Is it signifies something in C language or it has no use?
    Are you sure? This action cannot be undone.
    Cancel
  • vik001ind

    MemberMay 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 ##.
    Are you sure? This action cannot be undone.
    Cancel
  • vinsocorp

    MemberMay 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........
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register