-3

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?

wrangler
  • 3,454
  • 1
  • 19
  • 28

1 Answers1

2

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.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
P.P
  • 117,907
  • 20
  • 175
  • 238