c - Is there a better way to find length of any String or a Number? -
here 1 way problem unnecessary usage of output screen.
lenth=printf("%d",num);// or lenth=printf("%s",str);
how find length without having output printf?
you can use snprintf()
empty buffer (i.e. pass null pointer), return number of characters have been written buffer if there enough space in it:
int length = snprintf(0, 0, "%d", num);
this way, don't need use actual buffer , won't print on stdout
printf()
does.
Comments
Post a Comment