1

String manipulation in C is not something I'm used to, coming from higher level languages. In this situation, I want to be able to make a string of the form fooN where N is a variable representing a number. In Java, it would look something like

for (int N = 0; N < 5; n++) {
    System.out.println("foo"+N);
}

which will output

foo0
foo1
foo2
foo3
foo4

However, I don't know a straighforward way to do that in C. I could probably hack something out where I make a char[] and figure out how to fill it up, but what I have in mind doesn't seem elegant. Is there a simple way that the C language allows concatenating strings and int variables?

Thanks

3
  • 2
    Look up sprintf. It's similar to String.format. Commented Jun 10, 2020 at 0:24
  • 6
    printf("foo%d", N); Commented Jun 10, 2020 at 0:28
  • 2
    First you need to decide whether you're making a string, or just displaying a string. Your Java example conflates the two (you made a string before displaying it, because the syntax makes that easy). But in C, making a string, and displaying a string are very different tasks. Commented Jun 10, 2020 at 0:45

1 Answer 1

1

Is there a simple way that the C language allows concatenating strings and int variables?

Yes, use s*printf(). The trick is memory management.

  1. Use a fixed sized buffer.

    // Coarse calculation of maximum memory needed to string-ify an int
    #define INT_STRING_MAX (sizeof(int)*CHAR_BIT/3 + 3)
    #define FOO_SZ (3 + INT_STRING_MAX)
    
    char buf[FOO_SZ];
    sprintf(buf, "foo%d", n);
    println(buf);
    
  2. Allocate memory. Could use snprintf(NULL, 0, ... to calculate memory needs.

    int sz = snprintf(NULL, 0, "foo%d", n);
    char *buf = malloc(sz + 1);
    if (buf) {
      sprintf(buf, "foo%d", n);
      println(buf);
      free(buf);
    }
    
  3. Estimate and check memory needs with snprintf().

    // Assume 64-bit or narrower int and so needs at most 21 bytes
    #define FOO_SZ (3 + 21)
    
    char buf[FOO_SZ];
    int cnt = snprintf(buf, sizeof buf, "foo%d", n);
    if (cnt >= 0 && (unsigned) cnt < sizeof buf) {
      println(buf);
    } else {
      // wrong estimate
    }
    

The best choice depends on how simple, efficient & portable you want.

Sign up to request clarification or add additional context in comments.

2 Comments

note that for robust code you should also check sz > 0 , since snprintf can fail and return a negative error code. I have heard of this happening in situations such as a UTF-8 locale where the input contained invalid UTF-8. (And also this method won't work if the input can exceed INT_MAX)
@M.M All true. And more trouble should needed string exceed SIZE_MAX. With different locales, all bets are off too. The singular issue with detecting particular failures is how to handle them once found. IMO, knowing that tends to drive the design. For here and now - some of the basics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.