How can I concatenate characters in C?
and if those 2 characters are numbers for example, say char1 = 1, char2 = 4
so if i merge them, it will become 14
can i then manipulate htis value to perform mathematical calculations?
Printable View
How can I concatenate characters in C?
and if those 2 characters are numbers for example, say char1 = 1, char2 = 4
so if i merge them, it will become 14
can i then manipulate htis value to perform mathematical calculations?
Make a string out of the characters, use strtol to turn the result into a number, repeat as necessary for other values, then do your calculations.
You need to define a character buffer, likeQuote:
Originally posted by fkheng
How can I concatenate characters in C?
and if those 2 characters are numbers for example, say char1 = 1, char2 = 4
so if i merge them, it will become 14
can i then manipulate htis value to perform mathematical calculations?
char buf[10]; /* to hold a string of 9 characters */
Then add char1 and char2 into the buffer:
If you know they are numbers, use atoi() to convert them from a string into intCode:buf[0] = char1;
buf[1] = char2;
buf[3] = '\0'; /* End the 'string' with a null */
okay, er........say i have used atof to convert a string to float
it is a float number
and it looks like e.g. 60.00000
from this, what if i just want 2 floating points, i mean just 60.00
how do i shorten it to this?
>and it looks like e.g. 60.00000
I might assume that looks means you have chosen the default precision by which printf displays the number's representation.
>from this, what if i just want 2 floating points, i mean just 60.00
>how do i shorten it to this?
If the above assumption is true, then you do nothing to the number; you change the precision by which you tell printf to display its representation. For example,Code:#include <stdio.h>
int main(void)
{
double number = 60.0;
printf("number = %f\n", number); /* default precision */
printf("number = %.2f\n", number); /* 2 digits after decimal point */
return 0;
}
/* my output
number = 60.000000
number = 60.00
*/