Here is one way but the problem with it is unnecessary usage of output screen.
lenth=printf("%d",num);// or lenth=printf("%s",str);
How could i find length without having output from printf?
Here is one way but the problem with it is unnecessary usage of output screen.
lenth=printf("%d",num);// or lenth=printf("%s",str);
How could i find length without having output from printf?
You can use snprintf()
with an empty buffer (i.e. pass a null pointer), which will return the number of characters that would have been written into the buffer if there was enough space in it:
int length = snprintf(0, 0, "%d", num);
This way, you don't need to use an actual buffer and it won't print on stdout
like printf()
does.