How to concatenate a string and int in C

2 Answers

0 votes
#include <stdio.h>

int main() {
    int i = 738;
    char s[32] = "c programming";
    char buf[64] = "";

    snprintf(buf, 64, "%s - %d", s, i);
    printf("%s\n", buf);

    return 0;
}




/*
run

programming - 738

*/

 



answered May 2, 2021 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
int main() {
    char str[] = "Programming";
    int n = 31;
    char numStr[32]; 
    char result[64]; 
 
    // Convert integer to string
    sprintf(numStr, "%d", n);
 
    // Concatenate strings
    strcpy(result, str);
    strcat(result, numStr);
 
    printf("%s\n", result);
 
    return 0;
}
 
  
/*
run:
  
Programming31
   
*/

 



answered Jun 17, 2025 by avibootz

Related questions

2 answers 93 views
3 answers 82 views
1 answer 156 views
156 views asked May 3, 2021 by avibootz
2 answers 99 views
1 answer 128 views
1 answer 161 views
1 answer 155 views
...