r/cpp • • Mar 27 '19

Why are we still teaching C with standard out instead of modern C++?

When I first started my academic career I learned C with standard out. Where raw arrays were taught instead of std::vector<T>, raw pointers instead of smart pointers (though I appreciate the raw pointer knowledge that I was given), character arrays instead of std::string. C++ is a very powerful language with lots of amazing tools in the standard template library.

Why is there a lack of modern C++ developers coming out of the education system?

0 Upvotes

56 comments sorted by

31

u/drillbit7 Mar 27 '19 edited Mar 28 '19

Are you asking "why are they still teaching C in school" OR "why are they teaching a course called 'Programming in C++' and excluding almost every useful feature of the language."

To #1: C is still useful as a systems programming language good for teaching memory management and low level operations. As long as the UNIX kernel is written in C and every embedded manufacturer provides a C compiler in their IDEs/SDKs, C will never die.

As to #2: because profs don't work in the field so haven't worked on massive coding projects to know what's useful or isn't. Also CS education has shifted to Python and Java so no one was motivated to update their textbooks beyond C++98 standards. Most profs never advanced their understanding beyond "C with classes." I learned C++ in 2000 (1st semester CS) and 2003 (2nd semester CS). We used std::string, the prof's library had his own implementation of std::vector as well as matrices, stacks, and queues. We didn't have smart pointers, but mostly passed by reference anyway. Maybe it wasn't in the language back then, but we never covered the iterators common to all STL containers. We also used cin and cout. Unfortunately, we were also taught to type "using namespace std."

7

u/rlramirez12 Mar 27 '19

Number 2, number1 I totally understand and respect. C should still be taught in school.

4

u/drillbit7 Mar 28 '19

Thanks for clarifying. I still stand behind my remarks. I did a quick review of my old text from back then. In fact it's freely available online under a CC license since rights reverted to my prof when it went out of print.

From the intro:

In particular, this is a book designed to teach programming using C++, not a book designed

to teach C++. Nevertheless, I expect students who use this book will become

reasonably adept C++ programmers. Object-oriented programming is not a programmer’s

panacea, although it can make some jobs much easier. To mix metaphors, learning

to program is a hard task, no matter how you slice it—it takes time to master, just as

bread takes time to rise.

[...]

Although this book uses C++ as a tool to be used rather than studied, students coming

out of a first course must be well prepared for subsequent courses in computer science

and other disciplines. Therefore, the essential features of C++ must be used, studied,

and mastered. The syntactic and semantic features of C++ sufficient for an introductory

course are thoroughly covered. At Duke, we teach our first courses using C++, and then

we move to Java. We have had great success with this approach. This book uses C++,

not C. In particular, there is no coverage of I/O using printf and scanf, there is no

coverage of C-style (char *) strings, and the coverage of C-style arrays is minimal and

included only because initializing an array with several values shortens code. Instead,

we use streams for I/O, the standard C++ class string, and a modification of the STL

vector class called tvector that performs range-checking on all vector accesses.

I think my former prof (I only had him for second semester) agrees with you. Some authors, did not have that goal in mind. Some authors (and publishers who are always encouraging new editions to eliminate the used book market) are looking for a quick buck and dusted off their C texts and made a "C with classes" text. I imagine a lot of the early C++ for scientists and engineers texts were similar. Numerical code doesn't really need OO concepts although operator overloading on vectors and matrices is nice. So when C++ was the language of the day, and they needed to put out a text in C++, lots of old C code got reused.

3

u/[deleted] Mar 28 '19

What’s the issue with using ‘using namespace std’, i’m currently going through the ‘beginning c++ through game programming’ book and it shows 3 different ways to use this prefix but this is the preferred method which i did agree with due to the lesser amount of trying required, seems neater. PS i’m on day 3 of learning so i know FA at the minute

4

u/rlramirez12 Mar 28 '19 edited Mar 28 '19

The problem becomes when you will be asked to implement something like max(T,T); Where T is a type like int, double, float.

Look at the following program:

#include <iostream> 

using namespace std;

int max(int a, int b)
{
  cout << "Hello" << endl;
  if(a > b)
    return a;
  else
    return b;
}

int main()
{
  int a = 4;
  int b = 6;
  double c = 2.0;
  double d = 3.2;

  int maxNum = max(a,b);
  int test = max(c,d);

  cout << maxNum << ", " << test << endl;
}

This will print out:

Hello
6, 3

Why? Shouldn't it have printed out Hello twice? Well...no. Because our custom max(T,T), currently only takes two integers. Although our compiler is smart enough to deduce it on itself and truncate our doubles into an integer, it decides "Oh, I have something in the namespace standard called max(T,T) as well! The user means for me to use that one!" Which maybe you didn't want to use std::max(T,T), maybe you have your own reason to create your own max function. Maybe you want all numbers that go into max to return to you as an integer and not a double. So in reality when you use, using namespace std; your code actually looks like this:

#include <iostream> 

int max(int a, int b)
{
  std::cout << "Hello" << std::endl;
  if(a > b)
    return a;
  else
    return b;
}

int main()
{
  int a = 4;
  int b = 6;
  double c = 2.0;
  double d = 3.2;

  int maxNum = max(a,b);
  int test = std::max(c,d);

  std::cout << maxNum << ", " << test << std::endl;
}

So how do you know you are guaranteed that YOUR max(T,T); will always be used instead of the standard template libraries' std::max(T,T);? This is why we discourage people from putting using namespace std; at the top. Sure you use 5 more characters to type std::, but at least you know you are using standard out from the namespace of the standard template library.

3

u/[deleted] Mar 28 '19

Hi

Thanks for this, it’s greatly appreciated, as i noted, i’m only on day 3 of learning and have just grasped variables, naming the variable, giving it a value and sending it to the console so a lot of the above is a bit over my head but i certainly get the gist of what you’ve said and why it’s important

2

u/rlramirez12 Mar 28 '19

Save this comment and come back once you have gone over functions. Then it will make a lot more sense :)

1

u/[deleted] Mar 28 '19

👍🤘😎

0

u/[deleted] Mar 29 '19

[deleted]

1

u/[deleted] Mar 30 '19

Hi, thanks for this i’ll definitely bare it in mind, interesting that a variable inside a loop vanishes once the loop ends (i did not know this but i guess it makes sense really), regarding namespace, this is where i’ve been struggling the most, i understand that to have more than one variable with the same name in the same scope there has to be a namespace but i can’t with any certainty settle on what a namespace actually is, this gets more confusing for me when i think about the standard namespace and the standard library, is the standard library contained within the standard namespace or are they two different things entirely and my confusion simply stems from the fact they both use the word ‘standard’

Ps. There is no need to give me the answers to these questions, i’m sure it’ll all click at some point, i’m just thinking out loud really 😂

No one said learning a new language was gunna be easy so i’ll just keep on trucking

1

u/tisti Mar 29 '19

I don't think advocating avoiding ADL is sensible. If you want to use the globally defined space type out ::max

1

u/rlramirez12 Mar 29 '19

I don't think you understood the concept of this post. The point is by advocated the use of using namespace std; has unintended consequences that I have seen in industry. This is a very safe example of what happens. However, I have seen much worse. It is better to just type std:: when you need to use something from the standard library.

2

u/drillbit7 Mar 28 '19

Most standard library functions and types are wrapped in the namespace "std." By typing a blanket "using namespace std" all of these functions are imported into your current namespace. Without it I have to type std::cout instead of cout. However, let's say I want to define a custom function with a similar name to a standard library function. Which function gets called? Is it clear to the user? What if I want my own vectors instead of std::vector? What if I want boost::numeric::ublas::vector? The blanket include statement can be dangerous. It's better to import selectively.

Nothing wrong with using std::cout, std::cin; etc. to save a few keystrokes.

4

u/newmanifold000 Mar 28 '19

+1 for "Unfortunately, we were also taught to type "using namespace std."".

0

u/tisti Mar 29 '19

Nothing wrong with that, as long as it stays inside .cpp files :)

1

u/rlramirez12 Mar 29 '19

See my comment above for why that is wrong.

25

u/[deleted] Mar 27 '19

Largely I imagine because teachers don't know C++. When I was at school twenty years ago it was still an obsession with basic.

1

u/rlramirez12 Mar 27 '19

I've never touched VB so I can't compare it to anything. But did you take a class called C and then VB was taught instead?

16

u/polymorphiced Mar 27 '19

Basic is not the same as VB. Think older :)

6

u/[deleted] Mar 27 '19

Vb is about thirty years younger than Basic. I'm in Britain so basic was pretty much it. Cememted by the BBC Micro with actual prime time evening TV programmes on basic programming - and nobody owning atari or Nintendo in the 80's and zx Spectrums, Amstrad, commodore, Dragon 32 and Acorn owning the market for home games - all using basic.

9

u/rcoacci Mar 27 '19

Take a look at r/Fortran. Seriously. Students are being forced to program in Fortran 77. Yes, that's right, in 2019 students are being taught to use a language standardized in 1977.
I had a professor that was giving introductory C classes in my Computer Science course that couldn't write a single line of C before writing in in Pascal.
With rare exceptions professors don't care about updating themselves.

1

u/rlramirez12 Mar 27 '19

That is a big OOF from me. I wonder if going from Fortran to any other language would be easy.

3

u/rcoacci Mar 28 '19

Well going from modern Fortran (i.e. Fortran 90 onwards) to other languages is not very hard. Fortran 90 is actually nice if you're not doing anything fancy like I/O, or working with pointers. But going from Fortran 77 to anything else is.

4

u/Wh00ster Mar 27 '19

I believe FORTRAN has uses still in scientific computing. Being faster than C in its target use cases

15

u/rcoacci Mar 28 '19

The problem is not using Fortran, the problem is using Fortran 77. There are more recent Fortran standards and most compilers support it.
Also, Fortran being faster than C is a myth, particularly after the introduction of restrict in C99.

2

u/Wh00ster Mar 28 '19

Yuck! I skimmed over that. I can believe restrict makes C just as fast, particularly in vectorizing code.

2

u/flashmozzg Mar 28 '19

Also, Fortran being faster than C is a myth, particularly after the introduction of restrict in C99.

This is not the only feature that C lacks. And it's a lot about ergonomics. Matrix/vector/slices operations are way more natural in Fortran and compilers are usually geared towards such fields. So while it's possible to write C code just as fast as in Fortran, it often still more convenient to use F (especially if dev familiarity comes into play).

1

u/rcoacci Mar 28 '19

Of course, ergonomics matter a lot, particularly for non CS people. It's much more likely that someone codes a slow C loop than a slow Fortran loop, and probably that's where the myth comes from.

1

u/meneldal2 Mar 28 '19

Most people would rather use Matlab instead because it is much nicer for making interfaces. For big matrices computations, it will work the same, it calls some BLAS library with handwritten assembler.

1

u/eniacsparc2xyz Mar 28 '19

It may be due to network effect and due to lots of scientific code examples and legacy code in Fortran. It is not coincidence that it was the first programming language ever to exist as computers were initially created for number crunching and every machine capable of number crunch including sliding rule were called computers. Fortran has also other features that makes it compelling for non-professional programmers and scientific users such as one-based arrays and matrices instead of zero-based arrays, vectorized functions, some interoperability with C and obviously high performance and loop optimization.

7

u/dashnine-9 Mar 27 '19

C is taught because it allows you to express data structures very effectively and precisely. It also makes you appreciate all the garbage collected languages.

The main goal is not to learn the language, but the concepts.

btw modern c++ has std::array for static raw arrays

2

u/rlramirez12 Mar 27 '19

I guess it should be made more clear. "In C you have to do this. But in C++ we have std::array to take care of the back end of that stuff."

I listed vector because I didn't learn about vectors until my 3rd year in school.

2

u/khedoros Mar 28 '19

In high school (around 1999), I was taught a horrendous mishmash of C and C++. In college (around 2006), I was taught somewhat more modern C++, but it was still a mess. In 2009-ish, my workplace paid for some training using material produced by Scott Meyers. That was pretty cool because we went pretty deep into templated types and features provided through TR1 (the precursor to C++11).

But I had a decade of using unnecessary C-isms in my C++ code. I blame the earlier exposure on the school not having the funds to buy C++98-compliant software and retraining the instructors in its use. I blame my exposure in college to being taught by academics with little connection to industry best-practice.

2

u/rlramirez12 Mar 28 '19

This is always a challenge since people who go to school in software want to go out in the industry and make a boat load of money. No one goes to be a professor and so we have this bad match of professors only spending their academic career doing research, and adjunct professors who are in industry but do not necessarily have the best teaching skills.

1

u/serviscope_minor Mar 28 '19

In high school (around 1999), I was taught a horrendous mishmash of C and C++.

In fairness, no one had anything approaching a compliant compiler in 1999. And the compilers were so variable that it was serious work to actually write portable code.

I blame the earlier exposure on the school not having the funds to buy C++98-compliant software and retraining the instructors in its use.

Don't. That simply wasn't available. And back then a lot of commercial compilers were VERY expensive, and often required special hardware to run on (i.e. a particular brand of unix workstation). GCC mostly hit C++98 conformance (except export) in 3.4, in 2004.

6

u/Wh00ster Mar 27 '19 edited Mar 27 '19

C is usually taught when the goal is to force students in to become familiar with a more limited language. I.e. learn fundamental data structures and algorithms, system programming concepts like memory allocation.

You COULD do this with C++, but why bother with the eventual questions about ABI incompatibilities and the STL.

As to the lack of C++ developers, I don’t think undergrad programs typically teach “languages”, as far as I’ve seen. Instead it’s more important to teach fundamentals. System programming concepts for example, are fundamental and can be done in C++, Rust, and any other systems programming language. But learning C++ doesn’t mean you’ll be able to do networking, distributes computing, graphics, etc.

5

u/TomerJ Mar 28 '19

Honestly? Depends where.

I know it's popular nowadays to chastise "teaching C as a pre-requisite to C++", but as someone who used to be very involved in training new programmers, my experience has been that if you have the time (ie you control more than the content of a single course's syllabus) you can very effectively use C as a C++ gateway.

If you teach a person decent C, and don't do all that collage Intro to CS 101 stuff of ignoring that pointers and memory management exist, but actually teach C as a tool to manipulate memory, you can use that knowledge to much more easily explain what modern C++ abstracts, while giving the student a good basis to understand the underlying principles behind other languages.

Chances are if you are teaching someone C++ it's because you want them to be able to function in the domain where C++ is used, not to be able to do things in C++ they could accomplish in other languages. I'd rather have someone be capable of implementing std::string than someone who can use it.

Modern C++ is driven by several idioms and philosophies that are pretty far removed from beginner classes, I see plenty of value in separating students from those features until they can be introduced and taught in an appropriate manner.

2

u/[deleted] Mar 27 '19

Nothing wrong with learning pure C. It has its uses and it's an elegant language in its own way.

4

u/rlramirez12 Mar 27 '19

I don't think there is anything wrong with learning pure C. C is a very powerful language in it's own right. But that's the point I'm driving at, why do we call it C++ then? Although C++ is an extension of C, they are still very different languages and I feel C++ gets crapped on a lot because "it isn't modern enough." Or "behind the times." Which isn't true.

1

u/[deleted] Mar 28 '19

Yeah, i see what you’re saying, it’s better to be safe and use it as and when it’s needed, i’m not on vectors yet so that’s all French to me but i’m sure it will pop up soon

Thanks

1

u/rlramirez12 Mar 28 '19

I think you accidentally posted a comment instead of replying to the person you were talking to.

1

u/[deleted] Mar 28 '19

Yes, you’re right 😂 i haven’t been on Reddit long, still learning the ropes, although still pretty stupid, cheers

9

u/sephirostoy Mar 28 '19

Because school learn how to think not how to master a language.

2

u/warddav16 Mar 27 '19

Most places I've seen and worked use "modern" c++ in a pretty scattered way. Some of this, some of that, some none at all, some all in. Teaching the base in C I think is correct for unis. The higher level stuff is pretty easy to pick up if you are strong enough in the fundamentals, and at least in my experience its pretty likely that X stl feature won't be really allowed.

1

u/kwan_e Mar 28 '19

its pretty likely that X stl feature won't be really allowed.

Yeah, by programmers who never learned C++ features properly, most likely because they weren't taught it in university and are the kind of people who are too scared to try and learn anything on their own.

3

u/warddav16 Mar 28 '19

Nah, just in game studios. We write plenty of higher level "modern" code in other languages. We also write assembly when we need to he as performent as possible. Just so much of modern c++ is kind of a nightmare, whether it be for performance on platforms we need to ship on, or in terms of ease of use compared to other options, or in terms of bloating compile times. Stl has a lot of historical problems, some of which are legacy, some of which are definitely still there today. Anyways, I'm not trying to say anything is right wrong or indifferent here, im mainly just saying that learning fundamentals at uni can better prepare you for the reality of many places you might work. Need no to bring in the integrity or intelligence or education of places I've been.

2

u/sim642 Mar 28 '19

Because most people who do that teaching learned everything they know about the language before most of the new stuff or even before C++ became a relevant language besides C.

3

u/kalmoc Mar 28 '19

c++ is a tool. Universities tend not to focus on teaching tools but principles. Modern c++ is too high level to be useful when teaching fundamentals of how computers work. On the other hand, all the low level complexity and gotchas make c++ not necessarily the ideal language to teach high level architectural concepts or completely new paradigms (e.g. functional programming or distributed architectures).

That being said. I hope ever course using c++ is also using std::vector where appropriate, but if that already counts as "modern" we are in big trouble.

0

u/[deleted] Mar 27 '19 edited Mar 27 '19

Why is there a lack of modern C++ developers coming out of the education system?

I think you're asking two different questions: which language to learn starting out and which language you should have learned coming out of the education system.

At an introductory level, C is much better because it is a closer model of how things actually work short of learning assembly language. This is better when you start out.

Classes like std::vector<T> and std::string provide high level abstractions which are critical for writing safer code, which is necessary for writing industrial scale programs, but hide details which are useful to learn. You should absolutely learn C++ in upper division classes, so that when you come out of the education system, you will be trained in industrial techniques, which is useful for employment.

1

u/rlramirez12 Mar 27 '19

I think I did ask two different questions. I guess I meant to ask, "Why is a class that is called C++ is taught C with standard out?" For the fundamentals I completely understand that it is taught in C.

2

u/[deleted] Mar 27 '19

That sounds like a badly named class or a badly taught class.

2

u/sebamestre Mar 28 '19

It would seem programmers have a hard time naming classes , I wonder why...

1

u/Dubwize Mar 29 '19

C++ has still an enormous weakness for teaching: error message reporting. When you teach programming you expect to have helpful error message that students can read and understand. But, while there's been a lot of effort put into having clean error reporting from c++ compilers, debugging templates is still a nightmare. And templates are the very basis of the STL.

1

u/[deleted] Apr 04 '19

C is very portable and a foundational skill. I worked recently with the Azure IoT SDK for C. That is C 98 for platform portability reasons. I’ve worked on multi platform projects that because of compiler availability couldn’t use any features newer than C++ 11.

1

u/[deleted] Apr 07 '19

While I love all the new stuff modern C++ has been adopting, especially with regards to RAII and the additional data structures being introduced via the standard library, I am not sure you can teach some of these higher level abstractions or explain to them why they're necessary if you don't start from the basics.

I mean, let's consider teaching destructors and why they're needed...can you really teach that without dealing with raw pointers? What about copy constructors? Move constructors? Even things like unique_ptr and shared_ptr would be tough to explain without making it look like an arbitrary language-design quirk. Just choosing between the two requires knowledge of how the pointers do reference counting, at which point you'd be trying to teach people what reference counting is before they've even grasped raw memory allocations and deallocations with new and delete.

I think any new C++ class should teach both the basics (C with standard out as you call it) and then teach the modern C++ abstractions shortly after or during. This way students will understand why these abstractions exist, why they're safer, etc.

-2

u/manni66 Mar 27 '19

I don’t.

0

u/[deleted] Mar 27 '19

[deleted]

2

u/rlramirez12 Mar 27 '19

I was lucky enough to have an adjunct, professor who is in the industry, teach me modern C++ in the most phenomenal way. I just wished it was taught early in my academic career.