Results 1 to 6 of 6

Thread: Concatenating two strings and a variable

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Concatenating two strings and a variable

    I'm converting a C program to VB. I know how to do my question in VB but I don't know how to do it in C

    I have two strings and a variable:

    String1 = "s_rgbn["
    String2 = "]"
    Variable1 = 133

    Let's say I want to print the above so it looks like this:

    s_rgbn[133]

    So, in VB I would do this:

    String1 & Variable1 & String2

    How would I do same in C


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Concatenating two strings and a variable

    Code:
    String1 = "s_rgbn[";
    String2 = "]";
    Variable1 = 133;
    
    
    String1 + Variable1 + String2;



  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Concatenating two strings and a variable

    Code:
      char String1[] = "s_rgbn[";
      char String2[] = "]";
      int Variable1 = 133;
    
      char buf[strlen(String1) + strlen(String2) + 10];
      sprintf(buf, "%s%d%s", String1, Variable1, String2);

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Concatenating two strings and a variable

    I finally wound up using something similar to what penagate posted:

    Code:
    printf("s_rgbn[%d] = %d", index, s_rgbn[index]);


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Concatenating two strings and a variable

    Yeah, that's better. I took your question title literally.

    More general tips (probably not applicable to your project right now): If you're not actually printing or formatting numbers, the other way to concatenate strings is strcat (or you could use memcpy or strcpy if you want). If the size of your input buffers is not guaranteed, use the size-limited versions of the functions: snprintf, strncat, etc.

  6. #6
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: Concatenating two strings and a variable

    or you could use memcpy or strcpy if you want
    Warning with using memcpy() in this context - it doesn't automatically make the new string null-terminated. The number of chars specified to copy needs to include the null terminator from the source string. If the source buffer is not null-terminated then one will need to be appended to the destination buffer if it is to be treated as a string.

    Also consider the secure versions of the string functions - strcat_s(), strcpy_s() etc.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width