Placeholder Image

字幕列表 影片播放

  • COLTON OGDEN: All right.

  • Hello, world.

  • This is CS50 on Twitch.

  • My name is Colton Ogden, and I'm joined, once again, by--

  • NICK WONG: Nick Wong, hello.

  • That is my name, actually.

  • Sorry.

  • I guess I should start with that.

  • Oh, well.

  • COLTON OGDEN: What are we talking about today?

  • NICK WONG: So today we're going to talk about C, not C++, not C#, not Python.

  • COLTON OGDEN: A lot of C languages out there.

  • NICK WONG: C, yeah.

  • COLTON OGDEN: But C is like the progenitor-- progenitor?

  • NICK WONG: Yeah.

  • Progenitor, I think it was.

  • COLTON OGDEN: Progenitor.

  • These are the old fashioned ones, very simple, very lightweight

  • programming language.

  • NICK WONG: Yes.

  • COLTON OGDEN: And today--

  • NICK WONG: Fast is how it works.

  • COLTON OGDEN: --we're not going to-- yeah, very fast.

  • We're not going to be talking necessarily about the stuff

  • that we cover in CS50, though, right?

  • We're going to be doing maybe more of a deep dive?

  • NICK WONG: Yeah.

  • So we will open up GDB, which is the debugger for CS--

  • sorry, for C. Yes, for all of CS.

  • COLTON OGDEN: For all of CS.

  • NICK WONG: A granddaddy debugger.

  • We're going to walk through a little bit more in depth than CS50 does.

  • Although CS50 does talk about things like Malok

  • and memory spaces and things like that.

  • But I will also kind of be a little bit clearer on stack versus heap allocation

  • and why one was important, why the other one is not--

  • sorry, why any one is important at any one time.

  • We'll talk a little bit about buffer overflows and things

  • like that, as well as how input-output standard I/O works,

  • because I think that's really cool.

  • COLTON OGDEN: Yeah, yeah.

  • No, except low level, to a lot of people that are maybe not

  • super familiar with CS50 might think that means very basic.

  • NICK WONG: Easy, simple.

  • No, no.

  • That is a great misconception, I think, where a lot of programmers will say,

  • low level, high level, and then people are like,

  • oh, I want to learn high-level stuff.

  • And generally, that actually seems to be kind of true in the modern day and age.

  • A lot of people do like to learn high-level things.

  • But that is not because it's harder or cooler or anything.

  • High may kind of refer to, if you imagine a computer system

  • as at the lowest level, you have hardware, and at the highest level

  • you might have a graphic interface, and everything in between

  • is kind of organized.

  • Then that's what people are talking about.

  • So in the lowest level you have this literal, electrons running around

  • in, like, a piece of metal.

  • If you wanted to kind of vastly oversimplify and humorously

  • oversimplify what's going on, we basically took a rock,

  • tricked it into thinking, and then electrocuted it.

  • And that's the computer, right?

  • That is a computer's lowest level.

  • And then right above that, you have this kind of--

  • and we'll skip a few levels, just for the sake of time and clarity.

  • But there's basically machine code, and we'll kind of approximate that

  • as assembly, which is basically--

  • I'll show a couple of examples of what that looks like.

  • But the instructions, they're roughly one-for-one instructions

  • that a processor will execute.

  • And I say roughly, because that's not entirely true,

  • but it's the right concept.

  • And then maybe one step up from that, we might

  • have something like C, which is then going to be compiled down

  • into machine code or assembly.

  • And then that will get interpreted in some way.

  • And then maybe above that you might have something

  • like Python, which actually ends up kind of running on C. I mean,

  • it depends on the runtime you pick.

  • But it could actually be run through C.

  • COLTON OGDEN: Use C to compile the program that

  • executes the Python scripts, basically.

  • NICK WONG: Right.

  • Exactly.

  • Cool.

  • Awesome.

  • COLTON OGDEN: I'm very excited to dig into this stuff, because I

  • know C is a very popular language, or has been historically,

  • for game development.

  • NICK WONG: Right.

  • COLTON OGDEN: Assembly before it, for the old--

  • to tie back into, like, our NES stream.

  • NES was really in 6502 assembly--

  • NICK WONG: Oh, that's brutal.

  • COLTON OGDEN: --a very common older microprocessor,

  • still in use for small devices, but not as much

  • as I think more RMCPs these days.

  • NICK WONG: Yeah.

  • COLTON OGDEN: We have a lot of people in the chat.

  • I just want to shout out everybody who's joined us here in advance.

  • So scrolling way up, we have a lot of chat here.

  • So [? TwitchHelloWorld-- ?] that's [? JacksJPGuys, ?] [? ISOTV-- ?] let me

  • just keep scrolling here. [? Brenda, ?] [? RealCuriousKiwi, ?] was in the chat.

  • I see [? NonBored. ?] Thank you for joining--

  • [? Asley, ?] [? Nowanda353, ?] as always.

  • [? John09JLardinois, ?] thank you very much for joining today.

  • [? VippleBHJ ?] and [? Bhavic Knight-- ?]

  • [? Bhavic Knight's ?] a long-time regular as well.

  • Let me just make sure I haven't missed anybody else.

  • I think we had [? LKit57 ?] joined us.

  • I don't recognize that name.

  • I might just have a terrible memory.

  • But I hope I don't have a terrible memory.

  • Thank you very much for joining if this is your first time.

  • If not, I apologize. [? OoeyZuck, ?] Thank you very much for joining.

  • [? GoldenKappaChick-- ?] I'm not entirely sure what that means.

  • But I appreciate it.

  • Thank you very much.

  • Oh, we've got a lot of Kappas.

  • Maybe that's what-- maybe they're trying to do a Kappa test.

  • NICK WONG: Yeah.

  • COLTON OGDEN: Test and see if Kappas are working.

  • OK, [? JLardinois ?] was talking about [? AnnFogleman, ?] craft projects,

  • this is like the Minecraft C port, which sort of ties--

  • NICK WONG: I see, yeah.

  • COLTON OGDEN: --into this conversation.

  • He's saying he found a really good use for--

  • it looks like switch-case.

  • NICK WONG: Switch-case and break.

  • Nice.

  • COLTON OGDEN: Switch-case is a very, very common puzzle piece to use in C.

  • Everybody's hyped--

  • phones less delayed than PCs.

  • That's interesting-- higher the level, more abstractions.

  • NICK WONG: Right.

  • That is a great way of putting it, actually.

  • COLTON OGDEN: Yeah-- obviously trying to manipulate the Silicon directly with--

  • it's just you're going to be less productive than being able to write

  • a Python script, for example.

  • NICK WONG: Right.

  • Exactly.

  • COLTON OGDEN: So different layers of abstraction

  • allow you to do different things more or less easily.

  • MoHelat says, what can we do with the C language?

  • NICK WONG: So that's actually a really interesting question.

  • In talking about any--

  • they're called Turing-complete languages-- so basically,

  • any programming language you can think of--

  • you can do anything.

  • Anything that you can imagine being done with any language

  • can be done by any of the other ones.

  • So if I can do it in Python, I could also do it in C.

  • I could also do it using only NAND commands.

  • Technically, there is that possibility.

  • So generally, what ends up happening is, when people say what can we

  • do with language, or what do programmers do in a language,

  • they generally mean a little bit more like, what's convenient?

  • So what things have been written to be really, really good in that language?

  • What things have a bunch of APIs or libraries?

  • I guess, what has the language been really specialized for?

  • So when someone says that like R, for example, is built for biostatistics,

  • they mean, really, that there are a lot of developers who are in bio

  • and do a bunch of biostats who develop for R.

  • And those people are going to be a great community for that.

  • They have a bunch of resources.

  • They'll answer all your questions on it--

  • all sorts of things.

  • But that is not necessarily going to mean that you couldn't, in R,

  • build Minecraft, for example.

  • In concept, you could.

  • But whether or not you'd want to is--

  • I mean, I would argue you probably don't want to.

  • [LAUGHTER]

  • COLTON OGDEN: That'd be interesting to see.

  • NICK WONG: Right.

  • COLTON OGDEN: I'd be curious to see how somebody does that.

  • NICK WONG: --or like, a neural network in OCaml.

  • You can do it.

  • It would not necessarily be super fun.

  • Whereas in Python, It's pretty convenient.

  • And so these kind of choices-- they're based on convenience and comfort.

  • COLTON OGDEN: People have done a lot of hard work

  • making libraries and stuff like that, too.

  • NICK WONG: Right.

  • Exactly.

  • COLTON OGDEN: --people that have had an interest in solving these problems--

  • NICK WONG: Yes.

  • COLTON OGDEN: --put that hard work in in advance

  • to make it easy for everybody else.

  • NICK WONG: Exactly.

  • COLTON OGDEN: [? GravyWave123 ?] says buffer overflow.

  • NICK WONG: Yes.

  • We will talk about that, actually.

  • COLTON OGDEN: Why is C considered so fast once it is compiled?

  • Wouldn't any compiled language already be

  • compiled into binary, so therefore run with the same speed?

  • NICK WONG: Sure.

  • COLTON OGDEN: So that;s [? TwitchHelloWorld? ?]

  • NICK WONG: That is a great question.

  • So one of the things is that the developers of the C compiler--

  • if we're talking about CC or Clang, then that

  • has been just so thoroughly developed.

  • It's a brilliant compiler.

  • And we'll talk about compiler flags a little bit today.

  • And I think that--

  • there's a funny story dealing with compiler flags and the speller piece

  • in CS50.

  • But basically, the builders of that compiler-- the architects--

  • designed so many, just, brilliant techniques

  • for compiling from the C abstract machine down into assembly

  • that that ends up abstracting out-- or I guess not abstracting out,

  • but cleaning up a lot of your C code once you've

  • written it, to make it faster.

  • And one of the other problems that's faced by--

  • maybe you built another compiler and it's just as smart

  • as theirs, except maybe your code is like Python, where it's interpreted.

  • So every time I run it, it actually has to interpret the code that is being run

  • and then compile it as it goes.

  • So then that interpretation step actually

  • ends up taking up a bunch of time, as well.

  • And there's a couple other reasons that deal a little bit more with how C

  • integrates-- or C, C++, C#--

  • all integrate with--

  • I guess integrate's the wrong word.

  • It's not exactly what I'm looking for--

  • don't have as much overhead in what they do.

  • So for example, Java requires the entire JVM in order to run.

  • And that's a lot more overhead than C, which is literally just executing

  • instructions.

  • So there's all sorts of interesting things there.

  • There's a lot more complexity to that than I will be able to answer--

  • and probably--

  • guaranteedly more than I know.

  • [LAUGHS] But those are some reasons that come up to the top of my head.

  • COLTON OGDEN: Yeah.

  • So abstraction, in a lot of ways, you're paying for performance.

  • NICK WONG: Right.

  • COLTON OGDEN: Obviously electricity flowing through a rock--

  • NICK WONG: [LAUGHS]

  • More performance.

  • NICK WONG: Very fast.

  • You might not understand it, but it's very fast.

  • COLTON OGDEN: And to your point about NAND gates--

  • I think it was watching a video recently on Computerphile or something,