Thread: Concatenating characters

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Concatenating characters

    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...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Concatenating characters

    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?
    You need to define a character buffer, like
    char buf[10]; /* to hold a string of 9 characters */

    Then add char1 and char2 into the buffer:
    Code:
    buf[0] = char1;
    buf[1] = char2;
    buf[3] = '\0';    /* End the 'string' with a null */
    If you know they are numbers, use atoi() to convert them from a string into int
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    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...

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM