is C appropriate for intro to computers?

This is a discussion on is C appropriate for intro to computers? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I have a question. Is g++ the same fast as gcc? If not there will always be a reason to ...

  1. #31
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I have a question. Is g++ the same fast as gcc? If not there will always be a reason to use C over C++. I found this http://shootout.alioth.debian.org/gp4/cpp.php for example.
    But in any case, why wouldn't they two compilers be the same? And why have two compilers and not an upgraded one with a flag -Conly.

  2. #32
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Quote Originally Posted by C_ntua View Post
    I have a question. Is g++ the same fast as gcc? If not there will always be a reason to use C over C++. I found this http://shootout.alioth.debian.org/gp4/cpp.php for example.
    But in any case, why wouldn't they two compilers be the same? And why have two compilers and not an upgraded one with a flag -Conly.
    The C implementation and C++ implementation of those tests are different code, they work differently. If you compiled the C example with g++ instead of gcc it would come out exactly the same.

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by Mario F. View Post
    Why? Doesn't this simply mean that it is poor form to not declare your functions?

    If the behavior is unspecified (and you have a lot of that on C++ too) the general rule of thumb is... don't do it.
    Of course you shouldn't do it, but should a compiler actively allow it? I think not.
    The compiler should complain and avoid undefined behavior. It's very easy to hide this little warning.
    So IMO, it's a very good thing C++ forbid this nonsense, since it IS bad practice not to use headers.

    Quote Originally Posted by C_ntua View Post
    I have a question. Is g++ the same fast as gcc? If not there will always be a reason to use C over C++. I found this http://shootout.alioth.debian.org/gp4/cpp.php for example.
    But in any case, why wouldn't they two compilers be the same? And why have two compilers and not an upgraded one with a flag -Conly.
    It would not be a reason to use C over C++, since language-wise, they are pretty much equal in performance, some places where C perform better, some places where C++ perform better.
    Note that a poor C++ compiler may produce slower code than the equivalent C compiler, but that is hardly the language's fault and the solution is to get a BETTER compiler.
    Last edited by Elysia; 10-06-2008 at 09:28 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Doodle77 View Post
    The C implementation and C++ implementation of those tests are different code, they work differently. If you compiled the C example with g++ instead of gcc it would come out exactly the same.
    Or at least, for 99% of the cases, that would be true. There are some subtle differences between gcc and g++ that SOMETIMES cause a small difference.

    But in essence the Doodle77 statement is correct: The two pieces of code compared here are not identical - they use the same basic algorithm, but it's implemented in different ways.

    Compare:
    http://shootout.alioth.debian.org/gp...&lang=gpp&id=3
    http://shootout.alioth.debian.org/gp...otide&lang=gcc
    and you can see that the code is not very similar at all [it is of course similar in the aspect that it's implementing the same functionality - but in the detail, it is quite different]. I don't see any obvious "big hit" on the performance, but C++ tends to offer subtle ways to "hide" performance hits - a possible candidate is that the C version uses realloc to allocate a 10K larger chunk of memory inside the initial fgets() loop - which is done differently in the C++ version.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #35
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Quote Originally Posted by Elysia View Post
    Of course you shouldn't do it, but should a compiler actively allow it? I think not.
    The compiler should complain and avoid undefined behavior. It's very easy to hide this little warning.
    So IMO, it's a very good thing C++ forbid this nonsense, since it IS bad practice not to use headers.
    I don't even know where to start dismantling this opinion of yours.

    Code:
    // C
    int main() {
    
        int foo = 12;
        bar(foo);
    
        return;
    
    }
    
    void bar(int foo) { ; }
    Visual Studio complains starting on warning level 3 with default project setup.
    Didn't test it on gcc, but seen my share of "Implicit declaration of function..." warnings. So not sure what do you mean. The compiler complains.

    Meanwhile...

    Code:
    // C++
    int main() {
    
        int foo[5] = {1,2,3,4,5};
        int bar[5];
    
        int i = 0;
        while (i < 5)
            bar[i] = foo[i++];
    
    }
    Doesn't sound a blip on warning level 4.

    So back to your argument:

    >> So IMO, it's a very good thing C++ forbid this nonsense, since it IS bad practice not to use headers.

    So it is evaluation order dependency and yet I got no warning. In fact the C++ standard says I don't have to have a warning. It's undefined behavior... as it is the result of implicit declaration of functions in C. More, implicit declaration results in a warning, whereas evaluation order dependency doesn't. And on some circumstances the compiler couldn't even if it wanted too. Which is "worst"? Don't answer.

    One last small little unimportant thing of absolutely no consequence at all: C99 removed implicit declaration of functions. So... yeah.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #36
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Classic error:
    Code:
    // C
    int main() {
    
        int* foo;
        foo = (int*)malloc(sizeof(int));
    
        return;
    }
    Yes, Visual Studio is actually kind enough to warn me. Are other compilers just as nice? No.
    And just to clarify: I didn't include the proper header.

    And what does the C++ sample have to do with anything?
    No, it's not good that it doesn't warn, but it doesn't warn regardless if it's C or C++ code, does it?
    My point is that C++ actually improved on something - it removed something very nasty and dangerous from C. That doesn't mean C++ is always better in everything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #37
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    *opens mouth to say something but gives up...*
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #38
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Mario F. View Post
    Doesn't sound a blip on warning level 4.
    Why would it?
    Quote Originally Posted by Elysia View Post
    Yes, Visual Studio is actually kind enough to warn me. Are other compilers just as nice? No.
    And just to clarify: I didn't include the proper header.
    Which compiler did not give a warning?

  9. #39
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by robwhit View Post
    Which compiler did not give a warning?
    I do not know. I only tried with Visual Studio.
    But the FAQ does mention that it hides warnings, so obviously it can mask it on certain compilers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #40
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    <my two cents>I like C.*</my two cents>


    * My two cents are actually an arcade token and a plastic button. So actual monetary may vary depending on the exchange rates.

  11. #41
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    It saddens me to see that someone likes C*.
    * Individual opinion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,958
    Well, if you interpret that as a regular expression, then C.* includes C, C++ and C#.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #43
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Yeah, it's great to see C++ among them, but it's still a regular expression, meaning C is among them, which I don't have to clarify my feelings for. And further among them is C#, another language for which I have no love, although perhaps not as much dislike as for C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #44
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    I do not know. I only tried with Visual Studio.
    But the FAQ does mention that it hides warnings, so obviously it can mask it on certain compilers.
    The cast does mask the implicit declaration. However, there is really no use for an implicit declaration, except for cutting corners, which really isn't needed, I'd guess.

    It's an easily detected error that pretty much all compilers recognize, and if it doesn't, you could just use a static checking tool instead, or compile it with another compiler. It's a problem that's fixed with a compiler switch, providing a way to not have that problem and write in fully conforming code otherwise. I don't see what's so impractical about this.

  15. #45
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by robwhit View Post
    The cast does mask the implicit declaration. However, there is really no use for an implicit declaration, except for cutting corners, which really isn't needed, I'd guess.
    That's the point.
    It's not needed. It's bad practice.
    So why is it allowed?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numeric addresses for computers
    By great in forum C Programming
    Replies: 4
    Last Post: 08-23-2010, 11:53 AM
  2. Computers as authors
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-22-2004, 07:55 PM
  3. Industrial vs home computers
    By nbo10 in forum Tech Board
    Replies: 10
    Last Post: 09-01-2004, 02:04 AM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  5. Love programming, hate computers
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-14-2002, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21