Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 7th, 2006, 8:16 PM   #1
climbnorth
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 climbnorth is on a distinguished road
Calling an undeclared or undefined function in C/C++

Quote:
>C is part of the C++ language. C++ has often been described as a federation of languages which includes C. Anything you can do in C is ok for use with C++.
This is just incorrect. Try c
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).

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.
climbnorth is offline   Reply With Quote
Old Oct 7th, 2006, 8:28 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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:
Compiling...
undeclared.c
C:\Documents and Settings\David\My Documents\My Windows\undeclared\undeclared.c(5) : warning C4013: 'sum' undefined; assuming extern returning int

undeclared.obj - 0 error(s), 1 warning(s)
#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;
}
Note from the output that the function performs as written. You cannot do this in C++. There are a number of reasons that C++ is NOT a superset of C, though it seems to be the case for many things. My son has a goodly number of my genes. He is not a superset, though he is both larger and meaner.
Quote:
Originally Posted by Output
Sum of 2 and 2 is 4
Press any key to continue
__________________
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
DaWei is offline   Reply With Quote
Old Oct 8th, 2006, 12:02 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by climbnorth View Post
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 View Post
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;
   int i;
   int i;
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.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC