473,889 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why still use C?

no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.

i heard people say C++ is slower than C but i can't believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is basically the same.

in C there arent the simplest things present like constants, each struct and
enum have to be prefixed with "struct" and "enum". iam sure there is much
more.

i don't get it why people program in C and faking OOP features(functi on
pointers in structs..) instead of using C++. are they simply masochists or
is there a logical reason?

i feel C has to benefit against C++.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 13 '05
687 23922
E. Robert Tisdale wrote:
Richard Heathfield wrote:
I am yet to be persuaded that
you need anything other than extern "C" { } for C++ compatibility.
What would persuade you?


An expert, arguing rationally and convincingly. In other words, not you, as
I go on to show.
Nothing guarantees that you will be able to link object files
created by another compiler into your C++ program
even if that compiler is another C++ compiler.
That is of course true, but it is also irrelevant.
According to Bjarne Stroustrup,
"The C++ Programming Language: Third Edition",
Chapter 9: Source Files and Programs", Section 2: Linkage,
Subsection 4: Linkage to Non-C++ Code, page 205, "To help,
one can specify a /linkage/ convention
to be used in an /extern/ declaration.

The only way to ensure that you can link C code into your C++ program
is to compile it with the same C++ compiler.
Wrong. There is /no/ way to ensure that you can link (arbitrary) C code into
your C++ program. The way you outline is flawed, because no C++ compiler
will compile all C code.
The easiest and most reliable way to link C++ code into C programs
is to compile both of then with your C++ compiler.


I am not interested in linking C++ code into C programs. We are discussing
linking C code into C++ programs. Arbitrary C code is not guaranteed to
compile under a C++ compiler, and brief experimentation shows that most of
my C code doesn't compile under a C++ compiler.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #381
Richard Heathfield <do******@addre ss.co.uk.invali d> writes:

P.J. Plauger wrote: [deleted]
.... may summarise, you've basically pointed that you cast malloc because it
leads to more portable code and can help you find bugs. These are the

....

Maybe this came up earlier in the thread and I missed it, but I don't
see why, if the main reason for casting malloc calls is C++ compatibility,
it doesn't make more sense to use a macro something like one of the ones
in this example:
#include <stdlib.h>

#ifdef __cplusplus
#define CXXCAST(x) static_cast<x>
#define CXXCAST2(x,y) static_cast<x>( y)
#else
#define CXXCAST(x)
#define CXXCAST2(x,y) y
#endif

int main(void)
{
char *c;

c = CXXCAST(char *) (malloc(10 * sizeof *c));
c = CXXCAST2(char *, malloc(10 * sizeof *c));

/* watch the leak */

return 0;
}
A C-style cast could be used if the static_cast isn't portable enough.
It seems like you get the best of both worlds here, unless you're
coding to a DS9000 or something where __cplusplus causes the C compiler
to explode. Am I overlooking something?
Derek
Nov 13 '05 #382
Derek Millar wrote:

<snip>
A C-style cast could be used if the static_cast isn't portable enough.
It seems like you get the best of both worlds here, unless you're
coding to a DS9000 or something where __cplusplus causes the C compiler
to explode. Am I overlooking something?


From my perspective at least, yes, you're overlooking the fact that good C++
code tends to look nothing like good C code; nowadays, I never (okay, never
say never - let's just say "rarely") use malloc in C++ code. There's a
perfectly good new operator for creating objects dynamically, and I think
it's better to use it when writing C++.

Obviously, Mr Plauger's situation is rather different.
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #383
thp
In comp.std.c James Kuyper <ku****@wizard. net> wrote:
[...]
+ I just tried the same test with the first 81 C modules I could find,
+ all of which are part of the same C89 program, and most of which have
+ been modified by me personally at least once, and reviewed by me
+ multiple times. This set was significantly cleaner with respect to C++
+ compilation than yours:
+
+ Yes: 79
+ No: 2

Wow! But what about malloc's void* return type? Did you commit *bad*
practice of casting the return value to a non-void pointer type?

Tom Payne
Nov 13 '05 #384
th*@cs.ucr.edu writes:
James Kuyper <ku****@wizard. net> wrote:
+ This set was significantly cleaner with respect to C++
+ compilation than yours:
+ Yes: 79
+ No: 2

Wow! But what about malloc's void* return type? Did you commit *bad*
practice of casting the return value to a non-void pointer type?


I tend to use macros such as the following, defined in a per-project
header file:

#include <stdlib.h>

#define NEW(type) \
((type *) malloc(sizeof(t ype)))

#define NEW_ARRAY(type, num) \
((type *) malloc((num) * sizeof(type)))

#define RESIZE_ARRAY(pt r, type, num) \
((type *) realloc((ptr), (num) * sizeof(type)))

I consider the use of such macros to be good practice, not bad practice.
They are safer than using malloc() manually, since they avoid the possibility
that you accidentally specify a type inside the sizeof() which doesn't match
the type of the pointer which you are assigning the result to.
Using a single set of macros throughout the application also provides a
good place to hook in if any special application-specific handling of
out-of-memory conditions is desired.

--
Fergus Henderson <fj*@cs.mu.oz.a u> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
Nov 13 '05 #385
Fergus Henderson <fj*@cs.mu.oz.a u> writes:
th*@cs.ucr.edu writes:
James Kuyper <ku****@wizard. net> wrote:
+ This set was significantly cleaner with respect to C++
+ compilation than yours:
+ Yes: 79
+ No: 2

Wow! But what about malloc's void* return type? Did you commit *bad*
practice of casting the return value to a non-void pointer type?


I tend to use macros such as the following, defined in a per-project
header file:

#include <stdlib.h>

#define NEW(type) \
((type *) malloc(sizeof(t ype)))

#define NEW_ARRAY(type, num) \
((type *) malloc((num) * sizeof(type)))

#define RESIZE_ARRAY(pt r, type, num) \
((type *) realloc((ptr), (num) * sizeof(type)))


The macros look fine, but the casts are still unnecessary and
potentially dangerous. Why not just

#define NEW(type) \
(malloc(sizeof( type)))

?

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #386
th*@cs.ucr.edu wrote:

In comp.std.c James Kuyper <ku****@wizard. net> wrote:
[...]
+ I just tried the same test with the first 81 C modules I could find,
+ all of which are part of the same C89 program, and most of which have
+ been modified by me personally at least once, and reviewed by me
+ multiple times. This set was significantly cleaner with respect to C++
+ compilation than yours:
+
+ Yes: 79
+ No: 2

Wow! But what about malloc's void* return type? Did you commit *bad*
practice of casting the return value to a non-void pointer type?


Nope. But that's because we work according to a coding standard which
prohibits taking advantage of implicit conversions; all conversions must
be the result of explicit casts. That's a bad idea, in my opinion, but
we're stuck with it. I hope it won't take me too long to unlearn that
habit as soon as I'm able to work somewhere with more reasonable coding
standards. Also, a large fraction of this code was originally designed
by people with more experience with Fortran 77 than with C. As a result,
it contains very little dynamic memory allocation.

I simply checked the largest code base that I had easy access to; I
wouldn't claim that it is in any sense representative.
Nov 13 '05 #387
Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Derek Millar wrote:

<snip>
A C-style cast could be used if the static_cast isn't portable enough.
It seems like you get the best of both worlds here, unless you're
coding to a DS9000 or something where __cplusplus causes the C compiler
to explode. Am I overlooking something?


From my perspective at least, yes, you're overlooking the fact that good C++
code tends to look nothing like good C code; nowadays, I never (okay, never

.....

Well, sure, but as I understand it, the requirement in this particular
situation is to have a single piece of code which compiles under both
C and C++, in which case it seems to make sense to take as much advantage
as possible of the safety features of each language (i.e., no cast for
malloc in C and static_cast in C++). Whether there's a general need for
that sort of code or not is a separate question.
Derek
Nov 13 '05 #388
Keith Thompson wrote:
....
The macros look fine, but the casts are still unnecessary and
potentially dangerous. Why not just

#define NEW(type) \
(malloc(sizeof( type)))


Because those dangerous casts are the key motivation for those macros.
This NEW() macro is intended to do something vaguely similar to the
'new' operator in C++: it allocates memory of the correct size and
returns a pointer to the correct type. The advantage is that the type
used in the cast is always correctly matched with the type used in the
sizeof(). The disadvantage is that there's no mandatory warning if
there's no declaration of malloc() in scope, or a declaration with the
wrong return type.
Nov 13 '05 #389
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bn******* ***@sparta.btin ternet.com>...
James Kuyper wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message
news:<bn******* ***@hercules.bt internet.com>.. .

Do you have an example of a cast that is not required for correctness but
which can nevertheless add to the portability of a program? ....
Oh, I see. I'm rather disappointed, since I would not regard
clock()/CLOCKS_PER_SEC as "correct code", and it comes as rather a surprise
to hear that C experts think it is. I mean, /yes/, it may be "correct code
for that implementation" , but then so may void main().
Well, you asked about a case where something is "not required for
correctness, but is required for portability". If it's requried for
portability, it must be required for correctness on at least one
platform that you might be porting to. Therefore, the only sense in
which the code could be called "correct" is "correct for that
implementation" .

.... I am yet to be persuaded that you need anything other than extern "C" { }
for C++ compatibility.


Examine Annex C of the C++ standard. It's quite possible to write
valid C code that is also valid C++ code, with different meanings.
Using extern "C" is not sufficient to cover those cases.


Perhaps I wasn't quite clear. Firstly, I am well aware that it is possible
to write code that means different things in the two languages. Secondly, I
am referring not to deliberately-constructed "exploits" but to "real world"
code. Thirdly and perhaps most importantly, I am referring specifically to
the construct:

extern "C"
{
#include "c_header.h "
};

where the header contains the usual (or what /ought/ to be usual) mixture of
comments, preprocessor directives, typedefs, and function prototypes.


Also, declarations of global variables. Such variables are usually a
result of bad design, but not always.

If you're just talking about those cases, the opportunities for
something to go wrong are much reduced, certainly, but they're not
completely gone.

A header shouldn't contain function definitions, which means that many
of the possible problems can't occur there directly. However, a header
can contain macros that might expand into code containing those
problems. Even ignoring macros, a C header file might contain any of
the following:

Declarations of identifiers which match C++ keywords that aren't C
keywords.

Tentative definitions.

The name of a struct member outside of what C++ calls "class scope".

The declaration of a const object with external linkage at file scope
without 'extern' qualification, or without an initializer.

Declarations of or using enumeration types.

An object with enumeration type, whose initializer doesn't have that
same type.

A typedef with the same name as a structure tag.

A non-prototyped function declaration.

A type defined inside the declaration of a struct or function using
that type.

A string initializer for a character array that is not big enough to
contain the terminating null character.

A use of the __STDC__ macro.

Every single one of those features can result in code with a different
meaning in C++ than in C.
....
Let me translate his English; it's pretty straight forward.

here.h:
#include "somewhere_else .h"
typedef struct
{
// "... a structure declared here containing a pointer to ..."
their_struct *ptheirs;
// other contents.
} other_struct;


Already this doesn't compile. :-) (C99 comments.) But okay, you have a


Of course; we're talking about C, by which I mean the current version,
C99, where those comments are allowed.

.... But if you don't cast in the first place, there /can't/ be a mismatch and
therefore there is no problem. Am I missing something here?


No, I'm just explaining the purpose of his code. I don't agree with
it, except for the fact that he does have a customer requirement of
compilability both with C and with C++.
Nov 13 '05 #390

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

Similar topics

3
11267
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL) on the server because of that. Our site will have an SSL certificate next week, so I would like to use AIM instead of SIM, however, I don't know how to send data via POST over https and recieve data from the Authorize.net server over an https...
2
5865
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues to execute the code until the browser send his reply to the header instruction. So an exit(); after each redirection won't hurt at all
3
23053
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which field is completed.
0
8508
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. 354 roberto@ausone:Build/php-4.3.2> ldd /opt/php4/bin/php libsablot.so.0 => /usr/local/lib/libsablot.so.0 libstdc++.so.5 => /usr/local/lib/libstdc++.so.5 libm.so.1 => /usr/lib/libm.so.1
1
8621
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the column below. The viewer can select states from the drop down lists above the other two columns as well. If the viewer selects only one, only one column fills. If the viewer selects two states, two columns fill. Etc. I could, if appropriate, have...
4
18317
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the user comes back to a page where he had a submitted POST data the browser keeps telling that the data has expired and asks if repost. How to avoid that? I tried registering all POST and GET vars as SESSION vars but
1
6890
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url http://www.mis.gla.ac.uk/biquery/training/ but each of the courses held have maximum of 8 people that could be
2
31469
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value to :parameter I dont like the idea of making the SQL statement on the fly without binding parameters as I dont want a highly polluted SQL cache.
3
23617
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the results of the picture half the size. The PHP I have installed support 1.62 or higher. And all I would like to do is take and image and make it fit a 3x3. Any suggestions to where I should read or look would be appreciated.
1
10889
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
10441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9606
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
7993
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
7150
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
5828
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
4647
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
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3255
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.