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?
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?
Only by the cross are you saved...
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.
My best code is written with the delete key.
You need to define a character buffer, likeOriginally 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 */
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
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?
Only by the cross are you saved...
>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 */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*