473,699 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making C better (by borrowing from C++)

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C? No I am not talking out the OO stuff. I am talking
about the non-OO stuff, that seems to be handled much more elegantly in
C++, as compared to C. For example new & delete, references, consts,
declaring variables just before use etc.

I am asking this question with a vested interest. I would really like
to use these features in my C programs.

Masood

Nov 14 '05
85 3262
Chris Croughton wrote:
On 12 Feb 2005 19:13:45 -0800, websnarf wrote:
C has const. I don't quite see how C++'s consts are not redundant
with either #define or enums. You can even make enums locally
scoped.


Try declaring an enum with floating point type. Or using a const
int as a case value. I think that C++ should have found something
else to call them (and both C and C++ should have stopped the
overloading of the 'static' keyword, C++ even adds yet another
meaning to it), but typed constants are useful and don't have the
pitfalls of macros.


Ok, I see -- you really want are read-only variables. Fair enough.
declaring variables just before use etc.


This is in C99, and personally I would rather have seen this
*REMOVED* from C++, than added to C. It makes the job of reading
code twice as hard -- if we wanted that, we might as well be using
Perl. If you want to do "just in time constructors", wrap your
code with additional scope.


Used properly (as in declaring loop variables only when they are
used) it makes it more readable, not less. I've seen a lot of
errors where someone has used a variable (often i or j) for a loop,
it has compiled (it was already declared) but it has done unexpected
things because it wasn't obvious that the variable was already in
use.


Ok, so then that's just it, then isn't it? Using it "properly" as you
say boils down to declarations of variables in the first expression of
a control statement such as for, while, if, or switch. Fine, but
that's a narrower extension than C++'s scheme.

At least my namespace extension remains intact. :)

---
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 14 '05 #21
Randy Howard wrote:
And what happens when you want to port that source code to a platform
that doesn't have a compiler with your extensions?


Most of it compiles in C++.

Nov 14 '05 #22
In article <42************ ***********@new s.wanadoo.fr>,
ja***@jacob.rem comp.fr says...
Randy Howard wrote:
And what happens when you want to port that source code to a platform
that doesn't have a compiler with your extensions?


Most of it compiles in C++.


So you assume that all platforms even have a C++ compiler? If so,
then why not just write in C++ to begin with?

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #23
infobahn <in******@btint ernet.com> writes:
Keith Thompson wrote:

Since C already has malloc() and free(), new and delete wouldn't add
much value, but if I were designing a C-like language from scratch I'd
probably use new and delete *instead of* malloc() and free().


How would you deal with resizing? old?


I don't know. Since realloc() isn't used all that much, perhaps it
could remain a library function. I'll let you know when I get around
to designing a C-like language from scratch.

(Not sure what you mean by "old?".)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #24
On 13 Feb 2005 03:50:04 -0800, we******@gmail. com
<we******@gmail .com> wrote:
Chris Croughton wrote:
On 12 Feb 2005 19:13:45 -0800, websnarf wrote:
> C has const. I don't quite see how C++'s consts are not redundant
> with either #define or enums. You can even make enums locally
> scoped.
Try declaring an enum with floating point type. Or using a const
int as a case value. I think that C++ should have found something
else to call them (and both C and C++ should have stopped the
overloading of the 'static' keyword, C++ even adds yet another
meaning to it), but typed constants are useful and don't have the
pitfalls of macros.


Ok, I see -- you really want are read-only variables. Fair enough.


No, I have read-only variables in C, that's what C const does. What I
would like is the C++ "named typed constants", they aren't variables at
all (they may not have any existence in storage in many cases).
> > declaring variables just before use etc.
>
> This is in C99, and personally I would rather have seen this
> *REMOVED* from C++, than added to C. It makes the job of reading
> code twice as hard -- if we wanted that, we might as well be using
> Perl. If you want to do "just in time constructors", wrap your
> code with additional scope.


Used properly (as in declaring loop variables only when they are
used) it makes it more readable, not less. I've seen a lot of
errors where someone has used a variable (often i or j) for a loop,
it has compiled (it was already declared) but it has done unexpected
things because it wasn't obvious that the variable was already in
use.


Ok, so then that's just it, then isn't it? Using it "properly" as you
say boils down to declarations of variables in the first expression of
a control statement such as for, while, if, or switch. Fine, but
that's a narrower extension than C++'s scheme.


What do you mean by "the first expression"?

Actually, using variable declaration mixed with code can be readble in
other cases as well, but in my opinion the use in a control statement
(and personally I only use them in for loops) is the most useful and the
one which definitely makes the code clearer. The ability to declare
them anywhere, as in C++, can not only make the code les clear but can
muddy the syntax itself (there are cases in C++ where it is ambiguous
whether something is a declaration or a statement).
At least my namespace extension remains intact. :)


IMO namespaces definitely make things clearer and are a Good Thing(tm).
Possibly the best feature of C++...

Chris C
Nov 14 '05 #25
Chris Croughton <ch***@keristor .net> writes:
On 12 Feb 2005 19:13:45 -0800, we******@gmail. com
<we******@gmail .com> wrote:

[...]
This is in C99, and personally I would rather have seen this *REMOVED*
from C++, than added to C. It makes the job of reading code twice as
hard -- if we wanted that, we might as well be using Perl. If you want
to do "just in time constructors", wrap your code with additional
scope.


Used properly (as in declaring loop variables only when they are used)
it makes it more readable, not less. I've seen a lot of errors where
someone has used a variable (often i or j) for a loop, it has compiled
(it was already declared) but it has done unexpected things because it
wasn't obvious that the variable was already in use.


You're talking about a different feature. For loop variables, you can
do the following:

...
for (i = 0; i < N; i ++) { ... }

Independently, you can do:

int i;
/* code that uses i */
int j;
/* code that uses j */

If abused, this can cause confusion, but it does add some flexibility.

Both features are supported in C99 and C++, and not in C90.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #26
Keith Thompson wrote:

infobahn <in******@btint ernet.com> writes:
Keith Thompson wrote:

Since C already has malloc() and free(), new and delete wouldn't add
much value, but if I were designing a C-like language from scratch I'd
probably use new and delete *instead of* malloc() and free().
How would you deal with resizing? old?


I don't know. Since realloc() isn't used all that much,


It isn't? Perhaps I use it more than most, then.
perhaps it
could remain a library function. I'll let you know when I get around
to designing a C-like language from scratch.

(Not sure what you mean by "old?".)


Nothing more sinister than "not exactly new" :-)
Nov 14 '05 #27

"infobahn" <in******@btint ernet.com> wrote in message
news:42******** *******@btinter net.com...
Keith Thompson wrote:

Since C already has malloc() and free(), new and delete wouldn't add
much value, but if I were designing a C-like language from scratch I'd
probably use new and delete *instead of* malloc() and free().


How would you deal with resizing? old?


renew? :)
Nov 14 '05 #28
Serve Lau wrote:

"infobahn" <in******@btint ernet.com> wrote in message
news:42******** *******@btinter net.com...
Keith Thompson wrote:

Since C already has malloc() and free(), new and delete wouldn't add
much value, but if I were designing a C-like language from scratch I'd
probably use new and delete *instead of* malloc() and free().


How would you deal with resizing? old?


renew? :)


Much better :-)
Nov 14 '05 #29
>>>Since C already has malloc() and free(), new and delete wouldn't add
much value, but if I were designing a C-like language from scratch I'd
probably use new and delete *instead of* malloc() and free().


How would you deal with resizing? old?


renew? :)


Come on, "real C++ programmers" don't resize. They just allocate more
objects and delete the old stuff (that is, when they don't forget to).

That leads to bloatware, but new is so "cute" compared to malloc().
lol ;-)
Nov 14 '05 #30

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

Similar topics

204
4999
by: Masood | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am talking about the non-OO stuff, that seems to be handled much more elegantly in C++, as compared to C. For example new & delete, references, consts, declaring variables just before use etc. I am asking this question with a vested interest. I...
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
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
7745
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
6532
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
5869
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
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.