A int
will (almost) always be 32 bits, so you know the size required. Even if you do not know, in such cases it is best to allocate enough to the greatest possible. It's actually something so simple you should not allocate dynamically. Make the allocation as the vector and do not worry about it.
As the comments I made a code to at least demonstrate the operation (as missing information in the question, it will need adaptations):
#include <stdio.h>
int main(void) {
int inteiro = 3;
int tamanho = sizeof(int) * 8; //aqui acha o tamanho de um int
char binario[tamanho + 1]; //aqui aloca o vetor de char com espaço para o terminador
int i = tamanho - 1; //inicia o contador
while(i > 0) {
binario[i] = inteiro % 2 + 48; //acha o binário
inteiro /= 2;
i--;
}
binario[tamanho] = '#include <stdio.h>
int main(void) {
int inteiro = 3;
int tamanho = sizeof(int) * 8; //aqui acha o tamanho de um int
char binario[tamanho + 1]; //aqui aloca o vetor de char com espaço para o terminador
int i = tamanho - 1; //inicia o contador
while(i > 0) {
binario[i] = inteiro % 2 + 48; //acha o binário
inteiro /= 2;
i--;
}
binario[tamanho] = '%pre%'; //coloca o terminador
printf("%s", binario);
return 0;
}
'; //coloca o terminador
printf("%s", binario);
return 0;
}
See running on ideone .
Please note that it does not consider the endianness .