# and ## Operators in C https://www.skillvertex.com/blog Tue, 05 Mar 2024 12:49:07 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png # and ## Operators in C https://www.skillvertex.com/blog 32 32 # And ## Operators In C https://www.skillvertex.com/blog/and-operators-in-c/ https://www.skillvertex.com/blog/and-operators-in-c/#respond Tue, 05 Mar 2024 12:49:07 +0000 https://www.skillvertex.com/blog/?p=3294 Read more]]>

Table of Contents

# And ## Operators In C

Stringizing operator (#)

This stringizing operator is a preprocessor operator which will cause a corresponding actual argument and will be enclosed in double quotation marks. This # operator is known as the stringize operator. This operator will change the argument that will precede into a quoted string. This is known as the stringification operator. This is mostly used with macros in C.

Example– to show the usage of the stringizing operator(#).

The C code provided below will show the usage of the stringizing operator(#).

// C program to illustrate (#) operator 
#include <stdio.h> 
  
// Macro definition using the stringizing operator 
#define mkstr(s) #s 
int main(void) 
{ 
    // Printing the stringized value of "Skillvertex" 
    printf(mkstr(Skillvertex)); 
    return 0; 
}

Output

Skillvertex

Explanation

The preprocessor will change the line printf (mkstr(Skillvertex)); into printf(“Skillvertex”);

Token-pasting operator (##)

The Token-pasting operator will enable tokens which will be used as actual arguments and further, it will be linked to form other tokens. Therefore, it can merge two tokens into one during the expansion of macros. This process is known as token pasting or token concatenation.

The ‘##’ pre-processing operator in C/C++ is used for something called “token pasting.” When you expand a macro, this operator combines the two tokens on either side of ‘##’ into a single token. This combined token replaces the ‘##’ and the original two tokens in the macro expansion. This can be useful for creating new tokens or identifiers during the preprocessing stage.

Example- to show the usage of Token-pasting operator (##)

// C program to illustrate (##) operator 
#include <stdio.h> 
  
// Macro definition using the Token-pasting operator 
#define concat(a, b) a##b 
int main(void) 
{ 
    int xy = 30; 
  
    // Printing the concatenated value of x and y 
    printf("%d", concat(x, y)); 
    return 0; 
}

Output

30

Explanation:

The preprocessor will change  printf(“%d”, concat(x, y)); into printf(“%d”, xy);

Application of Token-pasting operator (##)

The ‘##’ operator in C/C++ macros is used to concatenate or join actual arguments during macro expansion. If a parameter in the replacement text is next to a ‘##’, the parameter is replaced by the actual argument. The ‘##’ and any surrounding white space are removed, and the combined result is then re-scanned by the preprocessor. This can be helpful for generating new tokens or combining values in a flexible way during preprocessing.

FAQ- # and ## Operators in C

Q1. What is the ## operator in C?

Ans. In the C language, the ‘##’ operator serves as a token-pasting operator in the preprocessor. It is employed to combine two tokens during the preprocessing stage of compilation. So, essentially, the ‘##’ operator concatenates the string “foo_” with the token “a” to create the string “foo_a,” and similarly, it combines “bar_b” by joining “bar_” and “b” together.

Q2. What does & and * mean in C?

Ans.In C and C++, when you see the symbol “&,” it means “get the memory address of.” This is often used in function parameters to allow functions to change the original variable. By default, C and C++ pass copies of variables to functions, but using the “&” lets you pass the original variable’s address instead, so changes made in the function affect the original.
On the other hand, when you see the “” symbol with a pointer, it means “get the value stored at that memory address.” So, if you have a pointer named ptr, “ptr” gets the value located at the memory address pointed to by `ptr.”

Q3. What is the logical && operator in C?

Ans. The logical AND operator (&&) in programming is a binary operator that works with two values and produces a result. It returns true or 1 only if both of its operands are true. If either one is false, it returns false or 0. In simpler terms, both conditions must be true for the AND operator to yield a true result; otherwise, it gives a false result.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/and-operators-in-c/feed/ 0