Quote:
Originally Posted by climbnorth
I am wondering, if a function has not been declared or defined- is it yet a function? I guess my question is: If something has no declaration, and no definition- does it exist? What has no declaration and no definition?
Also, I am wondering the validity of this claim- I know I came across something like this before and it sounds familiar but I can not think of an example for which this applies (maybe I'm missing the point).\
|
In the 1989 C standard, an attempt to call a function that has not been declared is valid: the compiler effectively assumes treats the call as an implicit declaration of the function (and assumes it returns int and accepts any number of arguments).
In practice, while such code would get past the compiler, it would typically not get past the linker unless the function was defined (i.e. implemented somewhere among the files being linked. In DaWei's example, removing the implementation of sum() from the file will typically give code that will sail past the compiler and then choke the linker.
This has always been illegal in C++ and is now illegal in C as of the 1999 standard.
Quote:
Originally Posted by climbnorth
I tried: make a function pointer- call the function pointer in both C and C++.
The function itself had not been defined or declared. Both C and C++ had the same result. Then I thought "obviously" but I can't think of a situation as described in the above quote- where behavior for one langauge is different than the other.
|
This is not a complete list, but some of the incompatibilities between C++ and C follow. Unless I state otherwise, I refer to both 1989 and 1999 C standards. There are some differences, as the 1999 C standard removed some incompatibilities, and introduced some others.
1) C++-style comments (//) are not supported in 1989 C standard but are now supported in the 1999 C standard.
2) C++-specific keywords. And C program that uses a C++ keyword as an identifier (eg a variable name) yield undefined behaviour in C++. There are a few of these (bool, catch, class, const_cast, delete, dynamic_cast, explicit, export, false, friend, mutable, namespace, new, operator, private, protected, public, reinterpret_cast, static_cast, template, this, throw, true, try, typeid, typename, using, virtual, and wchar_t). The 1999 C standard uses some of these, but as
macros. For example, bool, true, and false are macros declared in a 1999 C header file, and not keywords
3) Type of a character literal is char in C++ and int in C. In practice, this means that sizeof('X') is not necessarily equal to 1 in C but it is in C++.
4) String literals are const in C++, but not in C.
5) C++ does not support tentative declarations. For example;
is valid at file scope in C but not in C++.
6) A struct is a scope in C++, but not in C.
7) const declarations at file scope have internal linkage in C++ unless explicitly declared extern. They have external linkage in C.
8) main() can be called recursively in C but not in C++.
9) Two structs types that are compatible (i.e. apart from their name, their declaration is the same) can be used interchangeably in C but not in C++.
10) A void pointer can implicitly be converted to any other pointer type in C. It cannot in C++. Which is why, when malloc() is used in C++, its return value must be cast despite this being bad style in C.
11) Only pointers to non-const and non-volatile types can be implicitly converted to a void pointer in C++. That implicit conversion is allowed in C.
12) Implicit declarations of functions is allowed in C89 but not C++ or C99.
13) Types can be declared in expressions in C, but not in C++. For example;
p = (void *)(struct X {int i;} *)0); is legal in C but not C++.
14) Things cannot be implicitly int in C++, but can in C.
For example;
const n = 3;
void f(const parm);
main()
{
} is valid C but not valid C++. It is equivalent to this in C++;
const int n = 3;
void f(const int parm);
int main()
{
}
This is
NOT a complete list, but is enough to show that there are a few known compatibilities between C and C++ (i.e. some things that can be done in C cannot be done in C++). The above is derived from
part of Annex C of the C++ standard (which includes a more comprehensive list of incompatibilites between C++ and C89). The 1999 C standard removed some incompatibilities relative to C++, and added others.