CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@nerdy_me • 20 May, 2011

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?

@nerdy_me • 20 May, 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 • 21 May, 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 • 22 May, 2011 This question was asked by external in viva.I was searching its answer but couldn't find.😐😐
@nerdy_me • 22 May, 2011 I don't know about "##" .Is it signifies something in C language or it has no use?
@vik001ind • 22 May, 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 • 24 May, 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........
@Vinay Mishra • 26 Dec, 2019 • 3 likes

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 • 26 Dec, 2019 • 2 likes

## 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 • 26 Dec, 2019 • 2 likes

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 • 26 Dec, 2019 • 1 like

## 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 • 27 Dec, 2019 • 1 like

## 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 “tree”, without using any extra quotation marks. 

@Noothan Kumar • 24 Jan, 2020 • 1 like

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<stdio.h> means that i am asking for the standard input output functions(printf and scanf) in my program.

@Teja Reddy • 25 Feb, 2020

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 • 25 Feb, 2020

# is used to include all the necessary libraries required by your program and each library includes set of functions. #include<stdio.h> 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.

12k views

Related Posts

@sujeetha yoganathan · Jul 6, 2017

dear friends... i have got the following ranks 4254(category 1),3423(category 2) and 806(category 3) and my graduation cgpa is 8.20. i would like to attend embedded system design course. my...
5.9k views

@Guneet Khanuja · May 5, 2014

FashUpp, a social fashion platform startup, was founded by a trio team of Ankit Sachan, Ashish Kumar and Koustubh Sinhal - 2010 graduates from IIT Kanpur. The website lets you...
4.7k views

@akhil krishna · Sep 25, 2015

we are going to start a group in our college(central university). this group is to unite all present an past ece students in our college. and to create a platform...
11.8k views

@zion · May 28, 2012

class A { int a; int b; } class B extends A { int c; } class C extends B { int d; public int sum() { d=a+b+c; return d;...
13.8k views

@Neda Hashmi · Sep 23, 2014

A year ago, when i was in college, almost every branch were having certification course like SAP, autoCAD, MATLAB, embedded system etc, organised by training cell of our college which...
8.7k views