Hi.

I am trying to merge 2 chars. in C but i don't know how to do it.

My code:

``````char dd[1], aa[2];

dd[0] = "2";
dd[1] = "3";

aa[] = dd[0] + dd[1];``````

So it aa[] should show: 23 but it gives error. Maybe i am doing it wrong. Please help. Also this is just demo code, i am actually inputting the values of dd[0] and dd[1] from user using getche() function

Thanks.

6
Contributors
14
Replies
15
Views
9 Years
Discussion Span
Last Post by Murtan

char dd[1];

That's wrong. You're allocating memory for only one character. Should be dd[2].

aa[] = dd[0] + dd[1];

This statement adds the ASCII values of '2' and '3'. So your result will be 50(ASCII value of 2) + 51(ASCII value of 3). And the result can't be just assigned to an entire array.

If you just wanna merge two chars, you can use:

``````aa[0] = dd[0];
aa[1] = dd[1];``````

But I'm not sure if that's what you're looking for. Maybe more info about what you're trying to do would help.

You use the standard C functions strcat()

Hi,

I am using a custom function in which I have made so that the user can input only 1 char. and that is also just a numeric char. so meaning that user can only enter from 0 to 9.

I am doing like following:

``````char dd[2];

dd[0] = getinp(); // say user enetered 2
dd[1] = getinp(); // say user enetered 3``````

Now I have to merge the both values so that it becomes 23 and I can check whether the value is less than 30 or not. How to achieve this ?

If i could just do the following:

``````aa = dd[0] + dd[1]; // aa will then have 23
if (aa > 30)
{
printf ("Value should not be more than 30");
}
else
{
printf ("Thanks");
}``````

This is really what I am trying to do.

I am using a custom function becuase it does'nt allows a user to enter anything else other than numbers.

Thanks

If dd[0] and dd[1] contain the character they entered, the character is NOT the numeric value. (For example in ASCII '0' is 48)

So if the example is dd[0] = '2' and dd[1] = '3' then `aa = dd[0] + dd[1];` would give aa the value 50 + 51 or 101.

If you want the numeric value I usually do something like `(dd[0] - '0')` which in our case would be 2.

So if we did that as we went, we could write

``aa = (dd[0] - '0') + (dd[1] - '0')``

There, now aa is 2 + 3 which makes aa 5. (wait, didn't you say it should be 23? maybe I missed something.)

I guess I could have done:

``aa = 10 * (dd[0] - '0') + (dd[1] - '0')``

That would be 23.

You could probably even do it without the array of characters (unless you needed them for something else):

``````aa = (getinp() - '0');
aa = 10 * aa + (getinp() - '0');``````

Hi,

``aa = 10 * (dd[0] - '0') + (dd[1] - '0');``

It gives me error:
Lvalue required

What does that means ?

Thanks.

``````char onetwo[3]; /* for one, two, zero chars */
char one, two;
one = getche();
two = getche();
onetwo[0] = one; /* array indicies started from 0 */
onetwo[1] = two;
onetwo[2] = '\0'; /* c-string terminated zero byte */
/* Now you have well-formed c-string */
printf("echo: %s\n",onetwo);``````

Remember: a single char is not a string in C, a c-string is an array of chars with zero byte at the end of its contents.

HI,

I am a php and VB programmer and its very easy to do this task in those languages.

VB: a = a & b
php: a = a . b;

What the syntax to do it in C ?

Arrrgh.

Your original program declared aa as an array of characters:

``char dd[1], aa[2];``

In your most recent post, you treated aa as an integer:

``````aa = dd[0] + dd[1]; // aa will then have 23
if (aa > 30)``````

So I did too. It is not permitted to assign to the array.

``````char dd[2];
int aa;``````

>What does that means ?
It means you need to learn a little be more about array of chars and what they represent.
As far as the compiler is concerned aa is not a proper variable to re-assign the kind effect resulted from `10 * (dd[0] - '0') + (dd[1] - '0')`

one = getche();
two = getche();

No reason to use a non-portable function when getchar() could have do.

In C, you make your own strings if you're taking input one character at a time.

If you wrote:

``````char dd[3];
dd[0] = getinp();
dd[1] = getinp();
dd[2] = '\0';``````

Now dd is a real C string. If you wanted to assign it to another string (of sufficient size or bad things happen) you could `strcpy(otherstring, dd);` But your problem description implied that you don't really need the string, you want the number that it would represent (which is why my previous posts look like they did.)

No reason to use a non-portable function when getchar() could have do.

Yes, of course, but there is getche in the original post.
Let him/her shoot him/herself in the foot ;)...

hi if u want to merge two strings
"string.h" name as "strcat"
the function gives concatnation or
merging of two srings

Hi,

Its confusing me. I am new to this. Help me out! Can't someone help me fix my program so I can achieve what i want to do ? It will be highly appericiated. 2 days I have done several experiments to just merge the things i have already tried strcat, strcpy and what not but its not working. Help me! Please.

Thanks.

You have been given several suggestions. I haven't seen anything that you've tried, all I've seen are comments that it isn't working -- and I followed those up with what was wrong and I thought I told you how to fix it.

Post the code as it now exists and please confirm whether you want a string with the two characters in it ("23"), or the numeric value of the number formed by the two characters (23). If you want to perform a numeric comparison (say to see if the value they entered was < 30) then you need the numeric value.

[code=c] /* Your code here */

[/code]

PS- It may have felt like 2 days, but your first post was 13 hours ago.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.