Thread: Combining 2 chars

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    78

    Combining 2 chars

    Hello,

    I am trying to combine to chars together, having a big of problem

    char char1;
    char char2;


    char1 = 'one';
    char2 = ' two';


    char1 = (char1[3] + char2[0];




    printf("%s",char1);
    what's the correct code?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    'one' is not a single char, or a char string.

    You cannot use the '+' operator to combine two strings.

    Also, post code as CODE blocks not QUOTE blocks!

    Look at this code, but be cautious about buffer overflow. More code would need to prevent overflow.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char char1[20] = "one";
      char char2[10] = " two";
    
      // Or char *char2 = " two";   char2 is a pointer to a constant string
      // char1 must be a char array large enough to hold both strings
    
      strcat(char1, char2);
    
      printf("%s\n",char1);
    
      return 0;
    }
    You need to study a good up to date book on the C Programming Language before writing C code!
    Last edited by rstanley; 10-13-2023 at 01:05 PM.

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    thanks,

    so what exactly is '#include <string.h>' used for?

    is it for '
    strcat' can be used?
    Last edited by WaterSerpentM; 10-13-2023 at 03:38 PM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    thanks,

    so what exactly is '#include <string.h>' used for?
    You need to include the correct header file for the declarations of Standard Library functions.

    In this case you need to #include string.h for the declaration of the string concatenation function, strcat().

    Please see the online manpage for strcat(), or if on Linux, use the command line command, "man strcat"

    Once again, if you study a good up to date book on the C Programming Language, you would learn all about this an all the other details of the language!

  5. #5
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    Quote Originally Posted by rstanley View Post
    You need to include the correct header file for the declarations of Standard Library functions.

    In this case you need to #include string.h for the declaration of the string concatenation function, strcat().

    Please see the online manpage for strcat(), or if on Linux, use the command line command, "man strcat"

    Once again, if you study a good up to date book on the C Programming Language, you would learn all about this an all the other details of the language!
    thanks dude

  6. #6
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    ive tried to combine itself, but dont work

    strcat(char3, char3);

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    There is a function called sprintf() that could simplify this even more.

    I assume this is a homework assignment.

  8. #8
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    Quote Originally Posted by rstanley View Post
    There is a function called sprintf() that could simplify this even more.

    I assume this is a homework assignment.
    its not an assignment, im learning how to calculate things right now

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    WaterSerpentM:

    If you are attempting to learn from painfully inadequate online tutorials, YouTube videos, or some book claiming to teach you in a short time, then THROW THEM AWAY!

    Short of taking a course in C Programming from a qualified instructor, you need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach, 2nd Edition
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 10:42 AM
  2. Combining several variables into one
    By h3ro in forum C++ Programming
    Replies: 39
    Last Post: 04-24-2008, 10:03 AM
  3. combining int's into single int
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 02:07 PM
  4. combining C and C++
    By volk in forum C Programming
    Replies: 7
    Last Post: 03-18-2003, 08:41 PM

Tags for this Thread