How to store an int in a char array with C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
  
/*
Type	            Size (bytes)	      

int	                at least 2, usually 4
char	            1	                  

*/
  
   
int main(void)
{
    int n = 9801273;
    char arr[4];
    int same_n;
     
    memcpy(arr, &n, sizeof(n));
    memcpy(&same_n, arr, sizeof(same_n));

    printf("%d\n", same_n);

    return 0;    
}

 
 
/*
run:
 
9801273
 
*/

 



answered Apr 12, 2024 by avibootz
...