3

I am trying to find some official confirmation on a theory with respect to C functions. In a simple project of mine, I have a function which I only want to be visible within the .c file in which it is defined. The function prototype is:

static int sum(int a, int b);

The function definition is:

int sum(int a, int b) {
   return (a+b);
}

Upon analysis of the build output, link maps, etc, it seems that the function is indeed static. I'm surprised that I don't get any build warnings or errors either. However, is there anything in terms of documentation (ie: specific line in the GCC manual) that can confirm this behavior, or what is expected?

I have found the equivalent of this question for C++ (Static keyword in function declaration can be missing in function definition?), but I am looking for the answer with respect to pure C.

Thank you.

Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153
  • 1
    I think C++ simply inherited this feature from C. – Barmar Mar 10 '14 at 18:14
  • I'm not following why you were expecting an error or warning. Are your prototype and function defined in different files? – lurker Mar 10 '14 at 18:16
  • 1
    @mbratch He thought the prototype and definition needed to be consistent in their uses of the `static` modifier. – Barmar Mar 10 '14 at 18:21
  • @JoachimPileborg I made them both static, and in the case noted above, and in the case where they were both defined static, no errors were encountered and the same behavior resulted. I was expecting an error if they both weren't defined as static. – Cloud Mar 10 '14 at 18:21
  • Possible duplicate of [Does a static function need the static keyword for the prototype in C?](https://stackoverflow.com/questions/15670010/does-a-static-function-need-the-static-keyword-for-the-prototype-in-c) – Richard Chambers Sep 30 '18 at 16:46

2 Answers2

2

You can download the full specification from http://www.iso.org/iso/home/store/catalogue_ics/catalogue_detail_ics.htm?csnumber=57853

for 238 Swiss Francs, and I'm sure that will have the answer. Otherwise, the best source I have is from "The C Programming Language" 2nd edition by K&R, section 4.6 on page 83 (emphasis added)

"The external static declaration is most often used for variables, but it can be applied to functions as well. Normally, function names are global, visible to any part of the entire program. If a function is declared static, however, its name is invisible outside of the file in which it is declared."

Note that the quote only refers to the declaration of the function, not its definition, although it's common practice for static functions, that the definition serves as the declaration as well.

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • 2
    [n1570.pdf](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) is a freely available draft of the 2011 ISO C standard; only a few minor tweaks were made between that draft and the released standard. If you want a copy of the official standard, at least if you're in the US, you can get it for $60.00 US from [ANSI](http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2fISO%2fIEC+9899-2012) (hmm, it was $30 when I bought it). – Keith Thompson Mar 10 '14 at 19:00
  • Little bit of inflation there, but still a bargain compared to the 270 USD :) – user3386109 Mar 10 '14 at 19:35
1

The function will be visible only in the .c by default, in order to make it visible to other files you would have to declare its prototype in the header ( .h file ).

Don't think of static here as in Java or other OO languages where only the class can call a static method, it is quite a difference.

The behaviour of static in C is that a function or variable is only known within the scope of the current compile, the data is kept in the executable file generated.

eduardtm
  • 76
  • 6
  • 1
    I do understand the difference of the use of `static` in C vs Java, for example. What confuses me is the discrepancy between the prototype and the definition. – Cloud Mar 10 '14 at 18:31
  • well...think of it as with the variable, you would use **static int var1;** but then to use it you would simply call it var1, static in the prototype will tell the compiler how to initialise the function, compile or run time – eduardtm Mar 10 '14 at 18:33
  • That partially makes sense, but the comparison isn't exactly one-to-one. At the beginning of the function definition, we have to re-type the same function name, return type, and parameter list. The only thing that seems to differ is the storage class modifier. In the example you described, the data type is only used once. – Cloud Mar 10 '14 at 18:40
  • 1
    well, you don't actually have to write the prototype in the **C** file, you can simply write the function **static int sum(int a,int b){//implementation}** without anything else in the **C** file and use it there, for your use it makes sense not to but is general is good practice to keep all the prototypes in a **.h** so you keep track of how each is declared – eduardtm Mar 10 '14 at 19:01