Should I learn C before C++?

Hello All, I am a new one in this forum and I just completed my b.tech from computer science, I want to learn to programming. So Should I learn C before C++? or directly move to C++. I want to learn C++ programming.

Comments

  • I can't answer the question directly but you will end up knowing both in the end. There is too much straight C in circulation to not have to deal with it once you've gone down this path. And C++ is in some ways just a superset of higher-level features on top of C.

    Word of warning, and I'm sure you've heard it before: Because of the no-hand-holding nature they're probably the hardest languages to fully handle. There is no fast track learning it. Bugs are often crash inducing without notice. C/C++ is why debuggers exist.
    "We suspect that ALMA will allow us to observe this rare form of CO in many other discs.
    By doing that, we can more accurately measure their mass, and determine whether
    scientists have systematically been underestimating how much matter they contain."
  • ceptimusceptimus Posts: 108
    edited 2019-06-03 - 10:35:58
    C++ is mainly just C with classes.

    You have to know C anyway, in order to write classes in C++. A beginner in C++ will have to learn all the basics of C before tackling classes.

    The main difference for a beginner is using the C++ overloaded << operator to format output rather than C's printf(). A C++ course will teach you to use <<, but you can still use printf() if you wish: many C++ coders prefer it.
  • No need to learn C first. Every C++ tutorial will walk you through
    #include <iostream>
    
    int main () {
      std::cout << "Hello, world" << std::endl;
      return 0;
    }
    

    Which is almost entirely C anyway. The C equivalent would be
    #include <stdio.h>
    
    int main () {
      printf("Hello, world\n");
      return 0;
    }
    

    You'll be fine jumping directly into C++.
    David
    PropWare: C++ HAL (Hardware Abstraction Layer) for PropGCC; Robust build system using CMake; Integrated Simple Library, libpropeller, and libPropelleruino (Arduino port); Instructions for Eclipse and JetBrain's CLion; Example projects; Doxygen documentation
    CI Server: https://ci.zemon.name?guest=1
  • I'd actually suggest learning C first, because it's more generally useful. Almost all C programs works in C++, but the reverse is not true. Also, there's not as much to learn -- C is basically a strict subset of C++, so it's a simpler language.

    Once you have learned C though it's definitely worth-while going on to learn the rest of C++.
  • Learn C first.

    Learn C++, which is a superset of C anyway, when someone puts a gun to your head and makes you.
  • David BetzDavid Betz Posts: 13,480
    edited 2019-06-05 - 01:27:55
    localroger wrote: »
    Learn C first.

    Learn C++, which is a superset of C anyway, when someone puts a gun to your head and makes you.
    Someone put a gun to my head and now I'm using C++ and I'm not finding it so bad after all. One thing that surprised me is when I saw a declaration like this:
    auto x = 4; 
    auto ptr = &x;
    
    I had to look this up to see what it meant. It seems that "auto" declares a variable whose type is compatible with the type of the initializer. I guess that's kind of cool.
    This is the same as:
    int x = 4;
    int *ptr = &x;
    
    Of course, it's usually used with much more complex initializers. The case I saw was an iterator.
  • DavidZemon wrote: »
    No need to learn C first. Every C++ tutorial will walk you through
    #include <iostream>
    
    int main () {
      std::cout << "Hello, world" << std::endl;
      return 0;
    }
    

    Which is almost entirely C anyway. The C equivalent would be
    #include <stdio.h>
    
    int main () {
      printf("Hello, world\n");
      return 0;
    }
    

    You'll be fine jumping directly into C++.

    Never seen C++ before. From this C++ is so much better???
    My Prop boards: P8XBlade2, RamBlade, CpuBlade, TriBlade
    Prop OS (also see Sphinx, PropDos, PropCmd, Spinix)
    Website: www.clusos.com
    Prop Tools (Index) , Emulators (Index) , ZiCog (Z80)
  • localroger wrote: »
    C++, which is a superset of C anyway,

    Not 100% true. For example, const works different in C++ and C has some strange features C++ doesn't have. Nothing too major though.

  • Cluso99 wrote: »
    Never seen C++ before. From this C++ is so much better???

    No, this example isn't what makes C++ better (though the << operator is much more space efficient than printf, and why I use it in PropWare). I was just drawing attention to that fact that, as you walk through a C++ tutorial (or a formal class), you will learn the basics of C too. You will learn about functions and pointers and structs. You will surely learn about some of the C standard library too, like printf, just by using Google when you have a question. Most of the C standard library you would likely never learned if you focused 100% on C++, and that would be sad, but exactly the same thing can be said about not learning the C++ standard (template) library.

    If your end goal is to learn C++ (I'm going to assume that is a valid end goal for the moment, because debating that C vs C++ requires knowledge which we don't yet have about the application or intended applications), then I think focusing on C tutorials is like learning Latin before Spanish. Will it help? SURE! Is it necessary? Not remotely.
    David
    PropWare: C++ HAL (Hardware Abstraction Layer) for PropGCC; Robust build system using CMake; Integrated Simple Library, libpropeller, and libPropelleruino (Arduino port); Instructions for Eclipse and JetBrain's CLion; Example projects; Doxygen documentation
    CI Server: https://ci.zemon.name?guest=1
  • I found that the real difference in developing C versus C++ code shows up when you have to think in terms of object oriented programming (OOP) in C++. You can easily write non-object oriented code in C++ and the difference from C will be minor. The move from procedural programming in C to OOP in C++ requires a significant change in thinking (at least it did for me).

    So, I would say learning C first is probably an easier path to go... For small projects, it's like high-level assembly code, in some ways. It stays very close to typical instruction code (well, on the Propeller, I'm stretching that thinking a bit).

    Object oriented programming (in C++ and other languages) adds a higher level of thinking about any particular project. You have to decide how your project can be reflected in objects and procedures that make up the code. Lots of up-front decisions, some trial and error as you become more familiar with the concept, but eventually you get it!

    When you have a new project in mind, a new processor and languages to learn you might want to start with the most simple environment. C is probably a good start (outside of Spin, on the Propeller!).

    Just my observations...

    dgately
    Livermore, CA (50 miles SE of San Francisco)
  • dgately wrote: »
    I found that the real difference in developing C versus C++ code shows up when you have to think in terms of object oriented programming (OOP) in C++. You can easily write non-object oriented code in C++ and the difference from C will be minor. The move from procedural programming in C to OOP in C++ requires a significant change in thinking (at least it did for me).

    So, I would say learning C first is probably an easier path to go... For small projects, it's like high-level assembly code, in some ways. It stays very close to typical instruction code (well, on the Propeller, I'm stretching that thinking a bit).

    Object oriented programming (in C++ and other languages) adds a higher level of thinking about any particular project. You have to decide how your project can be reflected in objects and procedures that make up the code. Lots of up-front decisions, some trial and error as you become more familiar with the concept, but eventually you get it!

    When you have a new project in mind, a new processor and languages to learn you might want to start with the most simple environment. C is probably a good start (outside of Spin, on the Propeller!).

    Just my observations...

    dgately

    To play devil's advocate...

    I'd argue one should be writing object-inspired code in C, too. Create a "module" for relate code and a struct to hold all of the state for that module. Make it a consistent pattern to always pass an instance of that struct - as a pointer - as the first parameter of every function in the module. Provide a function for initializing and cleaning up instances of the struct. And what do we have now??? A class in C
    David
    PropWare: C++ HAL (Hardware Abstraction Layer) for PropGCC; Robust build system using CMake; Integrated Simple Library, libpropeller, and libPropelleruino (Arduino port); Instructions for Eclipse and JetBrain's CLion; Example projects; Doxygen documentation
    CI Server: https://ci.zemon.name?guest=1
  • DavidZemon wrote: »

    To play devil's advocate...

    I'd argue one should be writing object-inspired code in C, too. Create a "module" for relate code and a struct to hold all of the state for that module. Make it a consistent pattern to always pass an instance of that struct - as a pointer - as the first parameter of every function in the module. Provide a function for initializing and cleaning up instances of the struct. And what do we have now??? A class in C

    I didn't say you can't write OOP code in C :wink: C++ does intentionally provide infrastructure for OOP, while in C, it is up to the programmer to create the OOP realm from more primitive means... But, I agree that one can gain from an object-inspired style of writing code in any language!


    dgately


    Livermore, CA (50 miles SE of San Francisco)
  • tonyp12tonyp12 Posts: 1,949
    edited 2019-06-05 - 20:43:37
    C beginners start with single task that do hard-waits, not only waste battery and a risk of polling forever and locking up code.
    With more things to do they move on to a super-loop that for example use 10ms greatest common denominator.

    I would say the pinnacle is a 32task Co-operative state machine Functions, you just need to add new from template (below) if you need another task and add its name in common.h,
    You don't have to add anything in main.c that runs the 1uAh rtc task scheduler, any task can sleep for any random length between 1/1024 sec to 4hrs.
    All tasks boot-state is run at... boot (eventset = 0xffff is one time set in main) so you can set pins and IRQs, or just leave empty and the break there.

    Would like to add a custom pre-compiler so it can do shared IRQ vector merger so each task just have its isr snippet, but maybe could be done without.
    #include "common.h"
    
    /*   PUT YOUR TASK NAME HERE and also do that in common.h */
    #define this HERE
    
    /*   TASK ENTRY   */
    void TASKFUNX (void) {
    if (task[this].stateset){task[this].statedo = task[this].stateset; task[this].stateset = 0;}
    switch (task[this].statedo){
    
    case BOOT:
    	 break;
    
    case INIT:
             StateNowNext(this)
    	 break;
    
    case START:
             OS_Interval(this,512);
    	 break;
    
    case EXIT:
    default:
      break;
    }
    }
    
  • The primary difference between C and C++ is the standard libraries.

    You can write perfectly fine C++ code using just the C runtime libraries, and never touch the C++ runtime libs or STL. Most of the negatives people associate with C++ are in the libs/STL. If you turn off RTTI, which most do because of the bloat and security issues, then C++ becomes a fairly nice language to code in. The leanness of C with some niceties to make coding more rapid/efficient.
  • Thanks to all your support! One last suggestion I want, I have found this site C Tutorial I found here is a list of different site tutorials and its provide by the programming community. So is it good to start to learn from here?
  • I looked at the two top results on that site... I don't like the site. Not to say the results are not valid, not the comments are just plain weird and off topic and the courses do not have explanations under them explaining what they are or how they're different/better than alternatives.

    That being said, I've heard udemy is very good (never taken a course there myself) and it's the second highest rated suggestion.... Maybe go for it?
    David
    PropWare: C++ HAL (Hardware Abstraction Layer) for PropGCC; Robust build system using CMake; Integrated Simple Library, libpropeller, and libPropelleruino (Arduino port); Instructions for Eclipse and JetBrain's CLion; Example projects; Doxygen documentation
    CI Server: https://ci.zemon.name?guest=1
Sign In or Register to comment.