CS50 x 2024 Notes C - 09

All right, so beyond that, let's just go ahead and put this into context. Just in case it helps you to think about this.

This is just another flow chart, if you're more of a visual thinker, that represents what it is this loop is now doing. Previously, all of our arrows went from top to bottom and stoped. But now there's an arrow going back, up and around because of this loop, this cycle. So when we start this program, we set i equal to 0.

We then check, is i less than 3 ?

Obviously it is, so we print " meow ".

We increment i, and then we go back to that same condition. We check the condition. We print " meow ", i plus plus, go back, go back.

Now if I equals 3, 3 is not less than 3, so the answer is false. And we stop. So again, it's just another way of thinking about how the code in Scratch, how the code in C, might alternatively work in each of these contexts.

But these's this one other puzzle piece in Scratch, recall, that's not the repeat block, which is for finite numbers of repetitions, but forever.

And in C, there is a way to do this, but it's a little weird looking. There's no forever keyword. But you can use the while loop or, as you inferred, you can actually use the for loop without a condition in the middle. So here, I can actually say this,

If I want to do something forever I want to make sure that the answer to my question, the Boolean expression is always true, always true, always true, the easiest way to achieve that goal is just literally write " true " there, because true is true no matter what. And it's a trick for making the loop forever, go around and around, as you might if you want the cat to live forever and meow incessantly or if it is a clock that you want to tick forever or the like. So here, for instance, is how we might have a cat meow endlessly using this so-called for loop intead. But recall that in Scratch, we also had this ability to create some of our own puzzle pieces. And this, too, is something what we're going to be able to do here in C. And let me propose that we do exactly that by introducing the C analog of this.

So here, for instance, is, in Scratch, our definition of a function called meow whose sole purpose in life was just play the sound " meow " untill it's done.

This is going to look a little weird at first. But you'll notice some similarities with main. So recall this thing keep typing with main, int main( void ), int main( void ). That's just the " when green flag clicked " equivalent for today. But if you want to create your own puzzle piece or your own function in C, you, for now, literally do this.

You say, void, the name of the function you want to create and then void in parentheses. And technically what this means is that this function has no return value. It doesn't hand you anything back like get_string or get_int.

And the " void " in paretheses means it takes no inputs. It only meows. You don't have to tell it how to meow. It's just going to meow. So no arguments, so to speak.

This literally just prints out " meow ". But what this does for me is it abstracts away the idea of meowing. I don't need to know how to use printf or that you're using printf to make the cat meow. I now have a function in life called meow.

Because in Scratch, recall, I used it like this. when the green flag is clicked, I could repeat three times this new custom puzzle piece.

But in C, I could now do this. In my main program, I can use a for loop just like we saw a moment ago, copy/pasted from earlier. But now I can call my own C function called meow.

And let me go ahead now and do this.

If I go ahead and delete everything inside main.

Let me go ahead and do for in i equals 0, i is less than 3, i++. Inside of my curly braces, let me go ahead and say " meow ". But I now need this meow function to exist.

Because if I do " make meow " again, notice error. " Implicit declaration of function meow is invalid in C99 " - - the 1999 version of C. What does that mean ? Well, it doesn't know what the meow function is. And the meow function is not in CS50.h. It's not in stdio.h. I have to create it.

So let me type out or really copy/paste what I had on the screen a moment ago - - void meow( void ), printf, quote, unqote, " meow\n ", close quote, semicolon. Let me do make meow.

And unfortunately, I still have an error, if I scroll up, still on line 7 of meow.c, my compiler thinks that meow is invalid, that it does not exist. This too is a common mistake.

And as simple as, this code might be in spirit. Where did I screw up ? Yeah, I need to define the function before I use it. So again, C is going to take you literally.

If you try to call meow on line 7, you better not define it on line 11. You better define it higher up. So the simplest fix is going to be just to do this. Let me clean my terminal. Let me highlight and just delete the meow function.

And let me just paste it up here. And this will actually solve the problem.

Make meow, now works. And if I do ./meow, that, too, works. But this isn't really the best solution because if your solution is constantly, oh, well, just put it up there, put it up there, put it up there. I bet we could contrive a situation where one function needs to be above the other, but it needs to be above the other. And that's just not going to work in general. And more importantly, it just pushes main lower and lower and lower in your file. But the whole point of your main function is like, that's the entry point. That is what happens when the green flag is clicked. And so just in terms of user convetions, it's just useful for main to always be at the top of a file because then you can find it fast. Your friends can, your TFs can find it quickly if it's at the top. So the other solution here would be to leave meow at the bottom and leave main at the top. But this is the only time, if I may, that copy/paste is ok.

What I've highlighted here is line 11 is what's called the function's prototype. It is enough information to give you the return type, the name of the function, and the return value and any arguments.

And so if you just copy/paste that one line and end it with a semicolon up here, that's enough of a hint to the compiler, that, ok, it doesn't exist yet, but it will. And it will look like that. That's the only time it's ok to copy/paste, the very first line of a function you've written to the top of the file with a semicolon. So that you can make the compiler happy.

So if I do make meow now, still no errors, ./meow, and it now works.

相关推荐
兔小盈1 小时前
多线程篇-(二)线程创建、中断与终止
java·开发语言·多线程
hoiii1872 小时前
基于MATLAB实现内点法解决凸优化问题
开发语言·matlab
相醉为友2 小时前
040 Linux/裸机/RTOS 项目开发的跨平台兼容性——C语言静态接口抽象底层原理分析
linux·c语言·mcu
特种加菲猫2 小时前
多态:让代码拥有“千变万化”的能力
开发语言·c++
Mr_pyx2 小时前
【LeetHOT100】LRU缓存——Java多解法详解
java·开发语言
zx2859634002 小时前
Laravel 4.x:颠覆PHP框架的10大革新特性
开发语言·php·laravel
threelab3 小时前
Three.js 咖啡杯烟雾效果 | 三维可视化 / AI 提示词
开发语言·javascript·人工智能
初心未改HD3 小时前
gRPC 与 Protobuf 实战指南
开发语言·golang
weixin_421725263 小时前
2026年C/C++/C#全解析:底层语言的进化与场景抉择,选错直接掉队
c语言·c++·c·编程语言·技术选择