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.

相关推荐
aaaameliaaa3 小时前
字符函数和字符串函数
c语言·笔记·算法
夜月yeyue3 小时前
AUTOSAR CP 从上电到 Runnable
c语言·网络·tcp/ip·车载系统
微学AI4 小时前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
豆瓣鸡4 小时前
算法日记 - Day3
java·开发语言·算法
白白白小纯5 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
合调于形5 小时前
Bianfchheng (Liuu) 《边城(六)》字母标调拼音拼写实测案例
人工智能·自然语言处理·人机交互·语音识别·学习方法
韭菜炒鸡肝天5 小时前
VTK开发笔记(一):VTK介绍,Qt..+VSx+VTK.编译
开发语言·笔记·qt
小羊先生car5 小时前
RTOS-F429-HAL-绝对延时和相对延时(2026/7/31)
c语言·rtos
Aaron - Wistron6 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
Dxy12393102167 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python