Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does '#' exactly do in C? [duplicate]

I have a program.

#include <stdio.h>

#define f(a,b) a##b
#define g(a)   #a
#define h(a) g(a)

int main()
{
      printf("%s\n",h(f(1,2)));
      printf("%s\n",g(f(1,2)));
      return 0;
}

This program working properly and giving output as:

12
f(1, 2)

I don't understand how compiler giving this output.

What is the function of # in a##b and #a?

like image 828
gangadhars Avatar asked Oct 05 '13 13:10

gangadhars


People also ask

What is the plural form of the verb 'do'?

The subject of the sentence is the plural noun changes. Therefore, the verb must be plural. As the post states, do is the plural form of the verb. Please help. Do/Does either of you have a pen? While the rule states that the third person singular should be used, the fact that the people are being addressed directly (you) makes me want to use ‘do’.

What does “/” mean in writing?

In writing, the “/” symbol is used to stand in for “and”, “or” or “and & or”. You can use it to join two things, meaning “and”. You can also use it to denote two options, meaning “or”. Or you can use it to mean both, “and & or”. In writing, therefore, “/” can be an incredibly powerful ally if you know how to use it well.

Is it “do” or “does” the verb do?

The verb “do” is among the most common English verbs, and like most verbs we use a lot, it’s irregular. “Do” and “does” are both forms of the verb “do” in the simple present, so which is correct, “do” or “does?” When you talk about yourself, you should say, “I do” as in “I do the dishes,” not “I does the dishes.”

What does what all mean in a sentence?

“What all” refers to something in all its ramifications. The sentence roughly means “Give me a full explanation of what the clouds do.” how do we recognise noun in a sentence? Is subject always a noun? Which statement is correct? regards! A noun is a person, place, thing, or idea. A subject is always a noun or pronoun.


3 Answers

The ## concatenates two tokens together.

The important thing is it can only be used in the preprocessor.

The # operator is used to stringify tokens.

For example:-

#(a ## b) which becomes #ab which becomes "ab"

So h(f(1,2)) becomes "f(1,2)"

Also note that # and ## are two different operators.

The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ##, the parameter is replaced by the actual argument, the ## and surrounding white space are removed, and the result is re-scanned.

Also check this Concatenation for more details.

From here:-

Stringification

Sometimes you may want to convert a macro argument into a string constant. Parameters are not replaced inside string constants, but you can use the '#' preprocessing operator instead. When a macro parameter is used with a leading `#', the preprocessor replaces it with the literal text of the actual argument, converted to a string constant. Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification.

There is no way to combine an argument with surrounding text and stringify it all together. Instead, you can write a series of adjacent string constants and stringified arguments. The preprocessor will replace the stringified arguments with string constants. The C compiler will then combine all the adjacent string constants into one long string.

like image 169
Rahul Tripathi Avatar answered Oct 19 '22 14:10

Rahul Tripathi


let me break it down for you :

#define f(a,b) a##b //2 this macro is evaluated first with a = 1 and b = 2 it concatenates them and returns 12
#define g(a)   #a //4 g turns 12 into "12" (string)
#define h(a) g(a) //3 back to h which now has a = 12 and call g()

int main()
{
      printf("%s\n",h(f(1,2)));//1 printf calls the macro h() and gives it the macro f() as an argument 
      printf("%s\n",g(f(1,2)));// g here turns f(1,2) into "f(1,2)" (string)
      return 0;
}
like image 37
Farouq Jouti Avatar answered Oct 19 '22 14:10

Farouq Jouti


## is called "token-pasting" operator, or "merging" operator which can be used to combine two tokens to form an actual argument.

# is called Stringizing Operator which "converts macro parameters to string literals without expanding the parameter definition".

These are generally called Preprocessor Operators. There exists a few more preprocessor operators like these. Check out Preprocessor Operators in C (http://msdn.microsoft.com/en-us/library/wy090hkc.aspx) for more explanation.


Also checkout http://msdn.microsoft.com/en-us/library/3sxhs2ty.aspx and the "see also" section of that page for more information on C Preprocessor.

like image 27
Lid Avatar answered Oct 19 '22 14:10

Lid