Take the 2-minute tour ×

I might be going insane, but I don't think I've ever seen this in c++ (though my reference code is in C). Why is there a static on the return value of the code here and what impact does it have? I don't think I've ever seen a static function outside class scope (but obviously C doesn't have classes and this probably has a different syntactical meaning).

/* just like strlen(3), but cap the number of bytes we count */
static size_t strnlen(const char *s, size_t max) {
    register const char *p;
    for(p = s; *p && max--; ++p);
    return(p - s);

From http://www.zeuscat.com/andrew/software/c/strnlen.c.

share|improve this question
It should be noted that the names like strnlen for your own functions are a very bad idea. str* is reserved by ISO C and POSIX if string.h is included, and in fact POSIX defines its own non-static strnlen which will conflict with this static one. –  R.. Jan 17 '12 at 5:50
I'm not writing my own really, I just like to review things like this occasionally since interviewers frequently use this kind of question :) but thank you for the advice. –  w00te Jan 17 '12 at 14:44
add comment

1 Answer

up vote 6 down vote accepted

the static is not on the return type but on the function definition.

static functions don't have external linkage basically they are only visible to other functions in the same file.

share|improve this answer
Does this work in C++ too for namespace-scope functions (non class)? –  w00te Jan 16 '12 at 18:06
Yes it works for non class functions in C++ –  parapura rajkumar Jan 16 '12 at 18:11
Thank you very much! –  w00te Jan 16 '12 at 18:36
add comment

Your Answer


