![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
Calling an undeclared or undefined function in C/C++
Quote:
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). 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. |
|
|
|
|
|
|
#2 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Note that in this program the sum function is used before it is defined. It is also not pre-declared or prototyped. The compiler issues this warning:
Quote:
#include <stdio.h>
int main (int argc, char * argv [])
{
printf ("Sum of 2 and 2 is %d\n", sum (2, 2));
}
int sum (int a, int b)
{
return a+b;
}Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#3 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
Quote:
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:
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; int i; int i; 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);14) Things cannot be implicitly int in C++, but can in C. For example; const n = 3;
void f(const parm);
main()
{
}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. |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining languages | titaniumdecoy | Other Programming Languages | 12 | Jul 13th, 2006 2:03 PM |
| Compiling Maverik 6.2 (from C) | megamind5005 | C | 16 | May 3rd, 2006 5:41 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 2:12 PM |
| Jackpot game | zorin | Visual Basic | 3 | Jun 10th, 2005 1:19 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |