CppCast - Modern C for Absolute Beginners

Episode Date: January 6, 2022

Rob and Jason are joined by Slobodan Dmitrovic. They first talk about the latest update to the {fmt} library, another Raymond Chen blogpost, and a summary of ISO standardization progress made in 2021.... Then they talk to Slobodan about his new book Modern C for Absolute Beginners. News {fmt} v8.1.0 released You can't copy code with memcpy 2021 C++ Standardization Highlights Links Modern C for Absolute Beginners C++ and Friends Patreon CppCast Patreon

Transcript
Discussion (0)
Starting point is 00:00:00 episode 331 of cpp cast with guest loban dimitrovich recorded january 5th 2022 this episode of cpp cast we're thanking all of our patrons on patreon thank you so much for your ongoing support of the show and if you'd like to support the show too please check us out at patreon.com cpp cast In this episode, we talk about the wrong way to copy code. Then we talk to Slobodan Dimitrovich. Slobodan talks to us about his new book, Modern Sea for Absolute Beginners. Welcome to episode 331 of CppCast, the first podcast for C++ developers by C++ developers. I'm your host, Rob Irving, joined by my co-host, Jason Turner.
Starting point is 00:01:21 Jason, how are you doing today? I'm all right, Rob jason how you doing today i'm all right rob how are you doing doing okay uh you know january 5th first episode of of the new year i still don't have my flying car still no flying car but um you know hopefully this is the year that we finally get over covid yeah sure let's see let's go with that let's go with that uh anything else you want to talk about before we get started here i don't think so oh wait maybe i we did mention uh we did mention c++ on c right i think we mentioned that in last week's episode yeah all right yeah nothing else talking about right now then okay uh well at the top of every episode, I threw a piece of feedback. And we got this tweet from Rob Bernstein saying,
Starting point is 00:02:08 Woohoo, Mailman just brought me an early Christmas present. This one was like two weeks old. But this is Rob getting his copy of the Lifetime Puzzle book from you, Jason. I actually got my own copy. You ordered a copy. I'm embarrassed to say I have not had a chance to go through it at all yet i i keep meaning to that's hilarious uh well i hope the people who are ordering it are having fun with it i'll see tweets from people from time to time saying that they are having fun with it
Starting point is 00:02:36 um i want it to be fun for real like i thought it would be something that would just be you know idle time kind of thing like you're watching tv and you're doing some puzzles. I did see when I ordered it that it looked like it was doing well. It was like top of the charts for technical books or C++ books specifically, I guess. For a brief moment, it was the number one bestseller on Amazon for books in the C++ category. But you know, there's even, we're releasing a lot of books in the C++ community right now, but even at like one a month whoever was the last one released is probably going to be at the top of the charts
Starting point is 00:03:11 for a few days, you know? That's true. Holding its place on LeanPub's bestseller list, although I have to be fair, like I don't really understand buying it on LeanPub unless you can't get it from Amazon because as a puzzle book you kind of need to write in it. Oh, yeah.
Starting point is 00:03:27 I'm looking at Amazon bestsellers now for C++, and you are no longer number one. And the number one book does not appear to be about C++ at all. What is that? Make Electronics Learning by Discovery. The number two book has C++ in the title. Oh, you mean from actual from from make from like make zine like make colon whatever yes yes okay so it probably has some c++ like code in it for arduino programming oh that's true that's true oh yeah the number three is also from megan that
Starting point is 00:04:00 one is specifically an arduino book interesting okay Well, we'd love to hear your thoughts about the show. You can always reach out to us on Facebook, Twitter, or email us at feedback at cvpcast.com. And don't forget to leave us a review on iTunes or subscribe on YouTube. Joining us today is Slobodan Dimitrovic. Slobodan is a software development consultant, C and C++ trainer, and author of two programming books.
Starting point is 00:04:24 He specializes in training, research, development, and consulting. Slobodan has been working as a professional software developer for more than 20 years. Since 2019, he has been dedicated to providing training and consulting services, speaking at conferences, and writing programming books. He provides C and C++ training services to corporate clients through his company. Slobodan, welcome back to the show. Thanks. Thanks for having me. It's great to be back on your show. So making the switch to focusing on conferences and training in 2019, that's both good and terrible, right? Because you got like a great 2019 and then here we are.
Starting point is 00:04:58 Exactly. The timing couldn't be worse, but one needs to make the best out of this whole situation. Indeed. be worse but one needs to make the best out of the this whole situation indeed indeed the pandemic struck in this time when i was decided that i would dedicate fully be dedicated to the c++ training and c training and writing books and then the pandemic struck and but all in all we are trying to do the best with what we have so so have you been doing remote training yes exactly uh i had seen 90 of my time was remote training and then a few months ago there was some live training which was refreshing i think we all missed traveling and so on and so on well good
Starting point is 00:05:39 that you got some live training and things are well things are going weird at the moment i hope yes yeah all right well so we got a couple news articles to discuss uh feel free to comment on any of these and i'll start talking more about the new book okay sure okay so this first one is another update to the format library or fmt as some of our listeners might like to call it. This is version 8.1. It looks like there's a bunch of new features here. You know, more optimizations, more performance improvements. You can now have a blinking output as a text style. I thought that was kind of neat.
Starting point is 00:06:19 Hey, we all missed the blink tag, right? Yeah. Anything else you want to highlight in here, Jason? Or slow down? Yes, well, my experience with formatting data mainly revolves with std cout and printf, so other than that, I can't really say much
Starting point is 00:06:36 about that library. libformat is kind of like printf, that you can give formatting codes, but it's type-safe, and does compile- and runtime checking. Okay, that sounds great. The main thing that stood out to me looking at this, by the way, is the library is evolving so much and improving so much,
Starting point is 00:06:58 and it makes me question again, like I've questioned recently, what is the place for large library utilities in the standard library? Like I love lib format, but at the rate that it's continuing to improve, whatever we get in the standard library is going to be baked in there basically forever, right? And we have like the same question that I've been having lately about ASIO, like does it make sense to put networking in the standard library because we know what we get is probably going to be baked in for a very long time yeah yeah it's a great question and uh one that ultimately remains to be seen whether we should
Starting point is 00:07:38 include graphics and networking i guess one thing i would say is you know with libformat it continuing to evolve but what we have in the standard kind of being more static you know we're not missing out too much by not getting blink in right and what's in the standard um you know what what's there is is a great improvement over printf based formatting oh yeah what there is is ridiculously better and compared to c out, it's comical. But with what you're comparing it against, like networking, if new versions of secure networking come out
Starting point is 00:08:15 and the C++ standard is behind on that, that's a huge deal compared to libformat changing. And not to bring the conversation back around to ABI, but if there is a bug in your vendor's shipped networking implementation and they can't fix it without breaking ABI, the way things are right now, they probably won't fix it. So you could end up with a security flaw,
Starting point is 00:08:41 not by design, but by implementation, baked into the standard library's networking implementation provided by your vendor, I just, I don't know. I question it. I'd like to see it, but I question it still. Yeah. Okay, next thing we have is another post from Raymond Chen's blog, and this is, you can't copy code with memcopy.
Starting point is 00:09:04 Code is more complicated than that. And really interesting, you know, code that he got sent to him a couple of years back, I guess. And if you read through the whole post, it says it actually came from someone who worked for an antivirus software vendor. And it was apparently relying on this weird way of doing code injection. And he was terrified that they were doing this. Yeah. I actually recently was like, wait, is there a reliable way to copy a function from one place in memory to another?
Starting point is 00:09:43 And you only have to do like 10 minutes of research on Stack Overflow to find out, no, there's no reliable way to do this, particularly by the time the optimizer has its way with the function. And to think, yeah, that someone was relying on this for a shipping product is a little scary. Yeah, and kind of makes sense because functions are not objects.
Starting point is 00:10:02 So it kind of makes sense. So I guess this attempt to to perform this blatant copy of data was probably flawed in the first place so yes it would be cool if there was a standard way of saying i need to copy this function from one computer to another but there's too many variables by far. True. Don't try that. It's a fun read, though. You should read the article. Oh, yeah.
Starting point is 00:10:29 Definitely a fun read. Okay. And then this last thing we have is a really nice post from Botan Balos' blog, and this is 2021 C++ standardization highlights, where he's going through all the progress made this year in C++ standardization as we lead up to C++ 23 should be feature complete next month. And this is a nice wrap-up of all the progress that's been made, what's currently looking like it should get into C++ 23, what's still being worked on, what the process has been like working remotely and whether and what might change. Hopefully we might get back to normal meetings in July. Haven't been canceled yet, but who knows? Our new standard is, well, the meeting hasn't been canceled yet.
Starting point is 00:11:17 Yeah. Are there any standouts from here that either one of you want to call it? I thought this was an interesting wrap-up, personally. Well, my personal view on the... I have this feeling that people, some people, maybe find the complexity of the language challenging. But I guess, ultimately, that's the reason I'm employed and other trainees are employed. But then again, if approach uh learners in c++ and explain them that
Starting point is 00:11:47 this is probably what you need to know and this you can do without then all of a sudden c++ does it does not look so complex so just because something is there in the standard doesn't necessarily mean that we have to know about it so well, Alex, that's my view on this. Yeah, that's a good point. Yeah. So, determining what is a solid base in C++ is quite challenging, but
Starting point is 00:12:15 well, that's why Jason and me and Rob and others are... That's what puts food on our tables, I guess, right? I'm just kidding with you. It's partially true, though. Let's be fair. Definitely, C++23 doesn't look like it's going to be as big as C++20,
Starting point is 00:12:35 but there's definitely some nice things in here. Jason, I'm sure you're excited about constexpr class. Okay, was that definitely accepted? I missed that in here. Or is that in the probably will make it category oh good point constexpr class yeah you're right probably okay because i was just looking on compile on on cbv reference like yesterday like where did constexpr class go i was also looking for static operator paramand call operator because I knew that that was in the works and I thought it had been accepted, but no, that one hadn't been accepted yet. Both of those things I could have used this week.
Starting point is 00:13:15 Some of the things in the library I do think are really nice changes, like the stack trace facility should be really nice, and adding starts with, ends with, and contains to string. It's very nice to have. I think I need to spend a little more time looking at move only function because I believe part of the point of that is to reduce when allocations might need to be performed for holding a standard function type object.
Starting point is 00:13:42 That's the paper I'm thinking of. I need to read that some more. Yeah. Okay. Anything else anyone want to read that some more. Yeah. Okay. Anything else anyone want to highlight in here? All right. Well, Slobodan, so we had you on, I think a little over a year ago,
Starting point is 00:13:54 talking about your book, Modern C++ for Absolute Beginners. You're out with another one. Do you want to tell us a little bit about the new book? Sure. Thanks this time. Yes, it was around a year ago when I wrote my first book and it was about C++ and the C++ standard library and notable features in modern C++. And now I have gone the other way and wrote a C book
Starting point is 00:14:19 that is introduction to the C language, the C standard library, and the notable features in modern C standards. So if camera catches, here's the book. All right. Modern C for absolute beginners. Yes, exactly. And I would also like to steal this chance to acknowledge the two of my dedicates who are my very dear friends. And I would like to acknowledge them quickly. Okay. Yes, this book is my attempt
Starting point is 00:14:50 at providing an introductory course to C language. Sorry about that. Let's make a distinction. There is no such thing as a C slash C++. They are two different languages. This is very important.
Starting point is 00:15:06 Two different ways of thinking, two different paradigms. And I guess the biggest challenge for me, because 90% of my time is spent with C++, and when I was going to write a book, I also have a professional C experience. But when writing with C, C heavily relies on raw arrays, raw pointers, raw memory manipulation, all those things we tend to stay away from in C++. So, but C is also quite, quite interesting language. They are both tools and quite powerful tools. And yeah,
Starting point is 00:15:43 that was my intent to provide a concise introduction to the C language itself. So I'm very curious as to what modern C looks like, because I feel like when we're talking about modern C++, a lot of times we're talking about avoiding old things from C when writing C++. Sure. The C committee is well at work and they are introducing new things
Starting point is 00:16:10 to the C language and the C standard. And I guess starting with C11, I guess we can broadly call it also modern C. And the upcoming C2X, or rather as it will be called C23 standard, really introduces quite some nice features. For example, starting from C11, we have mechanisms in C that allow us to mimic function overloading we have in C++. So that's quite an interesting thing.
Starting point is 00:16:40 So the C is evolving, introducing new things. And yeah, it's not the old, not your grandfather's C. It is changing, as they say. What version of C do you cover in the book? Do you cover the C2X proposed things as well, is what I'm trying to ask? I have covered C11. There is nothing new in C17. And I also covered C2x new proposed features.
Starting point is 00:17:09 Do you have any highlights from the C2x things that maybe would surprise our C++ listeners? C++ listeners, let's look at... Well, anyone, whatever. Yeah, so there is a static assert without a message, for example. There are attributes also in C, quite a few. The book that covers these things in more details, I was focusing more on the language itself.
Starting point is 00:17:36 The book that covers these things in more details is Modern C by, I hope I pronounced it well, Jens Gustet. And he has some takeaway points in his book and there are many, many interesting things and rules. Also, novelties introduced in the modern sea language.
Starting point is 00:17:56 Quite, quite interesting read. I can recommend his work. Also, some localization, Unicode support etc. C is evolving but also I guess
Starting point is 00:18:11 the number of features introduced is to be expected is less in every new standard than we have new features in C++ for example. But that's to be expected because C is straightforward procedural language, whereas arguably C++ has everything C has plus more. And that more, roughly speaking,
Starting point is 00:18:34 can be classes, templates, and the vast universe that is the C++ Thunder library. So as someone who, you know, you said you spend most of your time in C++, but you have written both of these books now. When would you recommend C for a particular task over C++? My personal preference is C++ because I've spent the majority of my professional work with C++. But I would say use the right tool for the right job. So ultimately it's up to the developer to decide on whether to use the C or C++ depending on his use case
Starting point is 00:19:11 or the hardware he's working with or the available workflow, available compilers to him. So it doesn't... But if you want to handle both, as Bjarne says, both the efficiency and complexity, then I guess you have no choice. I mean, you should opt for C++ because it's much easier
Starting point is 00:19:33 to do object-oriented programming if you have classes and objects. And that is why I would suggest C++ for the most part. Do you cover interoperability with C++ at all or from the C's perspective of the book? No, this time I was focusing myself on the idiomatic C, idiomatic C.
Starting point is 00:20:01 And when it comes to interoperability, extern C in C++. But that's a good idea. So that's a good idea, perhaps, for another book, how to move to C++ from a C developer standpoint. That's an interesting idea. I'm just imagining the next book that you produce will be Modern C slash C++ for absolute beginners. I don't think that will bring good publicity to sorry i've been waiting to say that since you perhaps if that slash is replaced with
Starting point is 00:20:33 nth uh word then then i guess it would be better it reminds me and i don't recall if i've ever brought it up on the show before but a very long time ago, I remember this book on the shelf in bookstores that was called the C slash C plus plus slash C sharp Bible. Oh, wow. And that sounds quite new to me. It sounds confused is what it sounds um so you said you focus on like idiomatic good c and it's been i have to look at the calendar 20 years since i actively programmed in c um and i'm just like i'm sure that the code that i wrote back then was terrible but you you mentioned or rob mentioned that there's lots of like manual memory management and that kind of thing. Does good modern idiomatic C take steps to abstract away at all the manual management of arrays and memory and that kind of thing? Not so much, I would say.
Starting point is 00:21:39 Okay. But that, again, is not the C's goal. Right. So those are two different ways of thinking. They say that it's more challenging for a C developer to move to C++, but for me as a C++ developer, it was quite challenging to shift this mindset and to think in C ways of programming.
Starting point is 00:22:02 So it can be challenging both ways, but ultimately, ultimately it's no, it's no rocket science. So what inspired you then to work on this book? That's a great question. I had some free time on my own and I said, well,
Starting point is 00:22:20 people say that if you know C plus plus, you probably know the majority of C and I wanted to explore if that is really the case, but that is, as it turns out, there are a lot of things in C that we take for granted, but that needs to be explored quite into greater details. And, for example, row arrays and row pointers in C, it's part of the workflow. It is just what it is. And one needs to think in C ways.
Starting point is 00:22:47 One needs to think that there is no std string type. You have to work. If you want to work, manipulate strings, then that's quite a different way of working than in C++. I guess some of the C developers, when I was doing training, training C developers to move to C++. I guess some of the C developers, when I was doing training, training C developers to move to C++, and when they see how we have a std string and we have containers such as vectors
Starting point is 00:23:15 or maps and sets and so on, they begin to appreciate the, how should I say, the comfort we have in C++, if you wish. Because in C, we have to build all the containers from scratch each time. There are third-party libraries, but since there is no generic programming,
Starting point is 00:23:35 there is no container that can accept everything. I mean, there are some ways with void pointers and so on and so on. But when people who have spent the majority of their time with c and want to move to c++ when they see that there is a stood string type that does all the heavy lifting for us they begin to appreciate that level of comfort also vectors and arrays and stood arrays and maps sets and so on and so on, they begin to appreciate how vastly more capable, if you wish, the C++ standard library is.
Starting point is 00:24:14 Because it covers up more ground, I would say. So that was my experience with it. When I last used GTK, which is a c library i was observing that they there's lots of object oriented to c style approach there with like structs of function pointers and you know it's it's almost like using universal call syntax it'd be like append and then the the container thing you want to append to and then the object you want to append into it. I'm just curious, like, did that, does that kind of approach still come up in idiomatic C? Is that something you cover in your book?
Starting point is 00:24:50 I try to stay away from that approach as well. Okay. I try to teach idiomatic language, the theoretical introduction to the language, and when it comes to that kind of C with class style programming i my advice is to stay away from it so you would say just go to c++ yeah exactly but then again c is also capable language uh the python there is a python implementation in c so and python is high level functional programming language and it's all made in C. So there are
Starting point is 00:25:25 ways around it. As Scott Myers says, you can definitely do object-oriented programming without having objects and classes. So it's all a matter of approaching the how you approach this. And ultimately, what gets executed on the machine is neither C nor C++.
Starting point is 00:25:42 At best, we can think of it as an assembly language, but it's not even that. So it boils down to what subjects are we tackling, what's our goal, and what are the tools available to us. It's interesting that the quote from Scott that you just mentioned, that you can do object-oriented programming without classes um I had a friend in university who told me about a assembly language class that he had taken in high school so this was a very long time ago where one of their assignments involved object-oriented assembly code that they were handwriting that was I've never really quite wrapped my mind around that since then yeah Yeah, that sounds quite interesting and challenging, I would say.
Starting point is 00:26:28 To say the least. About how long did it take you to create this book? I would say something over six months. It was two and a half decades in the making and six months in the writing. So the first book must be going relatively well for you to invest time in a second one? Well, the first book is doing well, especially in other companies' subscription models.
Starting point is 00:26:56 I think one company has seen some tens of thousands of downloads of the subscription- based on interesting okay yes but i think bianna said or someone i can't remember exactly that if you wish if you hope to make a fortune writing technical books you might reconsider your decision right i don't know what what's your experience on that uh my my books are okay, but I have a little bit of a cheater perspective here in that I have my own advertising platform and I'm self-publishing. So I get a higher percentage of the sales. But that's beside the point. I mean, I've been I was sitting on a trainer's panel at CPP Con con long time ago and uh several people up there had written books and
Starting point is 00:27:47 someone asked about writing books and everyone agreed like no you don't write books to make money but scott myers happened to be on that panel i think it was his last appearance at cpp con and he's like i don't know what you guys are talking about it made plenty of money for me yeah interesting Scott Myers work was quite an amazing one ultimately his work is what got me interested into moving full time into C++ I remember his effective
Starting point is 00:28:16 C++ series that was quite quite an amazing for me when I was starting with C++ and it has kept me entertained And it has kept me entertained. This language has kept me entertained ever since. There's always something new and something interesting to
Starting point is 00:28:31 explore in C++. I think trainers, especially, they're spending quite a lot of work and their time in researching all the things and presenting those things to people who want to learn C++ in an easy-to-understand manner. So I say that's sometimes fulfilling work, often fulfilling work, I would say.
Starting point is 00:28:58 Do you have any other additional books planned at this point? Remember that effective C slash C++? Yes. Yeah, sorry. Exactly. point remember that effective c slash c plus plus or yes yeah sorry exactly i was thinking about a book that will help c developers uh move to c plus plus for people who want to move to c plus plus but have a c background and i would introduce the i would introduce the novelties in c plus plus and also make sure that they part with the C way of thinking if they're moving to C++.
Starting point is 00:29:28 So there are no raw arrays. I mean, there are raw arrays and there are raw pointers, but we should probably depart from those. Also, raw memory manipulation. We delegate those things to other facilities in C++. We don't have to manually new and delete everything. We have facilities for that job. Also, forget about malloc, realloc, and calloc.
Starting point is 00:29:55 Those are things that you will rarely, rarely use in C++, if at all. So changing this mindset and habits from a C developer, of a C developer when moving to C++ and vice versa, if there are cases, vice versa, and that can be interesting, I think. That can be an idea for a third book or something
Starting point is 00:30:19 else, or a training. So do you think, you said that this book kind of started with the premise of as a c++ developer you most you pretty much have to know most of the important parts of c already i believe that's how you phrased that yeah what has been your conclusion on this like should c pro c++ programmers also be efficient in c should we we worry about C? Should we just focus on C++? What did you learn in the process of writing this book? Great question. What was new to me is that, okay, now you have this completely different
Starting point is 00:31:01 way of thinking. So forget about smart pointers forget about not working with the raw arrays not working with the raw pointers pointer arithmetic even those are all building blocks of a C language because that's how C is and it what was revealing to me that I had to get used to this way of thinking and that is now that was the most challenging thing for me so get used to this we have these blocks of memory and we manipulate those blocks of memory and there are no containers so forget about that build your own containers that was a quote challenging for me so get used to not having the facilities to work with strings, for example. You always have to have in mind that you need to have at least two buffers for manipulating data.
Starting point is 00:31:54 If you want to add a string to a string, now you have to have a new array, the destination array, which will hold your data. So for a C++ developer who are used to working with std string, that can be something that might require some time to adapt. You said the book is... Oh, sorry, go ahead. And also, one of the biggest differences is
Starting point is 00:32:18 metaprogramming. We don't like to have in C++, we don't rely too much. But there are also some similarities because if you want a function, then write a function and do not rely on the pseudo function in C and C++. So that was something that was eye-opening to me when I was moving. People say, you moved down to C.
Starting point is 00:32:52 No, I haven't. I just moved to C when writing this book. And I had to suspend all my ways of C++ thinking when I'm writing a C program or a C book and thinking in C ways. That's how things work. There are similarities, but there are also differences. I do personally rely heavily on function, type-based function overloading,
Starting point is 00:33:13 which as we know, like C99 didn't support that, but you said C11 has some way of doing overload, overloading of some sort? Sure. It's called type generic macros using generic keyword. And it's called the controlling expression. So you end up writing, you can't have the same name for the function overloads as we do in C++.
Starting point is 00:33:38 But you write my function underscore E I, which is for for integer and then you write another one and then through means of controlling expression macro you write some code and then you end up with the function overloading appearance i mean you are calling the appropriate function but just because there are no function overloads per se as we we have it in C++, then we have to resort to this C11 standard using the generic macro. So you have to write n different versions of the function, however many that is, and then write a macro that ties them together, basically? Exactly. That sounds painful. It's a novelty introduced in C11.
Starting point is 00:34:24 Up to that point, there were no options for function overloading. So it might be painful to us C++ developers, but it provides functionality of function overloading. That was a comment I just saw from someone recently, is that one reason Rust is a non-starter for them is it doesn't have proper function overloading like we have in C++. Oh, really? Yeah.
Starting point is 00:34:48 I wasn't aware of that. Yeah, Rust is... Well, yeah, I forget the details. But yeah, its function overloading mechanism is different anyhow. Yes, I recently made a tweet about how C++ is really not that complex and some of the comments were, some of them were revolving around Rust and some of them were revolving around how it really is a complex language. But it's not rocket science.
Starting point is 00:35:19 I mean, C++ is not rocket science. They say although used in one, but really it isn't. It is complex, but it is as complex as we make it, as we want it to be. That's my view. I think I saw something that said lambdas might be making it into C23. Is that something
Starting point is 00:35:36 that came up in your research for this book? Lambdas? No? Okay. I haven't seen that one. Let me cheat a little and see if there are any. None that I see. Okay. I haven't seen that one. Let me cheat a little and see if there are any. None that I see. Okay. All right. Maybe I misunderstood that or didn't get in.
Starting point is 00:35:51 Perhaps I could also be wrong. I don't know. We'll see. We will have to wait another year or so. But there is a proposal. For example, did you know that CPP reference also has a C reference? Yes, but I never click on it. So here's the chance now.
Starting point is 00:36:09 That's a good point. Also, CPP reference has separated C standards in different articles. That's a little known fact, but it is there. I think you've mentioned that you do both C and C++ training. Is it sometimes hard to turn your C++ brain off when training a bunch of C developers or prospective C developers? It can be challenging from time to time, but ultimately, yes, it can be challenging.
Starting point is 00:36:40 I tend to not mix. If I have a C training today, I can't really. I do my best not to have a C++ training the other day because that context switching can be challenging. So I tend to do one training and then separate training for C++. And I also tell C++ trainees that definitely do not mix C with C++ and don't use the old ways, don't use const char star my string equals hello world in C++.
Starting point is 00:37:14 While in C, we don't have other options other than that. And character arrays, of course, which are basically the same thing because array gets converted to a pointer, as Bjarne says it at the slightest provocation that decadal pointer man, that's one of my most annoying
Starting point is 00:37:31 anyhow I said it's been 20 years since I programmed in C regularly and at the time I was C++ at work and when I went home I was playing with a toys project in C. And I did it partially just so that I could keep the differences separated in my head, like remember what made C
Starting point is 00:37:53 different. I gave up on that pretty quickly. So I can't even imagine how difficult it would be, like you said, if you were trying to teach a C++ class today and then a C class tomorrow how you could just be like well just use standard string I mean ah shoot I mean use str cat exactly but for example each C standard introduces quite some new functions that deal exactly with this string manipulation or rather any character array manipulations
Starting point is 00:38:29 because they are all bytes under the code. So one needs to get used to working with bytes directly in C, whereas in C++ we want to be shielded from having to work with those things. We can if we want to, but we probably should not, I think. An interesting point, too, yeah, because in C, I mean, it seems like there'd be something freeing there when you know, like, well, it's just bytes, I can do whatever I want to.
Starting point is 00:38:57 And in C++, you're like, no, if we don't think about the objects that exist there, then we're probably invoking undefined behavior. It's easy to do that. Yes. Now I want to program in C again so that I can just do whatever I want to to my data. Exactly. But also C as well, when it comes to similarity,
Starting point is 00:39:19 C is also a strongly typed language, statically typed language as well. One novelty, uh surprise for me was that for example the c the car literal type is int and not car as we have it in c++ that was interesting interesting for me probably due to some historical reasons or whatever right interesting i guess i've only seen that come up with things like the isDigit functions and that kind of thing, which I occasionally use isSpace and whatever. Those all expect an int since they're C standard library functions.
Starting point is 00:39:58 I hadn't thought about it more after that, though. Rob, have you ever worked with C in your professional life? I mean, no, not really. I've certainly had, you know, some C++ code with extern C, but I've never worked with C professionally. I've used some C libraries, but that's about the extent of it. Yes. And for example, if we have a function that has no parameters in C, we have to put a void keyword inside that. And the newest standard works toward removing that necessity. So in C++, we don't have that.
Starting point is 00:40:34 Also, we need to type def the entire structure in C to give it a name, whereas in C++, we have a structure whose name is a type name already. So those are some of, there are also some slight differences, but ultimately what we have today are operating systems that are mainly built in C for better or worse,
Starting point is 00:40:55 but they are built in C. And I hope, hopefully in the future, we can see an operating system that was completely built in the C++, hopefully. I'm starting to see the title of your next book, by the way. I don't think it's moving from C to C++.
Starting point is 00:41:09 I think it's modern interop between C and C++, because we all have to deal with it at some point. Exactly. Might as well be true. Might as well be true. We'll see. All right. Well, it's been great having you on the show again today slow but on definitely uh
Starting point is 00:41:28 let us know if you work on any additional books and uh you know definitely encourage listeners to go check out the the modern c for beginners book if uh they're interested in you know learning what the latest uh c development style looks like okay for having me. It was a pleasure for me. Thank you so much. Thanks so much for listening in as we chat about C++. We'd love to hear what you think of the podcast. Please let us know if we're discussing the stuff you're interested in, or if you have a suggestion for a topic,
Starting point is 00:41:56 we'd love to hear about that too. You can email all your thoughts to feedback at cppcast.com. We'd also appreciate if you can like CppCast on Facebook and follow CppCast on Twitter. You can also follow me at Rob W. Irving and Jason at Lefticus on Twitter. We'd also like to thank all our patrons who help support the show through Patreon. If you'd like to support us on Patreon, you can do so at patreon.com slash cppcast. And of course, you can find all that info and the show notes on the podcast website

There aren't comments yet for this episode. Click on any sentence in the transcript to leave a comment.