Topic: C vs C++
Author: <L74BC@CUNYVM.BITNET>
Date: Thursday, 18 Feb 1993 17:35:30 EST Raw View
I am currently learning C (ANSI) and am trying to work out some difference
between C and C++ (I am using Borlands Turbo 3.0).
In C if I have a function that does not return an integer I must have a
STUB or PROTOTYPE in the calling function.
According to Kernigan and Ritchie, the prototype must be in the form of:
TYPE FUNCTION ();
I am not sure about C++, though. Must I have every type for every parameter
that I am sending in the parenthesis?
For example if I am sending 3 integers and one float to a function that
returns float:
float func (int, int, int, float);
Plus I must have these types in the function header. This seems very
verbose for C. And a little to much like Modula-2. Am I misunderstanding
something?
I would appreciate if someone can e-mail me some help.
Thanx
Adam
Author: nason@bigwpi.WPI.EDU (Benjamin N. Lipchak)
Date: 21 Feb 1993 01:13:15 GMT Raw View
I am having a problem with a program I'm writing under Borland C++ v2.
My source is a .cpp file (I'm using Object Oriented libraries for a GUI)
and so I'm using the C++ compiler. My problem is that I have a library I
wish to link in order to use voice synthesis. When I try to link, it tells me
that I have "Undefined symbols" - the ones that were supposed to be in the
library. When I call those same functions from a .c file, using the plain C
compiler, it links fine. But then I wouldn't be able to use the OOPs. I know I've read somewhere how to call old C functions from C++... could someone
please help me!!
Thanx,
Benjamin Nason Lipchak
nason@wpi.wpi.edu
Author: nason@bigwpi.WPI.EDU (Benjamin N. Lipchak)
Date: 21 Feb 1993 01:18:53 GMT Raw View
I am having a problem with a program I'm writing under Borland C++ v2.
My source is a .cpp file (I'm using Object Oriented libraries for a GUI)
and so I'm using the C++ compiler. My problem is that I have a library I
wish to link in order to use voice synthesis. When I try to link, it tells me
that I have "Undefined symbols" - the ones that were supposed to be in the
library. When I call those same functions from a .c file, using the plain C
compiler, it links fine. But then I wouldn't be able to use the OOPs. I know I've read somewhere how to call old C functions from C++... could someone
please help me!!
Thanx,
Benjamin Nason Lipchak
nason@wpi.wpi.edu
Author: Chewy@cup.portal.com (Paul Frederick Snively)
Date: Sun, 21 Feb 93 16:50:07 PST Raw View
Benjamin,
Thanks to C++'s type-safe linkage, function names in C++ are `mangled,' which
literally means that they carry around encoded information about the types
of their arguments.
To refer to a straight C function in a library, you need to tell C++ that
that's what you're doing. The proper incantation to do that is:
extern "C" foo(short bar, long baz, float bletch);
or whatever the real declaration is. This will prevent the compiler from
generating the mangled form of the declaration, thereby causing your linker
to choke.
Good luck,
Paul
Author: h8708144@hkuxb.hku.hk (Lee Kwok Wai Joseph)
Date: Tue, 23 Feb 1993 09:16:02 GMT Raw View
(L74BC@CUNYVM.BITNET) wrote:
: I am currently learning C (ANSI) and am trying to work out some difference
: between C and C++ (I am using Borlands Turbo 3.0).
: In C if I have a function that does not return an integer I must have a
: STUB or PROTOTYPE in the calling function.
: According to Kernigan and Ritchie, the prototype must be in the form of:
: TYPE FUNCTION ();
: I am not sure about C++, though. Must I have every type for every parameter
: that I am sending in the parenthesis?
: For example if I am sending 3 integers and one float to a function that
: returns float:
: float func (int, int, int, float);
: Plus I must have these types in the function header. This seems very
: verbose for C. And a little to much like Modula-2. Am I misunderstanding
: something?
:
There are at least 3 differences between C++ function prototype and
ANSI C function prototype:
1. If the return type of your function is int, then you can ommit
the function prototype in ANSI C, but you are required to write
the function prototype in C++.
2. The function prototype
int f();
is interpreted by ANSI C as int f(...) while it is interpreted by
C++ as int f(void)
3. C++ allows "overloading" of function names, i.e. functions sharing
the same name but each of them distinguished by the argument list.
e.g. For the "func" function in your post, you can have 2 more
functions sharing the same name :
float func (int, int, int, float); // your one
double func (double, int);
int func (int, int, int);
So the argument list is neccessary in the function prototype.
Joseph
==============================
Internet : jkwlee@hkuxa.hku.hk
------------------------------