Your calculation of number1
and number2
will result in a number which can have anything from 1 to 4 digits. 32-bit integers can normally have anything from 1 to 10 digits, but since your formula applies a modulus of 1023, the number will be capped at 1022, or rather 1024, since you add 2 afterward. Thus it can range from 2 to 1024. Hence, your char n1[1]
and char n2[1]
buffers are not big enough to handle your calls to sprintf()
. They must be at least 5 chars long for that, which is necessary for the (up to) 4 digits, plus the terminal NUL added by sprintf()
.
Furthermore, your attempt to assign the entire n1
and n2
buffers to str[0]
and str[1]
is incorrect; you cannot assign a char array to a char. I'm also not entirely sure why you want to transfer the values from n1
and n2
to another string buffer.
What you seem to want to do is convert back to int
after converting to string. You can do this as follows:
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
struct timeval TempoInicial;
int main() {
int number1, number2;
gettimeofday(&TempoInicial,NULL);
number1 = ((TempoInicial.tv_usec / 47) % 1023) + 2;
number2 = ((TempoInicial.tv_usec / 47) % 1023) + 2;
char n1[5],n2[5];
sprintf(n1, "%d", number1 );
sprintf(n2, "%d", number2 );
int i1 = atoi(n1);
int i2 = atoi(n2);
printf("%d %d\n", i1, i2 );
}
You seem to have some confusion about what data can be stored in which data types. A char cannot handle a number that can range over 0..1024. A char is an 8-bit value, thus it can only hold 2^8 == 256 possible values. Thus it is incorrect to assign these numbers to str[0]
or str[1]
.
If you want a string representation of the numbers, you already have it in n1
and n2
, courtesy of sprintf()
. If you want an integral representation of the numbers, you already had it in number1
and number2
, and you can get it again from the string representations courtesy of atoi()
, as I show in my code. If you want to print out the string values, you can say something like printf("%s %s\n", n1, n2 );
. If you want to print the integral values, you can say something like printf("%d %d\n", i1, i2 );
, as I show in my code. Those are all the actions that I believe are relevant to what you're trying to do. Let me know if you have any more questions about anything.
Ah, I think I see what you're after. You can combine n1
and n2
into the same string with sprintf()
. You just have to be careful to use a sufficiently large buffer, which will be 4 digits + 1 space + 4 digits + 1 NUL == 10 as the upper limit or "worst case" scenario, if you will. (The "best case" scenario is 1 digit + 1 space + 1 digit + 1 NUL == 4.) Thus we have:
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
struct timeval TempoInicial;
int main() {
int number1, number2;
gettimeofday(&TempoInicial,NULL);
number1 = ((TempoInicial.tv_usec / 47) % 1023) + 2;
number2 = ((TempoInicial.tv_usec / 47) % 1023) + 2;
char n1[5],n2[5];
sprintf(n1, "%d", number1 );
sprintf(n2, "%d", number2 );
char str[10];
sprintf(str, "%s %s", n1, n2 );
printf("%s\n", str );
}