## how to concatenate two positive integers using macros?

I need to write a program to join two positive integers to form another positive integer using macros. Example , if `x=12`

, `y=35`

and the answer should be `1235`

. How should I do this?

You simply need to use the ## operator

It is often useful to merge two tokens into one while expanding macros. This is called token pasting or token concatenation. The ‘##’ preprocessing operator performs token pasting. When a macro is expanded, the two tokens on either side of each ‘##’ operator are combined into a single token, which then replaces the ‘##’ and the two original tokens in the macro expansion. Usually both will be identifiers, or one will be an identifier and the other a preprocessing number. When pasted, they make a longer identifier. This isn’t the only valid case. It is also possible to concatenate two numbers (or a number and a name, such as 1.5 and e3) into a number. Also, multi-character operators such as += can be formed by token pasting.

From https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html

So, in your case, `#define CONCAT_NUMBER(FIRST, SECOND) (FIRST ## SECOND)`

should work

**How to concatenate two or more Integers using Macros?,** You can't -- the preprocessor has no idea about variables and what values you're going to assign to them when the program runs at some arbitrary time after the� It is possible for preprocessor defined integers. The preprocessor needs a call to another function to expand. You do it as follows: #define I_BASE_CONCAT(x,y) x ## y #define I_CONCAT(x,y) I_BASE_CONCAT(x,y) There it is. Now if you call I_CONCAT it will expand it to x##y, but with the values of x and y.

Example , if x=12, y=35 and the answer should be 1235. How should I do this?

You need to shift `x`

left by some number of decimal digits that depends on the number of digits in `y`

. So what is the number of digits in `y`

? Well, it's the integer part of `log(y)`

. And to accomplish the shift, you need to multiply `x`

by 10 raised to that value. So the mathematical expression you need is, depending on the language you're using, something like:

finalValue = x*(10^^(floor(log(y))) + y;

Translate that into your language of choice and you should have what you want.

**Concatenation (The C Preprocessor),** It is often useful to merge two tokens into one while expanding macros. It is also possible to concatenate two numbers (or a number and a name, such As with stringizing, the actual argument is not macro-expanded first. Keep in mind that the C preprocessor converts comments to whitespace before macros are even� Given two integers n1 and n2, the task is to concatenate these two integers into one integer. Example: Input: n1 = 12, n2 = 34 Output: 1234 Input: n1 = 1, n2 = 93 Output: 193

If you are trying to pass variable x and y to the MACRO then this cannot be done. As Macro expansion happens at compile-time.

In case if you want to pass the direct value to the macro then concatenation can be done as follows-

`#define CONCATENATE(NUM_1, NUM_2) (NUM_1 ## NUM_2)`

**How to concatenate two integers into one integer!!,** I have code that concatenate two integers but if the 2nd integer is zero it [edit] Or even easier, just initialize "pow" to 1. First don't use pow as a variable name, there is a standard function that I figured it out with macros. Concatenation means joining two values or two strings together, similar to excel we use & or also known as ampersand operator to concatenate, two concatenate two strings we use & operator like String 1 & String 2, now there is an important thing to remember and that is while using the & operator we need to provide spaces or VBA will consider it as long.

You can do this with a simple macro if the numbers are integer literals. For the general case, you must compute the result or use a very cumbersome macro:

#include <stdio.h> /* Simple token pasting macro, x and y must be literals */ #define CONCAT(x,y) x ## y /* Elaborate macro: arguments are evaluated multiple times */ #define CONCAT1(x, y) ((y) + (x) * (\ (y) < 10 ? 10 :\ (y) < 100 ? 100 :\ (y) < 1000 ? 1000 :\ (y) < 10000 ? 10000 :\ (y) < 100000 ? 100000 :\ (y) < 1000000 ? 1000000 :\ (y) < 10000000 ? 10000000 :\ (y) < 100000000 ? 100000000 :\ 1000000000)) int main() { int x = 12; int y = 35; int result; /* Solution with simple macro only for literals */ result = CONCAT(12, 35); printf("%d\n", result); /* Solution with long macro */ result = CONCAT1(x, y); printf("%d\n", result); /* Generic solution */ result = x * 10; for (int i = y; i > 10; i /= 10) result *= 10; result += y; printf("%d\n", result); return 0; }

Output:

1235 1235 1235

As can be seen on Godbolt's Compiler Explorer, the compiler generates the same code for all 3 approaches because `x`

and `y`

have known values.

**Concatenation,** In the context of macro expansion, concatenation refers to joining two lexical units into Specifically, an actual argument to the macro can be concatenated with another variable or type, or a C keyword; it might even be the name of another macro, It is also possible to concatenate two numbers (or a number and a name,� 1. The CONCATENATE function below concatenates the string in cell A1 and the string in cell B1. 2. Use the & operator to produce the exact same result. 3. The CONCATENATE function below concatenates the string in cell A1, the string " and " (enclose text in double quotation marks) and the string in cell B1. 4.

**How to concatenate two Integer values into one?,** Given two integers n1 and n2, the task is to concatenate these two integers into one integer. Convert both numbers to string; Concatenate both strings into one, as this is comparatively easy using namespace std; How to Read and Print an Integer value in C++ � Count even and odd digits in an Integer� Given two integers M and N the task is to find the number formed by concatenating the binary equivalents of M and N i.e. M + N. Examples: Input: M = 4, N = 5 Output: 37 Binary equivalent of 4 is 100 and for 5 it is 101 after concatenation, the resultant binary number formed is 100101 whose decimal equivalent is 37. Input: M = 3, N = 4 Output: 28

It is also possible to concatenate two numbers (or a number and a name, such as 1.5 and e3) into a number. Also, multi-character operators such as += can be formed by token pasting. However, two tokens that don’t together form a valid token cannot be pasted together. For example, you cannot concatenate x with + in either order. If you try

In this tutorial, you will learn various ways to concatenate text strings, cells, ranges, columns and rows in Excel using the CONCATENATE function and "&" operator. In your Excel workbooks, the data is not always structured according to your needs.

##### Comments

- What language or environment are these macros in?
- I am using c language for this
- @Aye: you can accept one of the answers by clicking on the grey checkmark below its score
- @Caleb This works if the tokens are known at compile-time, even for number tokens, test it onlinegdb.com/B1mhWK2s4
- This will incure runtime-penalty and/or relies on the compiler to deduce that it does not change in value. A macro-based solution is more efficient.
- Don't you like my
`CONCAT1`

macro? - I executed and verified your code. It works and gives the expected output. The only limitation is the size.