The C function printf()
returns the no of characters printed then what makes strlen()
different?
Asked
Active
Viewed 1,110 times
-5

deceze
- 510,633
- 85
- 743
- 889

Akash Kamble
- 5
- 4
-
3Eh, because strlen() returns the length of the string without printing it? BTW: Please learn [how to ask a good question](http://stackoverflow.com/help/how-to-ask) – B001ᛦ Aug 26 '16 at 13:27
-
4*"Cutting the chicken open with a knife also gets us the egg, why don't we do that instead of having it lay the egg?"* – Err, because one has side effects, and the other doesn't…? – deceze Aug 26 '16 at 13:47
-
Well, thank you. Fine explanation for a newbie. But I never thought that the community is so negative about beginners. I cannot ask more questions from this account now :-( – Akash Kamble Aug 27 '16 at 10:27
2 Answers
3
As bub said, part of the difference is that strlen
doesn't print the string, just returns its length. Also, though, printf() interprets its argument as a format string - what it ends up printing can be a very different length from the string it was given if it inserts values into placeholders.

Geoff Mackenzie
- 76
- 3
-
Although the question is terribly imprecise, it is only fair to point out that `strlen(s)` and `printf("%s", s)` will return the same value as long as there is no problem with `stdin`. The assumption that OP intended to use `printf` incorrectly is not based on anything written in the question. – rici Aug 26 '16 at 15:15
0
strlen()
function calculates the length of string. Thats it for strlen
but printf()
sends formatted output.

Gkan
- 385
- 1
- 5
- 20