top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Why a c function's parameter can't be declared as static ?

+1 vote
378 views
#include<stdio.h>
void fun(static int a);
int main(void)
{
   static int z=0;

   fun(z);

   return 0;
}

void fun(static int a)
{
   printf("%d",a);
}
posted Jul 29, 2014 by Ganesh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

This generates compile time error because formal arguments of a function are of register (storage class) type by default. You cannot change it.

The problem is not just with static. You can cross verify by replacing the static with auto or extern or any storage class. It will still generate the same error.

Hope this helps.

answer Jul 30, 2014 by Ankush Surelia
Similar Questions
+5 votes

What would be the drawback if we define a static variable in the header file?

+1 vote

I have an oversight in my code where I'm declaring & defining a function
with C-linkage, though it's not possible.

Example snippet:

#ifdef __cplusplus
extern "C"
{
#endif

// ... some functions which are compatible with C linkage

// Intended to be a helper function not exposed from library
std::string GetEngineVersion()
{
 // ...
}

#ifdef __cplusplus
}
#endif

Obviously the GetEngineVersion function cannot have C linkage because it returns a C++ class.

My question is: does GCC have a warning for this scenario? Specifically, can it warn when something is declared extern "C" that's incompatible with C linkage?

I compile with the following warning flags and I didn't get a warning:

-Wall -Wunused-parameter -Wextra -Weffc++ -Wctor-dtor-privacy
-Wnon-virtual-dtor -Wreorder -Wold-style-cast -Woverloaded-virtual
-Werror

Incidentally, when I compile the same code in VS2013 I get this warning:

warning C4190: 'GetEngineVersion' has C-linkage specified, but returns UDT 'std::basic_string' which is incompatible with C
...