Thread: Concatination of chars

  1. #1
    Registered User
    Join Date
    Mar 2008

    Concatination of chars

    I am having trouble concatenating 2 chars
    char a = 'a';
    char b = 'b';
    strcat (a,b);
    cout  << a;
    what is the proper way of doing something like this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    You can't concatenate two chars into a string of two chars unless you have a string to store it in [and strcat do not take inputs of char anyways].

    Either of these would work:
    Using C++ properly:
    string a("a");
    string b("b");
    a += b;  // or a = a + b; if you like typing a lot. 
    cout << a;
    or standard C with a bit of C++ for output:
    char a[3] = "a";
    char b[2] = "b";
    strcat(a, b);
    cout << a;
    Or using indexes to arrays:
    char a[3] = "a";
    char b = 'b';
    a[1] = b;
    a[2] = 0;   // Just in case. It should really be zero from the a[3] = "a"; line.
    cout << a;
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM