int test = 1000;
Serial.print((sizeof(test)/sizeof(int)));
This example outputs: 1
Shouldn't it output 4?, maybe i'm just lost?
int test = 1000;
Serial.print((sizeof(test)/sizeof(int)));
This example outputs: 1
Shouldn't it output 4?, maybe i'm just lost?
I mean:
int test = 1000;
Serial.print(sizeof(test)/sizeof(int));
sizeof(test) is 2
sizeof(int) is 2
2 / 2 = 1
If you're trying to find out how many bytes an int occupies, I think you mean to say:
Serial.print(sizeof(int));
-Mike
okey,
Acctually what i need is something to get the length of the int.
int i=1000; <--- length 4.
Ah, you wish a function to return the number of significant digits of the contents of an integer variable?
If you don't mind using some floating point, you could do it like this
#include <math.h>
int numdigits(int i)
{
int digits;
i = abs(i);
if (i < 10)
digits = 1;
else
digits = (int)(log10((double)i)) + 1;
return digits;
}
If you don't want to use floating point, you will need to have a loop that counts how many times you can divide the integer by 10 until it becomes zero.
-Mike
Another way that is less elegant
int numdigits(int i)
{
char str[20];
sprintf(str,"%d",abs(i));
return(strlen(str));
}
Note that for negative numbers, the minus sign is not counted as a digit. If you want the minus sign to count, change the "abs(i)" to "i".