How to append int to int in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
  
int main(void)
{
    int a = 10, b = 938; 
    char str1[8] = "", str2[8] = "";
      
    sprintf(str1, "%d", a);
    sprintf(str2, "%d", b);
     
    strcat(str1, str2);
    
    // long int strtol(const char *str, char **endptr, int base) 
    a = (int)strtol(str1, NULL, 10);
 
    printf("%d", a);
      
    return 0;
}
   
    
    
    
/*
run:
    
10938
  
*/

 



answered Oct 19, 2023 by avibootz
edited Oct 19, 2023 by avibootz
0 votes
#include <stdio.h>

unsigned concatenate(unsigned x, unsigned y) {
    unsigned pow = 10;
    
    while(y >= pow)
        pow *= 10;
        
    return x * pow + y;        
}
 
int main(void) {
    int a = 10, b = 938; 
      
    a = concatenate(a, b);
     
    printf("%d", a);
 
    return 0;
}
 
 
 
 
 
/*
run:
 
10938
 
*/

 



answered Oct 19, 2023 by avibootz

Related questions

2 answers 232 views
232 views asked Oct 19, 2023 by avibootz
2 answers 240 views
2 answers 257 views
2 answers 282 views
282 views asked Oct 19, 2023 by avibootz
2 answers 264 views
264 views asked Oct 19, 2023 by avibootz
1 answer 195 views
195 views asked Oct 19, 2023 by avibootz
2 answers 256 views
256 views asked Oct 19, 2023 by avibootz
...