Shop OBEX P1 Docs P2 Docs Learn Events
What exactly is C/C++ — Parallax Forums

What exactly is C/C++

LoopyBytelooseLoopyByteloose Posts: 12,537
edited 2012-09-04 18:03 in General Discussion
I am reading ANSI C in depth and have tried to ignore pursuit of C++ at this point on the learning curve. But I see Parallax has created a compiler for the Propeller that is C/C++.

Frankly, I'd prefer to first learn C and later comprehend C++ as the need arrises. But I begin to wonder what is going on here. I am very comfortable now with reading ANSI C and understanding how it relates to both computers in general and microcontrollers. At times, I tend to feel programmers can be an extremely nuanced nusance and fear the C++ is one of those occasions.

So can one explain if there is a real need to acquire a precise understand of C++ in order to use C with the Propeller or is the C/C++ indicative of the learner being able to choose either C or C++ according to their abilities and ambitions?
«13

Comments

  • TorTor Posts: 2,010
    edited 2012-08-17 10:44
    The latter. GCC (capitalized) is really the Gnu Compiler Collection (way back it used to be Gnu C Compiler), so you get several frontends for the price of one. g++ is one that comes together with the C compiler as standard. But you don't have to use g++, I almost exclusively use just the C compiler part of GCC on other platforms. I haven't started yet with propgcc but I haven't seen anything yet to indicate that g++ should be somehow madatory on the prop.

    -Tor
  • David BDavid B Posts: 592
    edited 2012-08-17 11:19
    The C++ compilers I've worked with will, in almost every case, compile and execute a C program perfectly, with maybe just a little tweaking of include files.

    So if you don't need or don't like all the scope and permission verbosity of C++, you should be able to get by with just using C.
  • jazzedjazzed Posts: 11,803
    edited 2012-08-17 13:34
    What is it? C++ is what you make of it.

    C++ is flexible. C++ can be complicated "large embedded systems" used in controlling SONET/STM switched optical networks (backbone of world communications). It can also be as simple as the chopped language (Wiring/Processing) used with Arduino. Wiring is essentially C++ for the rest of us. Nothing keeps a C++ program from adopting the same ideas except will and work. Of course C++ is a very long, thick rope that you can get quickly tangled in (and hung), but with good guidance, it's cake.

    Spin is a very nice analog to simple C++ once you get past the small syntax differences. Actually Eric, who is very talented, wrote a Spin to C++ translator. The translation is easier mainly because C++ uses objects too.

    The same could have been done for straight C, but the coding method would have to be different. C++ can be mimicked with C pretty easy if you know both languages. C++ just standardizes what is possible with C regarding objects without being so ugly. C++ syntax does make inheritance (and other things) possible that are not easy to do in straight C. Straight C is generally a very disorganized language where global scope of functions cause all kinds of problems. If all you need to do is blink an LED, then you will never see these issues in C.

    Some folks will tell you to learn an object oriented programming language (OOP) so that you can use C++ effectively. After using C++ for decades I don't really believe that is necessary despite statements like "but that's not really C++" which is valid to a point, but mostly irrelevant. Inheritance for example is a valued OOP concept in C++ and Java. Inheritance (and other things) can be difficult to swallow if you've never heard of it, but it's a super easy concept once you've had a simple explanation and a chance to interact with a teacher about what's happening.

    So what exactly is C++? It's an extension of C with good organization, and it is a joy to use with the right guidance. Of course there is a time and place for everything, so C++ is not an answer for every problem. Open minded users will really enjoy some of the C/C++ examples and guidance Parallax has to offer when it's published.
  • TorTor Posts: 2,010
    edited 2012-08-17 13:44
    David B wrote: »
    The C++ compilers I've worked with will, in almost every case, compile and execute a C program perfectly, with maybe just a little tweaking of include files.

    So if you don't need or don't like all the scope and permission verbosity of C++, you should be able to get by with just using C.
    While this is true (you can often compile plain C code with a C++ compiler), it may confuse matters a bit. With propgcc (I just downloaded and verified that it's like any other GCC), you get _two_ distinct compilers: A C++ compiler, and a C compiler. Two different front ends for the code generator. C++ (c++/g++ binaries) invokes C++ and compiles C++ code, and will eat most C code too, but if you write C then you'll normally use the C compiler (called gcc in the propgcc suite).
    I have not yet looked at how SimpleIDE invokes the compilers though.

    -Tor
  • prof_brainoprof_braino Posts: 4,313
    edited 2012-08-17 13:50
    jazzed wrote: »
    .... a very long, thick rope... it's cake.

    A long thick rope of ... cake...

    Good enough for me. Sold!
  • Heater.Heater. Posts: 21,230
    edited 2012-08-17 15:14
    Good grief,
    I believe it is possible to understand C and most of the libraries that come with it in a week or month or two.
    However I do not believe there is anyone alive who understands the full impact of C++ and all the libs that come with it.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-08-17 17:11
    C came first and is a pretty simple language with some squirrely syntax in a few areas. C++ started off as a fairly simple object oriented extention to C, but ballooned with the addition of templates and the STL into a huge programming language. They are both worth knowing as they influenced a major family of programming languages including: C#, Groovy, Java, JavaScript, PHP, Perl, Python, Ruby, and probably a few others. Ignore them at your peril!

    Spin is a bit like a typeless Python without a dynamic memory allocator. Which means it is conceptually similar to C++ using only static allocation of objects. The Arduino libraries use C++ in exactly the same manner, so I would imagine that would work well on the Propeller as well.
  • GadgetmanGadgetman Posts: 2,436
    edited 2012-08-17 17:28
    I once tried to program in C++, and even took a short course(I was already able to program in Pascal and ADA)

    What I learned is that just about anything in C++ is a pointer, especially when you need something else...
    I did learn to ALWAYS create the 'destructor' at the same time as I created a class, though.

    Anyone want to buy Watcom C/C++ V11.0 compiler for DOS/Windows and OS/2?
    Complete with all manuals...
    (Yeah, the version that never really worked on OS/2... Should really take a look at Open Watcom soon...)
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-08-17 18:26
    What is C?

    For me, it is a way of breaking out of the 32k limitation of the propeller chip. C lets you write huge programs. It also lets you do things you can't do easily in Spin, like reload cogs fifty times within a program.
    Frankly, I'd prefer to first learn C and later comprehend C++ as the need arrises.

    That is a good idea. You could start with Catalina, which is C89. Or start with GCC but just use C syntax, as C is a subset of C++.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-08-17 21:56
    Heater. wrote: »
    Good grief,
    I believe it is possible to understand C and most of the libraries that come with it in a week or month or two.
    However I do not believe there is anyone alive who understands the full impact of C++ and all the libs that come with it.

    These observations are at the core of what I wanted to know. It is because C is easy to comprehend that I see it as the first step towards C++.

    I have tried Java, Python, and other OOP languages - but they certainly are vast and seemingly abstracted from the machine level of micro-controllers. The terminology becomes somewhat confusing and overwhelming. Examples begin to have the look and feel of a text on chess openings - useful for experts, but not the whole game for the beginner. I don't have a lot of comfort with all that abstraction, though I do understand that this approach allows the rather vast programs we are using today.

    The main point here is HOW the new learner is going to come to terms with SPIN or GCC on the Propeller. I am very happy to hear that C as a separate compiler so one does not have to tweak C++ in mysterious ways to have it provide a 'virtual C compiler' of some sort.

    I am in no rush to use the SIMPLE IDE or C on the Propeller as I have plenty to do and learn. I just wanted to be assured that the C/C++ really meant that C was a valid alternative entry point and not just a subset of C++ in this context.

    thanks all. Hopefully the SIMPLE IDE will live up to and excell beyond its name. I've looks at a lot of other IDEs that are so complex that they exceed the languages they support in documentation and demands for study.
  • RossHRossH Posts: 5,512
    edited 2012-08-19 17:45
    These observations are at the core of what I wanted to know. It is because C is easy to comprehend that I see it as the first step towards C++.

    Hi Loopy,

    I'd probably recommend you plan to move from C to Objective-C instead of moving from C to C++. Although I would also contend that in the embedded space (which is where we are) there is a real need for moving from C to anything at all :lol:.

    The TIOBE programming index tells an interesting story about these (and other) languages:

    tpci_trends.png

    The TIOBE index shows that the popularity of both C++ and Java has been in fairly consistent decline over the last 10 years. The index shows C as overall stable or even on the increase over the last few years. It also shows C# and Objective-C as the new "up and comers" - but keep in mind that this is a general "across the board" index, and some of these are very platform- or application-specific languages - i.e. one is popular only on OSX or the iPhone, and the other is popular only on Windows.

    If you could see the same type of graph restricted to the embedded space (I don't know of an equivalent - if anyone does, please post it), then I think Java, C++ and C# would all drop off even more dramatically - there was a flurry of adoption when these languages were fairly new, but people have realized over time they are inappropriate for low-level embedded work. But both C and Objective-C would show even bigger increases - C because it remains the only "universal" low-level language (i.e. it is supported on all platforms, and is reasonably efficient on all of them - even on tiny micros with a few kilobytes and a few kilohertz) and Objective-C because of the current popularity of the iPhone (where you have hundreds of megabytes of space available and hundreds of megahertz of processing power).

    Someone said elsewhere on this site that we may soon see only two general-purpose processor architectures - Intel and ARM. I suppose it is possible we may soon only see two general-purpose languages - and this graph shows there would be a good case for saying this might end up being C and Objective-C.

    Ross.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-08-20 05:48
    Frankly, I plan to continue with PBasic, Spin, and Assembly language for the BasicStamps, SX chips and the Propeller. SX/B is something that seems to distract me more than to help (it certainly is NOT equivalent to PBasic, nor as flexible as SX Assembly).

    C is the latest addition as I can now see how close to Assembly language it is and why it is abstracted in the way it is. Beyond C, I am not sure I'll ever get into C++ or Objective C or whatever. It has taken a few years to sort out what the role of various compilers are and why so. With relatively small chips, knowing you can exploit Assembly is a much higher value than with desktop systems in Linux, Windows, or Apple.

    With microcontrollers, there are quite a few advantages to get down to Assembly language as one in generally not trying to drive large data processing projects or sophisticated graphic or interface a wide variety of tasks over the internet. But one does often need to squeeze utility out of every register and every pin.

    I've tried to learn Java, but the documentation is overwhelming and while every author claims OOP is easy - their jargon soon obscures the presentation. Similarly, I have read Python in the depth and though that seems more appealing to me than Java, it too gets off into regions that presume one knows why they need and what they want to do with tuples, and such.

    I am sure there are people that will greatly enjoy C++ on the Propeller as they have already mastered it and have higher ambitions. They might like Objective C as well. I am just not at that point and dislike reading and rereading huge tomes about any given computer language.

    So it seems that there is a new Objective C. I'll keep it in mind, but at this juncture I am intent on ANSI C - either Catalina or Propeller GCC. I have no great ambition of bolster a resume or set the programing world ablaze with glory. I'd just like to feel that I have a reasonable working knowledge.

    I am happy Simple IDE is going to allow me to remain in a Linux environment as these days I rarely go to Windows of any sort unless I want to program a BasicStamp or and SX chip. Visual Basic is something I try very hard to ignore.

    I suppose that one could divide the programing world into 'macro-programing' and 'micro-programing'. The macro-programing requires big languages with big tool sets for ambitious projects, whilst the micro-program requires fitting tight constraints of spaces, resources, and timing. I am more interesting in the latter.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-08-20 05:59
    Objective C is catching on because of iOS success in the handheld arena. As Android takes over the handheld market I expect interest in it to decline. It is definitely not of interest to anyone working outside of the iOS area.

    As an aside, I'm not happy about there being three proprietary successors to C++ (Java, C#, and Objective C). They lock apps tightly to their respective platform and are bad for software vendors and consumers. Great for platform vendors which is why they are doing it.
  • Heater.Heater. Posts: 21,230
    edited 2012-08-20 06:11
    RossH,
    ...in the embedded space ... there is a real need for moving from C to anything at all

    Ha! Might I suggest we get back to ADA? A language designed for exactly that. There is ADA for little AVR's so why not?

    The TIOBE index is interesting. Java is shown to be in decline Objective C has been ramping up since 2009. I might speculate the rise in Objective C is due to the widespread up take of iPhones and such. BUT then Android has also been a hugely growing market so where is the corresponding ramp up in Java? Well, thing is they never call Java on Android "Java". Perhaps all the job posting and such analysed by TIOBE are just saying "Android developer".
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-08-20 06:15
    My own impression is that C++ is still valid if I want to read Linux code. But nothing beats Assembler for small tasks on small micro-controllers. I am NOT interested in writing Apps for the Android or iPad.

    ADA? Gawd.............. programers always have something they are curious about. I just wanted to make sure that C and C++ were not clumped together on the Propeller's compiliers as references to C/C++ and GCC are confusing.
  • Heater.Heater. Posts: 21,230
    edited 2012-08-20 06:16
    There are at least 4 successors to C++, don't forget JavaScript (Which is nothing like Java). It follows a somewhat different path when it comes to things like objects and classes but supports them none the less. It must be one of the most widely used and longest lived languages ever what with being in every web browser and used on most web pages. And now usable for server use and general purpose tool creation, like Python or Perl, with node.js.
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-08-20 06:26
    Forth...non-proprietary, multi-platform, scales from micro to mainframe...portable, extensible...just sayin' :cool:
  • ninogenioninogenio Posts: 7
    edited 2012-08-20 06:48
    i cannot really add much more than what has already been said. a huge diffrence between c and cpp is garbage collection i personally made the switch because i needed a way of keeping track of my resources easyer. during the transition i found i sometimes had a few leaks but once you get your head around it you wont look back.

    also if you ever built something like a software finit state machine every thing can be broken up into litte objects or states and fed through. things like this i cannot imagin doing in a non oop language.
  • GadgetmanGadgetman Posts: 2,436
    edited 2012-08-20 07:04
    ADA? Gawd.............. programers always have something they are curious about. I just wanted to make sure that C and C++ were not clumped together on the Propeller's compiliers as references to C/C++ and GCC are confusing.

    Nothing wrong with ADA...
    Handles multitasking, pretty well platform-independent(you ned to recompile, but unless you've been a naughty boy, that should be painless), handles garbage collection, and allows you to create such useful functions as '>', '+', '-' and so on for any custom datatype you can imagine...
    Used ADA on VAX-VMS in 90 - 92(School computer) and in the evening used Janus ADA on my PC.
    Beause VAX ADA didn't have a 'long integer'(The Integer type was 32bit instead of 16) I had to add a type definition to create a Long Integer, and REM it out when I worked at home.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-08-20 07:16
    Heater. wrote: »
    The TIOBE index is interesting. Java is shown to be in decline Objective C has been ramping up since 2009.

    Java was supposed to be a portable systems programming language, but the only major area where it caught on was server side web application development. Around the mid-2000's people figured out how to run other languages (e.g. Groovy, Jython, and JRuby) on the JVM. Web development is essentially a text processing problem and those languages are better suited to the task than Java, so interest in the web development community has been shifting to them and away from Java.

    Android's version of Java is nearly a complete redo with a register based VM and different API's. Oracle sued them and lost because Google was fairly aware of the potential for getting sued after the Sun Microsoft debacle.
    Heater. wrote: »
    There are at least 4 successors to C++, don't forget JavaScript (Which is nothing like Java).

    True enough, and if the HTML5 people have their way it might actually be useful for application development. It's also the only one of the bunch that is portable and standardized.
  • Heater.Heater. Posts: 21,230
    edited 2012-08-20 07:35
    ninogenio,

    Not sure what you mean. C++ has no automatic garbage collection built in. Except for objects created on the stack I guess.
  • Heater.Heater. Posts: 21,230
    edited 2012-08-20 07:44
    Martin_H,
    Java was supposed to be a portable systems programming language...

    I always liked this particular piece of marketing non-logic.
    Java is portable provided you implement a Java virtual machine for your target.
    You cannot create that virtual machine in Java.
    You have to use a systems programming language, like C or even ASM, to do so.
    That is to say you cannot do systems programming in Java.

    I guess it might work out if there is a straight Java to native compiler. Is there such a beast?
  • Martin_HMartin_H Posts: 4,051
    edited 2012-08-20 08:04
    Heater. wrote: »
    I always liked this particular piece of marketing non-logic.
    ...
    I guess it might work out if there is a straight Java to native compiler. Is there such a beast?

    Initially there were no native compilers, and frankly Sun was never keen on the idea. But several direct to machine language compilers have been created, including one by the GNU project http://gcc.gnu.org/java/
  • Heater.Heater. Posts: 21,230
    edited 2012-08-20 08:15
    So perhaps Java could now actually be used to do systems programing. Like actualy write an OS kernel in it.
    Things like unsigned ints might help. And get rid of the need for all functions to be methods of a class and...and....and oh looks like C again!
  • ninogenioninogenio Posts: 7
    edited 2012-08-20 08:41
    @heater i know cpp has no automatic garbage collection . i meant i like working with new and delete rather than malloc dealloc. i have worked on several projects in c that had so many pointers and dynamic memory allocations that it got too a point where it was no longer possible to debug when and where they started leaking memory from.

    these projects had to be scrapped and rewrote in cpp using lots of small structered classes using new and delete and they are a dream to work with even being away from them for a few years i can go back and easily start modifiying the code.

    here is one example is a small peice of mem release code in cpp from one of my projects.
    ~BackDropClass(){
            if ( InitBGState ){
                if ( InitBGState->TileGrid ){
                    delete [] InitBGState->TileGrid ;
                }
                if ( InitBGState->SnowFlakeGrid ){
                    delete [] InitBGState->SnowFlakeGrid ;
                }
                if ( InitBGState->HouseStack ){
                    delete [] InitBGState->HouseStack ;
                }
                delete InitBGState ;
            }
            delete BGLightTimer ;
    
            std::vector<OglSpriteClass *>::size_type x;
            for (x = 0; x < TileSprites.size(); x++){
                delete TileSprites[x];
            }
            TileSprites.clear();
    
            for (x = 0; x < SnowFlakeSprites.size(); x++){
                delete SnowFlakeSprites[x];
            }
            SnowFlakeSprites.clear();
    
            for (x = 0; x < TreeSprites.size(); x++){
                delete TreeSprites[x];
            }
            TreeSprites.clear();
    
            for (x = 0; x < HouseSprites.size(); x++){
                delete HouseSprites[x];
            }
            HouseSprites.clear();
    
            for (x = 0; x < ChrimboLights.size(); x++){
                delete ChrimboLights[x];
            }
            ChrimboLights.clear();
    
        }
    

    now that small peice of code free's a few classes and thousands and thousands of allocated objects from they classes. to do this with malloc dealloc its just a plain nightmare. thats sort of what i ment.
  • 4x5n4x5n Posts: 745
    edited 2012-08-20 17:49
    My own impression is that C++ is still valid if I want to read Linux code. But nothing beats Assembler for small tasks on small micro-controllers. I am NOT interested in writing Apps for the Android or iPad.

    ADA? Gawd.............. programers always have something they are curious about. I just wanted to make sure that C and C++ were not clumped together on the Propeller's compiliers as references to C/C++ and GCC are confusing.

    I spent a lot of time programming in C and only a bit of time with C++ and OO programming. The time I spent with C was a long time ago however and I've forgotten more about both C and C++ than I remember.

    I think it's important to remember that C was originally developed to be used for embedded programming and is by design a very low level language. I'd like to think that C++ was also designed for embedded work but it's hard to tell for sure since the libraries are so closely tied to the language and all the books I've seen on C++ are geared towards application type programming. One thing I do remember about C++ is that it hides a lot of the "ick" of C.

    For what it's worth if it's worth anything I've been looking into "relearning C/C++ for the propeller and have decided on C++. The object oriented nature of C++ seems to be well suited for use on the propeller and I like the idea of being able to use constructors and destructors to start and stop cogs.

    The reason for the tight coupling of C and C++ is that C++ is a superset of C and ansi compliant C code is valid and legal C++ code. Like spin C++ is an object oriented language. How many of the advanced "features" of C++ you use is entirely up to you and if you decide to learn C first do so with the knowledge that you're also learning C++ without the classes!
  • 4x5n4x5n Posts: 745
    edited 2012-08-20 18:10
    Martin_H wrote: »
    Java was supposed to be a portable systems programming language, but the only major area where it caught on was server side web application development. Around the mid-2000's people figured out how to run other languages (e.g. Groovy, Jython, and JRuby) on the JVM. Web development is essentially a text processing problem and those languages are better suited to the task than Java, so interest in the web development community has been shifting to them and away from Java.

    Android's version of Java is nearly a complete redo with a register based VM and different API's. Oracle sued them and lost because Google was fairly aware of the potential for getting sued after the Sun Microsoft debacle.



    True enough, and if the HTML5 people have their way it might actually be useful for application development. It's also the only one of the bunch that is portable and standardized.

    In my never humble opinion the reason why Java became popular for web sites is that it's platform agnostic. The same code can run on a client running windows, mac-os, linux, etc! Had nothing to do with it's "quality" as a language.
  • Mike GMike G Posts: 2,702
    edited 2012-08-20 18:55
    I think it's important to remember that C was originally developed to be used for embedded programming and is by design a very low level language.
    I did not know that.

    I always thought of C++ as a natural progression to C. To me, C is procedural and method based. While C++ has more of a data perspective. That is, if the programmer wishes.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-08-20 23:22
    From what I can garner, C remains a useful language to learn. That is mainly what I am concerned with at this point.

    Java is a tokenized language and as pointed out, it tends to be used on top of an OS.

    I am aware that the OOP and garabage collection and so on are worthwhile features. But one must crawl before they walk and walk before they run. At times, it seems that it is getting harder and harder to find a good path for learning through all the programing languages that have come and gone.

    Taiwanese are very much hanging on to Visual Basic - not because it is the best solution, but because it appeals to their comfort zone. Americans and Europeans have a richer history of computer languages to ponder and exploit. This all has to do with different degress of access across language barriers.

    But there is a rather large corpus of examples of code in C. So if one doesn't understand C, one often just cannot read about programming with any real depth of comprehension. Even 'pseudo-code' is very much like C.
  • Mike GMike G Posts: 2,702
    edited 2012-08-21 06:56
    I am aware that the OOP and garbage collection and so on are worthwhile features.

    IMO, garbage collection is nice and a marketing ploy. Garbage collection is useful but does not stop memory leaks. Crappy code is not language specific.

    On the other hand, OOP is a good thing to start learning now. The concepts are relatively easy to understand but it takes a lot of practice to implement effectively.
Sign In or Register to comment.