1. Home >
  2. Apps >
  3. Groups >

What is the use of "##" operator in C language?

Question asked by nerdy_me in #Programming on May 20, 2011
nerdy_me
nerdy_me 路 May 20, 2011
Rank D2 - MASTER

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?

Posted in: #Programming
nerdy_me
nerdy_me 路 May 20, 2011
Rank D2 - MASTER
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 路 May 21, 2011
Rank D1 - MASTER
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 路 May 22, 2011
Rank D2 - MASTER
This question was asked by external in viva.I was searching its answer but couldn't find.馃槓馃槓
nerdy_me
nerdy_me 路 May 22, 2011
Rank D2 - MASTER
I don't know about "##" .Is it signifies something in C language or it has no use?
vik001ind
vik001ind 路 May 22, 2011
Rank C1 - EXPERT
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 路 May 24, 2011
Rank E1 - BEGINNER
## 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........
Vinay Mishra
Vinay Mishra 路 Dec 26, 2019
Rank B2 - LEADER

Don't be confuse. Let me clear it..

'##' is known as Token-pasting operator.

As per name, this operator is used to paste the tokens. This operator works to allow any token as a argument and concatenate to form other tokens. It is a pre-processing event which can paste tokens.

Simple difference in '#' and '##' is:

鈥 # : It is known as 'Stringize operator'. It is used to convert a token into string. By this operator we can convert some text into string without using and quotes.

鈥 ## : It can be defined by simple example i.e This operator converts-

printf(鈥%d鈥, concat(x, y));

Into

printf鈥%d鈥, xy);

Hope you got it!

Divisha Madupalli
Divisha Madupalli 路 Dec 26, 2019
Rank C1 - EXPERT

## is used for concatenation like if you have聽

X=5,y=6

And you print X##y then it prints 56.

## is a preprocessor operator which is mostly used with the 聽# define in C .


# define sum(x,y) x##y .

It is written like this .

Teja Reddy
Teja Reddy 路 Dec 26, 2019
Rank C3 - EXPERT

The operator ## which is used to concatenate two strings or arguments by leaving no blank spaces between them聽

example #define demo(a,b) a##b

聽 聽 聽 聽 聽 聽 聽 聽 聽demo(c,out)<<"YOYO";


Mohana Chandra
Mohana Chandra 路 Dec 26, 2019
Rank C1 - EXPERT

## is called "Token-Pasting operator.

Tokens is the smallest units of a program. ## is used to concate/merge two tokens ,which are actual parameters ,to form a New token.聽

As ## concates the tokens ,this process also known as Token concatenation.

#define concat (x,y) x##y

int main ()

{ 聽 聽 int ab=20;

printf ("%d",concat (a,b));

}

The above code snippet gives the output 20


Kavya Burramolla
Kavya Burramolla 路 Dec 27, 2019
Rank C1 - EXPERT

## operator in C programming generally concatenates two tokens and pastes the single token. Hence called the Token pasting operator. For example : let one token be A whose value is 1 and other be B whose value is 2, then using ##operator as A##B, it results in the value of 12. Note that the resulted single token can be a variable, number.

While the #operator is to convert a text or a token into a string. Hence called stringise operator. For example: let a token A be the value of tree. Then #A will result in 鈥渢ree鈥, without using any extra quotation marks.聽

Noothan Kumar
Noothan Kumar 路 Jan 24, 2020
Rank C1 - EXPERT

Any programming language consists of a set of instructions(pre-written) called library functions. # means that you are essentially telling the compiler to put whatever follows into the program.

to put whatever follows into the program.

#include means that i am asking for the standard input output functions(printf and scanf) in my program.

Teja Reddy
Teja Reddy 路 Feb 25, 2020
Rank C3 - EXPERT

C language is used to develop system applications that forms major portion of operating systems such as Windows, UNIX and Linux. Operating systems, C compiler and all UNIX application programs are written in C language.

  • Database systems
  • Graphics packages
  • Word processors
  • Spread sheets
  • Operating system development
  • Compilers and Assemblers
  • Network drivers
  • Interpreters
supriya VN gowda
supriya VN gowda 路 Feb 25, 2020
Rank C2 - EXPERT

# is used to include all the necessary libraries required by your program and each library includes set of functions. #include Library will have printf() like functions.聽

## is used along with#def and ## is used to concatenate two strings and these operator is known as token pasting operator. It is a preprocessor operator. These can be used with # define diff(a, b) a##b.

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up