Making C better (by borrowing from C++)

Discussion in 'C Programming' started by masood.iqbal@lycos.com, Feb 12, 2005.

  1. Guest

    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
    , Feb 12, 2005
    #1
    1. Advertising

  2. infobahn Guest

    wrote:
    >
    > 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,


    Pointless without adding constructors and destructors too, which
    might be further than the C community wants to go.

    > references,


    An excellent way to confuse people still further wrt pointers.

    > consts,


    Got them.

    > declaring variables just before use etc.


    Got them in C99.
    infobahn, Feb 12, 2005
    #2
    1. Advertising

  3. On 12 Feb 2005 05:36:00 -0800,
    <> wrote:

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


    Note that quite a few features have already made it back -- void,
    declaring variables in the middle of code, single-line comments for
    instance.

    There is no point to new and delete without constructors and
    destructors, and to have those you'd need to introduce classes (and then
    you'd have "C with classes", which was how C++ started out.

    References are nice in some ways, but are really just syntactic sugar
    round pointers, and can be even more confusing (especially when it isn't
    obvious that a parameter may change).

    Proper named and typed constants would indeed be useful, C's version of
    const is a compiler convenience, and I can see those making it into C at
    some point, but most of the other features depend on classes.

    I can think of other things I'd like more. A typeof operator, for
    example, and to have known-width arithmetic types build into the
    language instead of via a header file (which isn't guaranteed to even
    provide them). I'd really like a portable way of having enum values as
    strings so that I could use strtoenum() for input and a printf
    descriptor to output an enum as its named values. I'd like those in C++
    as well...

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


    Why not use C++ instead, if that's what you like? That's what I do, I
    use C++ for things where I want high-level features and I use C for
    portability (adding C++ features to C won't make them portable, we're
    over 5 years after the last C standard came out and I still can't safely
    assume that any of the features introduced are portable (there seems to
    be possibly one compiler and library which is fully C99 compliant) and
    some systems don't even fully implement the standard from 10 years
    before that).

    Chris C
    Chris Croughton, Feb 12, 2005
    #3
  4. CBFalconer Guest

    Chris Croughton wrote:
    >

    .... snip ...
    >
    > References are nice in some ways, but are really just syntactic
    > sugar round pointers, and can be even more confusing (especially
    > when it isn't obvious that a parameter may change).


    I used to miss them sorely, but eventually got used to the idea
    that the caller can tell when it is exposing its internal data to
    outside influence without examining the header of the called
    function. Of course the cost is that pointers are not under
    control, and that proper verification is impossible to all
    practical purposes.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Feb 12, 2005
    #4
  5. On 12 Feb 2005 05:36:00 -0800, in comp.lang.c ,
    wrote:

    >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?


    Some C++ features made it into C99. Others don't make sense in C, such as
    new, delete, reference variables and so forth. For your info consts have
    been in C for decades, and declare-before-use is in C99.

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


    Then use C++

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    Mark McIntyre, Feb 12, 2005
    #5
  6. jacob navia Guest

    wrote:
    > 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
    >


    The lcc-win32 compiler adds some features of C++ like:

    o operator overloading
    o default arguments to functions
    o references

    Declaring variables just vefore use is part of the
    C99 standard

    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Feb 12, 2005
    #6
  7. On 12 Feb 2005 05:36:00 -0800, in comp.lang.c ,
    wrote:

    >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?


    Some C++ features made it into C99. Others don't make sense in C, such as
    new, delete, reference variables and so forth. For your info consts have
    been in C for decades, and declare-before-use is in C99.

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


    Then use C++

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Feb 12, 2005
    #7
  8. In article <>,
    wrote:

    > I know that this topic may inflame the "C language Taleban"


    Not very clever starting with an insult like that, if you want to be
    taken serious?
    Christian Bau, Feb 12, 2005
    #8
  9. On Sat, 12 Feb 2005 18:47:44 GMT, CBFalconer
    <> wrote:

    > Chris Croughton wrote:
    >>

    > ... snip ...
    >>
    >> References are nice in some ways, but are really just syntactic
    >> sugar round pointers, and can be even more confusing (especially
    >> when it isn't obvious that a parameter may change).

    >
    > I used to miss them sorely, but eventually got used to the idea
    > that the caller can tell when it is exposing its internal data to
    > outside influence without examining the header of the called
    > function. Of course the cost is that pointers are not under
    > control, and that proper verification is impossible to all
    > practical purposes.


    When I'm writing C++ I use reference parameters, because I like the
    cleanness of the call. However I've been bitten by them several times,
    so it's a kind of "love-hate" relationship, I can see good reasons both
    for using them and for avoiding them. The thing I do like and miss from
    C++ (which C99 has now but none of the compilers for which I have to
    write portable C code supports) is declaring 'local' loop variables:

    for (int i = 0; i < n; ++i)
    ...

    I'm always forgetting that C89 doesn't have that...

    Chris C
    Chris Croughton, Feb 12, 2005
    #9
  10. writes:
    > 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.


    As others have pointed out, some of these things are already in C99.
    And I believe that prototypes were first introduced in (pre-standard)
    C++ and added to C with the 1989 ANSI standard. I don't think there's
    any great resistance to the idea of adding some C++ features to C *if*
    they fit into the C language.

    On the other hand, if you want C++ features that C doesn't have, the
    easiest way to get them is to use C++.

    Incidentally, calling people "Taleban" is a really bad way to
    influence them.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 12, 2005
    #10
  11. Randy Howard Guest

    In article <>,
    says...
    > 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.


    What would be the point? There is nothing precluding you from using
    C++, without the "OO stuff", to write whatever it is you are interested
    in that you can't do the way you want it done in pure C.

    > I am talking about the non-OO stuff, that seems to be handled much
    > more elegantly in C++, as compared to C.


    C++ is over there --------->

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


    Rename your C program file extensions to .cpp, find every occurrence
    of code in those programs that uses something in a different way than
    it is in C++, then add those features that you like from C++.

    The proper place to discuss this is in a newsgroup with "c++" in the
    title.

    --
    Randy Howard (2reply remove FOOBAR)
    "Making it hard to do stupid things often makes it hard
    to do smart ones too." -- Andrew Koenig
    Randy Howard, Feb 13, 2005
    #11
  12. Randy Howard Guest

    In article <420e5560$0$19408$>,
    says...
    > The lcc-win32 compiler adds some features of C++ like:


    And using those features means you are writing in some other
    language that is not C, call it "Navia" perhaps?


    --
    Randy Howard (2reply remove FOOBAR)
    "Making it hard to do stupid things often makes it hard
    to do smart ones too." -- Andrew Koenig
    Randy Howard, Feb 13, 2005
    #12
  13. Guest

    wrote:
    > 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,


    new & delete are really tied to to the semantics of constructor and
    destructors in C++. There is little point in adding them to C without
    adding the whole "class" paradigm.

    > [...] references,


    Yes, I absolutely agree with this. References allow a receiving
    function to better rely on the fact that the variable is supposed to be
    backed by valid memory -- specifically it gives a syntactically sound
    way of passing "pointers" that have better chances of being good at
    compile time (its still possible to pass an array element that's beyond
    the array's boundaries -- but at least you know you can't pass a
    pointer to the heap that has been freed, or NULL, or just pure
    garbage). As it is today, C programmers do this with raw pointers
    which increases the potential danger by allowing for semantics that
    usually are not intended.

    > [...] consts,


    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.

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

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


    Personally, I think namespace should be added to that list. The single
    shared global namespace situation that exists in C today is nothing
    short of haphazard.

    --
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
    , Feb 13, 2005
    #13
  14. infobahn Guest

    Keith Thompson wrote:
    >
    > Incidentally, calling people "Taleban" is a really bad way to
    > influence them.


    That kind of depends on what you mean by "influence".
    infobahn, Feb 13, 2005
    #14
  15. writes:
    [...]
    > new & delete are really tied to to the semantics of constructor and
    > destructors in C++. There is little point in adding them to C without
    > adding the whole "class" paradigm.


    They're tied to constructors and destructors in C++, but they needn't
    be in C. They could be added to the language as built-in versions of
    malloc() and free(). For example:

    int *ptr = new int;
    ...
    delete ptr;

    is valid C++, and has nothing to do with constructors or destructors.

    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().

    [...]

    > Personally, I think namespace should be added to that list. The single
    > shared global namespace situation that exists in C today is nothing
    > short of haphazard.


    Agreed.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 13, 2005
    #15
  16. infobahn Guest

    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?
    infobahn, Feb 13, 2005
    #16
  17. On 12 Feb 2005 19:13:45 -0800,
    <> 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.

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

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

    >
    > Personally, I think namespace should be added to that list. The single
    > shared global namespace situation that exists in C today is nothing
    > short of haphazard.


    Definitely. Then we could get rid of "static used for
    non-external-scope" and leave it for "variable does not change between
    invocations of the function". As well as getting rid of prefixes just
    in case something else has a similarly names function.

    Chris C
    Chris Croughton, Feb 13, 2005
    #17
  18. jacob navia Guest

    wrote:

    > wrote:
    >
    >>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,

    >
    >
    > new & delete are really tied to to the semantics of constructor and
    > destructors in C++. There is little point in adding them to C without
    > adding the whole "class" paradigm.
    >


    I agree. there is not a lot to be gained with "new" and "delete"
    without going into constructors.

    >
    >>[...] references,

    >
    >
    > Yes, I absolutely agree with this. References allow a receiving
    > function to better rely on the fact that the variable is supposed to be
    > backed by valid memory -- specifically it gives a syntactically sound
    > way of passing "pointers" that have better chances of being good at
    > compile time (its still possible to pass an array element that's beyond
    > the array's boundaries -- but at least you know you can't pass a
    > pointer to the heap that has been freed, or NULL, or just pure
    > garbage). As it is today, C programmers do this with raw pointers
    > which increases the potential danger by allowing for semantics that
    > usually are not intended.
    >


    Again, I agree. The lcc-win32 compiler implements them, and they do
    improve program security.

    >
    >>[...] consts,

    >
    >
    > 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.
    >
    >
    >>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.
    >
    >
    >>I am asking this question with a vested interest. I would really
    >>like to use these features in my C programs.

    >


    Use the lcc-win32 C compiler:
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Feb 13, 2005
    #18
  19. jacob navia Guest

    Randy Howard wrote:

    > In article <420e5560$0$19408$>,
    > says...
    >
    >>The lcc-win32 compiler adds some features of C++ like:

    >
    >
    > And using those features means you are writing in some other
    > language that is not C, call it "Navia" perhaps?
    >
    >

    The C99 standard explicitly allows extensions, and those extensions have
    been designed keeping compatible with the C99 standard.

    There are NO NEW keywords. You can still say:

    int operator = 45;

    int operator(int g);

    etc.

    Each implementation of the C standard can extend the language
    in the form prescribed by the standard, and *most* of them
    do.

    Your answer is typical for people that say that C should stay
    at the level of assembly language and only C++ should have any
    higher level constructs.

    Operator overloading is a standard extension that is used in Fortran,
    for instance, and in many other languages.
    jacob navia, Feb 13, 2005
    #19
  20. Randy Howard Guest

    In article <420f183d$0$6593$>,
    says...
    > Randy Howard wrote:
    >
    > > In article <420e5560$0$19408$>,
    > > says...
    > >
    > >>The lcc-win32 compiler adds some features of C++ like:

    > >
    > >
    > > And using those features means you are writing in some other
    > > language that is not C, call it "Navia" perhaps?
    > >
    > >

    > The C99 standard explicitly allows extensions, and those extensions have
    > been designed keeping compatible with the C99 standard.


    And what happens when you want to port that source code to a platform
    that doesn't have a compiler with your extensions?

    --
    Randy Howard (2reply remove FOOBAR)
    "Making it hard to do stupid things often makes it hard
    to do smart ones too." -- Andrew Koenig
    Randy Howard, Feb 13, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. whizkid

    TIME borrowing in synthesis

    whizkid, Nov 2, 2004, in forum: VHDL
    Replies:
    1
    Views:
    6,432
    Tom Verbeure
    Nov 2, 2004
  2. Peter Bencsik
    Replies:
    2
    Views:
    810
  3. Masood

    Making C better (by borrowing from C++)

    Masood, Dec 23, 2007, in forum: C Programming
    Replies:
    247
    Views:
    3,002
    David Thompson
    Jan 14, 2008
  4. Roedy Green

    borrowing Constants

    Roedy Green, Sep 24, 2011, in forum: Java
    Replies:
    22
    Views:
    579
    Arne Vajhøj
    Sep 26, 2011
  5. Novice

    "Borrowing" code

    Novice, Feb 16, 2012, in forum: Java
    Replies:
    60
    Views:
    1,034
    Novice
    Feb 20, 2012
Loading...

Share This Page