473,624 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can a static function declaration conflict with a non-static declaration?

Let's say I have two files, myfile.h and myfile.c:

myfile.h:

int myfunction(int x);

myfile.c:

#include "myfile.h"

static int myfunction(char *x)
{
return 0;
}

Does this conform to the C standard? With GCC 4, this generates an
error, because the definitions of myfunction() conflict. I have a
colleague who says this compiles in GCC 3, because the static version
of myfunction() overrides the non-static prototype from the header
file. I believe that GCC 3 is just ignoring the problem, and that it's
technically a bug, but I can't find any documentation that specifically
says that static and non-static declarations of the same function have
to have the same parameters and return type.

Dec 12 '06 #1
4 6102
In article <11************ **********@n67g 2000cwd.googleg roups.com>
no**********@ta bi.org <ti***@tabi.org wrote:
>Let's say I have two files, myfile.h and myfile.c:

myfile.h:

int myfunction(int x);

myfile.c:

#include "myfile.h"

static int myfunction(char *x)
{
return 0;
}

Does this conform to the C standard?
6.1.2.2 Linkages of identifiers
[snippage]
[#7] If, within a translation unit, the same identifier
appears with both internal and external linkage, the
behavior is undefined.

The first declaration, in myfile.h (first because it is #include-d
before the second declaration) does not use the "extern" keyword and
has no previous declaration in scope, so it specifies external linkage
for the identifier "myfunction ".

The second declaration, using the "static" keyword, specifies internal
linkage for the identifier "myfunction ".

The behavior is undefined.
>With GCC 4, this generates an error, because the definitions of
myfunction() conflict.
Allowed, since the behavior is undefined.
>I have a colleague who says this compiles in GCC 3, because the
static version of myfunction() overrides the non-static prototype
from the header file.
Also allowed, since the behavior is undefined.

GCC 4's complaint is probably a good idea, since relying on undefined
behavior when "well-defined behavior" is at least as good is unwise.
(Using undefined behavior for positive benefit, in places where
"well-defined behavior" is no good, is another matter entirely.
That is, I would not call it "unwise" to "#include <graphics.h>"
in order to draw graphics on a screen or window. The cost --
behavior undefined by a widespread standard -- is outweighed by
the benefit of being able to do what the code is required to do.
But here there is no benefit from mixing internal and external
linkage, only a cost.)
>I believe that GCC 3 is just ignoring the problem, and that it's
technically a bug, but I can't find any documentation that specifically
says that static and non-static declarations of the same function have
to have the same parameters and return type.
Since the behavior is undefined, anything can happen.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 12 '06 #2
Chris Torek <no****@torek.n etwrites:
In article <11************ **********@n67g 2000cwd.googleg roups.com>
no**********@ta bi.org <ti***@tabi.org wrote:
>>Let's say I have two files, myfile.h and myfile.c:

myfile.h:

int myfunction(int x);

myfile.c:

#include "myfile.h"

static int myfunction(char *x)
{
return 0;
}

Does this conform to the C standard?

6.1.2.2 Linkages of identifiers
[snippage]
[#7] If, within a translation unit, the same identifier
appears with both internal and external linkage, the
behavior is undefined.

The first declaration, in myfile.h (first because it is #include-d
before the second declaration) does not use the "extern" keyword and
has no previous declaration in scope, so it specifies external linkage
for the identifier "myfunction ".

The second declaration, using the "static" keyword, specifies internal
linkage for the identifier "myfunction ".

The behavior is undefined.
I agree.

However, it may be worth adding that the opposite situation,
shown below, is OK:

static int myfunction(void );
int myfunction(void ) {
...
}

This is because there is an explicit exception for this situation
in the Standard:

For an identifier declared with the storage-class specifier
extern in a scope in which a prior declaration of that
identifier is visible,23) if the prior declaration specifies
internal or external linkage, the linkage of the identifier
at the later declaration is the same as the linkage
specified at the prior declaration. If no prior declaration
is visible, or if the prior declaration specifies no
linkage, then the identifier has external linkage.

--
Just another C hacker.
Dec 12 '06 #3
In article <87************ @blp.benpfaff.o rg>
Ben Pfaff <bl*@cs.stanfor d.eduwrote (in part):
>... it may be worth adding that the opposite situation,
shown below, is OK:

static int myfunction(void );
int myfunction(void ) {
...
}

This is because there is an explicit exception for this situation
in the Standard:

For an identifier declared with the storage-class specifier
extern in a scope in which a prior declaration of that
identifier is visible,23) if the prior declaration specifies
internal or external linkage, the linkage of the identifier
at the later declaration is the same as the linkage
specified at the prior declaration. If no prior declaration
is visible, or if the prior declaration specifies no
linkage, then the identifier has external linkage.
The exception quoted here is specific to declarations using the
"extern" keyword. So, this means:

static int myfunction(void );
extern int myfunction(void ) {
...
}

is OK. So is:

static double d;
extern double d;

In these two cases, the "extern" keyword functions the same as the
"static" keyword, giving the identifier internal linkage.

Omitting the "extern" keyword entirely is even more bizarre:

[#5] If the declaration of an identifier for a function has
no storage-class specifier, its linkage is determined
exactly as if it were declared with the storage-class
specifier extern. If the declaration of an identifier for
an object has file scope and no storage-class specifier, its
linkage is external.

Note the different specifications for functions vs objects. This
means Ben Pfaff's example (a function) is OK: with no storage class
specifier keyword, the function inherits the linkage of the visible
prior declaration (i.e., the "static" one right above it) -- but
my example with "d" above becomes invalid if the "extern" keyword
is removed:

static double d;
double d; /* ERROR */

I have no idea why this particular (apparently entirely gratuitous)
inconsistency exists in the C standards.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 12 '06 #4
In article <el*********@ne ws2.newsguy.com >,
Chris Torek <no****@torek.n etwrote:
>Omitting the "extern" keyword entirely is even more bizarre:

[#5] If the declaration of an identifier for a function has
no storage-class specifier, its linkage is determined
exactly as if it were declared with the storage-class
specifier extern. If the declaration of an identifier for
an object has file scope and no storage-class specifier, its
linkage is external.

Note the different specifications for functions vs objects. This
means Ben Pfaff's example (a function) is OK: with no storage class
specifier keyword, the function inherits the linkage of the visible
prior declaration (i.e., the "static" one right above it) -- but
my example with "d" above becomes invalid if the "extern" keyword
is removed:

static double d;
double d; /* ERROR */

I have no idea why this particular (apparently entirely gratuitous)
inconsistenc y exists in the C standards.
Presumably it's related to the fact that

double d;

is all you need to do to cause a double to exist, but

int myfunction(void );

still leaves you in need of the definition. There's no default
initializer for functions! The only use of a function declaration
without a body is to declare the function's interface, so why require
the user to add a redundant "extern"?

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2074
by: Marc Walgren | last post by:
Greetings I have an ASP application to enter reservations. There are multiple user security settings that require some users to have a restricted list of client in a drop list on a form. I constructed the following function: CREATE FUNCTION (@pUserId int)
2
1718
by: Mark Preston | last post by:
Its perhaps a bit early to ask, since I'm still doing the page design and haven't got down to the coding yet, but I wonder if anyone can help with a bit of a question. To lay the groundwork, a quick description of the template design for the pages:- 1. There will be a body section of the page consisting of a set of <div> layers. The top level layers will all have the same CSS class to ensure a common design look. 2. At the side or top...
13
4336
by: Roy Hills | last post by:
I've seen two different function prototype formats used for ANSI C, and I'm unsure as to which is the correct or preferred one. 1st Format (this is what I use) type function(type, type, type); e.g. "int multiply(int, int);" 2nd Format (I've seen this used in other people's code)
7
6870
by: Jon Davis | last post by:
http://support.microsoft.com/default.aspx?scid=kb;en-us;329014 Adding the code at this URL results in an error message: An unhandled exception of type 'System.Resource.MissingManifestResourceException' occurred in mscorlib.dll Additional information: Could not find any resources appropriate for the specified culture 9or the neutral culture) in the given assembly. Make sure "WebBrowser.resources" was correctly embedded or linked into...
56
3957
by: Luke Matuszewski | last post by:
I am designing the library, which will hidden all its functions within singleton object ... So for clients they will use it like . eg. system.getElementWithId('ruler'); At library side, i will use constructs like follows (at global scope) eg.
20
3792
by: ramubdvt | last post by:
hi, i have written this strcat but sometime it is giving problem, while handeling some strings containing binary and if string containing zero , funtion which takes string 1 and its length string2 and its length as arguments please tell the correction , here iam using
7
3084
by: | last post by:
All, I have a MFC Mixed mode dll which is working well. I am now tring to use a regular C++ class from another DLL which has a method called GetMessage. When I link I get 2 error messages: MyClass.obj : error LNK2028: unresolved token (0A00074C) "public: class CMessage * __thiscall CTransmitMessage::GetMessage(int)" (?GetMessage@CTransmitMessage@DSS@@$$FQAEPAVCMessage@2@H@Z) referenced in
7
2220
by: lovecreatesbea... | last post by:
Shoud we declare non-pointer function parameters with const keywords? int main(void){ int f(const int i); int i; f(i); return 0; }
23
2292
by: nsa.usa | last post by:
Hi, I used to use a function in other languages (TP or asm don't remember) where I could get number of clockticks since 1980. Is there a similar function in C? I don't seem to find it. I need to seed the random generator and since it's a web-server app, I can't seed with the date/time because the resolution is not great enough. If the app runs twice in the same second (or millisecond or whatever the resolution is) then the random...
10
3348
by: themadjester | last post by:
This is weird, I know what an IP address conflict is, and how to avoid it, but this problem seems atypical - and apparently client side? Basically I have a router internet network, it is DHCP and the range 100-199 Now, to my knowledge myself and everyone else on the network is using DHCP (non techies). some things I have tried are: - I changed the address range from 100-199 to 150-199 to force my laptop to obtain a new IP (and not...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.