473,889 Members | 1,387 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 23917
Fergus Henderson wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Fergus Henderson wrote:
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))) ... 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.
But it is only by putting a type in the sizeof that you risk the accident!
With the following technique, there is no such risk.

T *p = malloc(N * sizeof *p);


With that technique, you run a different risk.
You may accidentally type

T *p = malloc(N / sizeof *p);


ROTFL!!! Yes indeed! And I might type memcmp when I mean memcpy, or unsigned
when I mean unsigned long, or float when I mean double.
or

T *p = malloc(N * sizeof p);
This is perhaps a more serious objection, but it can be overcome via a
macro, without reference to type information - if you are sufficiently
concerned about it (I am not):

#define FOO(p, N) do { p = malloc(N * sizeof *p) } while(0)

(choose your own better name for this macro)
or

T *p = malloc(N * sizeof **p);
Likewise.
or

T *p = malloc(N * sizeof *p);
T *q = malloc(N * sizeof *p); /* cut-and-paste error */


Likewise.

--
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 #431
James Kuyper <ku****@saicmod is.com> writes:
"E. Robert Tisdale" wrote:
> cat malloc89.c

int main(int arc, char* argv[]) {
char* p = malloc(10);
return 0;
}
> gcc -Wall -std=c89 -pedantic -O2 -o malloc89 malloc89.c

malloc89.c: In function `main':
malloc89.c:2: warning: implicit declaration of function `malloc'
malloc89.c:2: warning: initialization makes pointer from integer
without a cast
malloc89.c:2: warning: unused variable `p'

The "initializa tion makes pointer from integer without a cast" warning
is cryptic and misleading.


Unless you're suggesting that the compiler special-case the standard
library functions, I don't see any alternative way to handle that code.
It's warned you about the implicit declaration, then it attempts to use
it. In the process, an int is implicitly converted to a pointer type,
and that's pretty much the only other feature of the code on that line
that it could use as a basis for giving a warning message.


The compiler should treat implicit int differently than an explicitly
declared int, and should special-case error/warning messages for conversions
from implicit int.

--
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 #432
Fergus Henderson <fj*@cs.mu.oz.a u> wrote in message news:<3f******* *@news.unimelb. edu.au>...
James Kuyper <ku****@saicmod is.com> writes:
"E. Robert Tisdale" wrote:
> cat malloc89.c
int main(int arc, char* argv[]) {
char* p = malloc(10);
return 0;
}

> gcc -Wall -std=c89 -pedantic -O2 -o malloc89 malloc89.c
malloc89.c: In function `main':
malloc89.c:2: warning: implicit declaration of function `malloc'
malloc89.c:2: warning: initialization makes pointer from integer
without a cast
malloc89.c:2: warning: unused variable `p'

The "initializa tion makes pointer from integer without a cast" warning
is cryptic and misleading.


Unless you're suggesting that the compiler special-case the standard
library functions, I don't see any alternative way to handle that code.
It's warned you about the implicit declaration, then it attempts to use
it. In the process, an int is implicitly converted to a pointer type,
and that's pretty much the only other feature of the code on that line
that it could use as a basis for giving a warning message.


The compiler should treat implicit int differently than an explicitly
declared int, and should special-case error/warning messages for conversions
from implicit int.


It did: the warnings started with "implicit declaration of function
`malloc'". That treats implicit int differently from an explicitly
declared int, and constitutes all of the special-casing that's needed
to clarify the warning about
the conversion from int.
Nov 13 '05 #433
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bn******* ***@sparta.btin ternet.com>...
James Kuyper wrote: .... 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().


"Correct code for that implementation" is "correct code", for that
implementation. The fact that it's not portable doesn't make it
incorrect.
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.


I didn't realize that you were restricting the scope of your comment
that way. I agree with your list of reasonable contents for a header
file, except that I would also include declarations of global
variables. Global variables are often a sign of bad design, but not
always, and as a practical matter it can take a long time to remove
them from legacy code.

Since you're not talking about files containing function bodies, that
reduces the likelihood of running into problems. Of course, any
problem that can be created in code in a function body, can also be
created by use of a macro defined in a header file, and expanding into
such code when actually used.

However, even ignoring the possible problems with macros, there's
other C/C++ problems that can occur even in a properly used header
file. Any header which contains any of the following features can be a
problem:

1. Declarations of identifiers matching C++ keywords that are not C
keywords.
2. Tentative definitions.
3. Use of a struct member name outside of the region that, according
to C++ rules, has struct scope. This is most common when the member
name is itself a struct tag, enumeration type, or enumerator name.
4. A declaration of a const variable with file scope, that was
intended to have external linkage, but which doesn't explicitly say
'extern'.
5. Non-prototyped function declarations.
6. Storage class specifiers as part of the definition of a pure type;
that is, a typedef, or a struct, union, or enumeration declaration
that is not also the declaring any particular object of that type.
7. A typedef with the same name as a struct tag.
8. A type defined as part of the declaration of a function that uses
that type.
9. A struct definition containing both a declaration of an inner
struct, and a use of a file scope variable with the same name as the
struct.
10. A struct defined inside another struct, but used outside of the
outer struct.
11. Use of a typedef inside a struct, followed by declaration of
another struct member whose name matches the typedef name.
12. Use of __STDC__.
13. #undef of any of the macros defined in <iso646.h>.
14. Any use of an enumeration. Different enumeration types can have
different sizes and representations in C++, which would make virtually
any use of them in a header file context dangerous.

I'll grant you, use of these features isn't very common, and many of
these problems are guaranteed to produce diagnostics. However, they
are things you need to worry about, beyond simply inserting extern
"C".

....
#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
struct type with a pointer in it. With you so far...


Last time I checked, C99 is the current standard for C. If you're
talking about some other version on comp.std.c, you need to explicitly
say so.
Nov 13 '05 #434
On Thu, 23 Oct 2003 09:22:34 GMT in comp.std.c, Fergus Henderson
<fj*@cs.mu.oz.a u> wrote:
th*@cs.ucr.e du 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)))


Where such macros are used, prefer the safer:

#define NEW(ptr) \
((ptr) = malloc(sizeof(* (ptr))))

as that is how I've used malloc() safely since C89: avoiding
having to keep the pointer declaration type in sync with the
allocation type by implicitly using the type of object pointed
to.
#define NEW_ARRAY(type, num) \
((type *) malloc((num) * sizeof(type)))

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


With the same type safety, could also then define:

#define NEW_ARRAY(base, n) \
((base) = malloc(sizeof(* (base) * (n))))

#define RESIZE_ARRAY(pt r,base,n) \
((ptr) = realloc((base), sizeof(*(base) * (n))))

Thanks. Take care, Brian Inglis Calgary, Alberta, Canada
--
Br**********@CS i.com (Brian dot Inglis at SystematicSw dot ab dot ca)
fake address use address above to reply
Nov 13 '05 #435
James Kuyper wrote:

<snip>
"Correct code for that implementation" is "correct code", for that
implementation. The fact that it's not portable doesn't make it
incorrect.
MMMV. :-)

<snip>
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.


I didn't realize that you were restricting the scope of your comment
that way.


That is entirely my fault, of course. I have a particular style which I
consider robust, and I all too easily fall into the trap of answering
questions w.r.t. "my" way of writing C.
I agree with your list of reasonable contents for a header
file, except that I would also include declarations of global
variables. Global variables are often a sign of bad design, but not
always, and as a practical matter it can take a long time to remove
them from legacy code.
I think that to offer my opinion re global variables here would side-track
the discussion too much. :-)
Since you're not talking about files containing function bodies, that
reduces the likelihood of running into problems. Of course, any
problem that can be created in code in a function body, can also be
created by use of a macro defined in a header file, and expanding into
such code when actually used.
Absolutely correct, and that's Yet Another Reason to avoid macros whenever
possible.
However, even ignoring the possible problems with macros, there's
other C/C++ problems that can occur even in a properly used header
file. Any header which contains any of the following features can be a
problem:

1. Declarations of identifiers matching C++ keywords that are not C
keywords.
Funny you should say that, because I was considering that very problem (or
rather, the potential for it) earlier today, in Real Life. Hence this
reply, really. I must confess that I'm a bit of a fan of using C++ keywords
in C code; I must make a mental note not to do this in headers! (I don't
recall ever doing this, but then I don't recall not doing it, either.)
2. Tentative definitions.
From my perspective that's Someone Else's Problem, but yes, it's a fair
point.

<some less common points snipped>
6. Storage class specifiers as part of the definition of a pure type;
that is, a typedef, or a struct, union, or enumeration declaration
that is not also the declaring any particular object of that type.
Could you possibly elaborate on this? What, specifically, is the problem
here?
7. A typedef with the same name as a struct tag.
This one may yet bite me, although in recent years I've got into the habit
of using a trailing underscore on the tag specifically to disambiguate it
(mainly for the benefit of Visual C++'s "go to definition" hotkey).

<a number of less common points snipped>
14. Any use of an enumeration. Different enumeration types can have
different sizes and representations in C++, which would make virtually
any use of them in a header file context dangerous.
I don't /think/ this is a problem for me, but thanks for the warning.

I'll grant you, use of these features isn't very common, and many of
these problems are guaranteed to produce diagnostics. However, they
are things you need to worry about, beyond simply inserting extern
"C".


Thank you for a most informative article, which I've saved for future
reference. As a result of reading it, I'm now far less convinced than I was
about the ease with which C code can be linked to C++ code. I'll have to
think about it, I guess.

--
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 #436

On Mon, 27 Oct 2003, Fergus Henderson wrote:

Keith Thompson <ks*@cts.com> writes:
The real problem is that a cast is too blunt an instrument. If there
were an operator that meant "convert a void* value to FOO*", [...]
You might find the following macros useful:

/*
** CHECK_EXPR_TYPE (expr, type):
** This macro checks that the given expression has a type
** which is compatible with (assignable to) the specified type,
** forcing a compile error if it does not.
** It does not evaluate the expression.
** Note that the specified type must be a complete type,
** i.e. it must not be a pointer to a struct which has
** not been defined.
**
** The implementation of this macro looks like it dereferences
** a null pointer, but because that code is inside sizeof(), it will
** not get executed; the compiler will instead just check that it is
** type-correct.


Chapter and verse, please. You may very well be correct about
the dereferencing-of-null-pointer's not mattering, but I'd like
to be sure.
*/
#define CHECK_EXPR_TYPE (expr, type) \
((void) sizeof(*(type *)NULL = (expr)))
Note that this code is not very general; it doesn't work for e.g.

CHECK_EXPR_TYPE (foo, int(*)[5]);

or really anything except the basic scalar types, user-defined
types, and pointers-to-pointers-to-...-those types. Arrays and
function pointers break it badly.

/*
** CAST_TO_FROM(ex pr, srctype, desttype):
** The same as EXPR cast to type DESTTYPE,
** except that it checks that the expression has a type which is
** compatible with (assignable to) SRCTYPE, forcing a compile error
** if it does not.
*/
#define CAST_FROM_TO(ex pr, srctype, desttype) \
Whoops!
(CHECK_EXPR_TYP E((expr), srctype), \
(desttype) (expr))


Of course, this macro doesn't check whether srctype itself
is implicitly convertible to desttype, so you could use it
to "safely" convert 'double' to 'int *' if you wanted. We
can't have everything.

-Arthur

Nov 13 '05 #437
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bn******* ***@hercules.bt internet.com>.. .
James Kuyper wrote:

<snip>
"Correct code for that implementation" is "correct code", for that
implementation. The fact that it's not portable doesn't make it
incorrect.


MMMV. :-)


I don't recognize that one. Sorry. However, I'd like to point out that
not all code needs to be portable. Some code is, by deliberate and
well-thought-out design, intended only for a restricted set of
platforms.

Still, deliberately non-portable code is a specialized need, and I'd
certainly recommend that all C programmers learn first how to write
portable code. They can learn how to write code that is deliberately
non-portable, when and if they ever run into one of the rare
situations where it is appropriate.

....
6. Storage class specifiers as part of the definition of a pure type;
that is, a typedef, or a struct, union, or enumeration declaration
that is not also the declaring any particular object of that type.


Could you possibly elaborate on this? What, specifically, is the problem
here?


It's legal in C, and not legal in C++. Of course, it's not much of a
problem. The reason it was made illegal in C++ is that storage class
specifiers are meaningless except when actually declaring an object
requiring storage.
Nov 13 '05 #438
James Kuyper <ku****@wizard. net> scribbled the following:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bn******* ***@hercules.bt internet.com>.. .
James Kuyper wrote:

<snip>
> "Correct code for that implementation" is "correct code", for that
> implementation. The fact that it's not portable doesn't make it
> incorrect.
MMMV. :-)

I don't recognize that one. Sorry. However, I'd like to point out that
not all code needs to be portable. Some code is, by deliberate and
well-thought-out design, intended only for a restricted set of
platforms.


My Mileage May Vary.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli
Nov 13 '05 #439
Joona I Palaste wrote:
My Mileage May Vary.

Do they still say "mileage" in the UK? Shouldn't it be kilometerage?

Brian Rodenborn
Nov 13 '05 #440

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.
0
11188
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10439
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...
1
7991
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
7148
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
5825
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...
0
6025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4644
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
4249
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3253
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.