CS50 x 2024 Notes C - 04

But C's not done with us yet. If you've learned Python or certain other languages, you'd kind of be done writing code at this point. In C though, you additionally have to tell the compiler what type of variable you want to use. So if it's a string of text, you say " string ". If it's an integer, a number, you say " int ", as we might have seen before. So it's a little more pedantic. It's more annoying, frankly, the more onus on you and me, the programmers.

But this just helps the compiler know how to store it in the computer's memory. And I'm so close to being done with this line of code, but what's missing ? So semicolon. And mark my words, if you've never programmed before, sometimes this week, this semester, you will forget a semicolon. You will raise your hand. You'll get frustrated because you can't understand why your code's not working. You will run into stupid issues like that. But do take faith that they are stupid issues. It doesn't mean it's not clicking for you or you're not a programmer. It just takes time to see these things if it's a new language to you.

So there now is my semicolon. All right, let's go ahead then and do something, with that return value using the second of the big puzzle pieces in Scratch. So when I wanted to say, " hello, David ", or whatever the human's name is,

I kind of stacked my puzzle pieces like this. This is actually similar to Python and maybe some other languages some of you have learned. But C is a little bit different.

And the closest analog to this Scratch solution is going to look like this. I still use printf because printf is the equivalent of Say.

Inside of my parentheses, I'm going to go ahead and say, weirdly, " hello, %s ". So these's no real analog in C of Join. Instead there's a way to specially format text using printf, hence the F in " printf ". And what you do in printf is you type whatever English word or human words that you want. You then use %s a placeholder. If you want a string of text to be added to your own text, you literally write " %s ". And let me anticipate a question from the crowd - - how do you print out %s ? There's a solution to that, too, if you literally ever want to print out %s. But it's deliberately a weird choice of characters so that the probability that we ever need to type this ourselves is just low that no one really worries too much about it. All right, but that's not quite enough. In addition to saying " hello ", comma, space, placeholder %s - - and just for vocabulary sake, that's a format code. Again, " format " being the F in printf.

I still need my double quotes around the whole thing. In this case, to match my previous program, I am going to go ahead and add the backslash n to move the cursor to the next line. And now I've left a crazy amount of room here, but that's deliberate. Does anyone have an instinct for what I'm probably going to to want to add after the quotes but still inside of the parentheses ? So answer itself I need to somehow tell printf with a second input, otherwise known as an argument, what I want to substitute for that %s.

And so I put a comma and then the name of the variable that I want printf to figure out how to plug in here. So honestly it's a little annoying, and this is kind of a dated approach. Newer, more modern languages, like we'll see later, in the course, Python and JavaScript, actually have much more user-friendly ways of doing it. But once you wrap your mind around the heuristics, the rules here, it's just formatting a string by plugging in whatever you want into this format string, so to speak.

And again, the comma here is important. This signifies that it takes one input at left and a second input at right.

But notice this comma. There's technically two commas. But I'm not claiming that this function takes three inputs. This comma I'm pointing out doesn't mean the same. This comma that I'm pointing to this part of the quotation marks and therefore part of my string of English text. So this is just English grammar. This is sort of C syntax. And again, these are the sort of annoying little details that we've using the same symbol for different things, but context matters. So just stare at your code, look carefully left to right and generally the answer will pop out, no pun intended.

Ok, questions now on this syntax before we actually write it and run it ? Why is the backslash n not after the answer ? So the way functions work, including printf, is that you pass to them one argument inside of the parentheses.

And then if you have a second argument, you put it after this comma here. But the way printf work is that its first argument is always a string that you want to be formatted for you.

So anything you want printed on the screen has to go in those quotes. And you can perhaps extrapolate from this. If I actually wanted to say multiple things in this sentence, so " hello ", maybe first name, last name, I could actually do " hello " comma, %s, space, %s, if I had two variables, one called First Name, one called Last Name. But then I would need another comma for a third input to the function. And so it's very general purpose in that sense.

All right, so let's actually do something with this code rather than just talk about what it might be doing for us.

Let me go over to, for instance, VS Code again.

And I'm going to go ahead now and remove this middle line of printf. I'm still in my same file called " hello.c ". I'm going to clear my terminal window just to eliminate distraction.

And to do that, I can literally type " clear ",

But this is just for aesthetic's sake. That's not functionally that useful. Or you can hit control L to achieve the same on your keyboard. But I'm going to back to line 5 here, where I previously just said " hello, world ". And I'm going to do this instead.

I'm going to give myself a variable called answer, the type of which is string. I'm going to set it equal to whatever the return value is of get_string, asking an English question, " what's your name ? ", with just a single space just to move the cursor over, followed by a semicolon.

Then I'm going to go ahead and say printf, quote, unquote, " hello, placeholder, backslash n ", comma, and then what goes here again ? This is where answer goes. And then I just need a semicolon on the right of that. But I think now that I'm done. But let me point out a couple of details. This got very colorful, very pretty quickly. And it's not like the black and white code I had on the screen a moment ago.

This is because what programs like VS Code do for us is it " pretty " prints, or rather it syntax highlights our code for us. So syntax highlighting means just add some colors to the code, so that different ideas pop out , so you will notice, for instance, that printf here, get_string here, are in purple because they represent functions, just like the Say block. Here, " what's your name ? ", quote, unquote, in VS Code is a light blue instead of white. But it's still going to be consistent if I use strings of text elsewhere, as well, so I didn't type anything special. This isn't like Microsoft Word or Google Docs, where I'm highlighting and changing colors of things. This is all happening automatically. But it's just unicode text. It's just being interpreted automatically and having these colors applied so that things pop out more usefully visually. Now, I've unfortunately made a mistake. But I'm going to deliberately induce this one because you, too, will probably make this mistake.

I'm going to go ahead and run " make hello " again, because I've changed my code. So I have to regenerate the machine code from the new source code.

But unfortunately, when I hit Enter now, my God, the errors don't even fit on the screen.

So let me make this bigger. I'm going to click the little caret symbol here just to make my terminal bigger for just a moment.

And you'll see that there's more lines of errors than there are of code that I actually wrote, often which is written pretty arcanely, again, for programmers who've been writing code for 10, 20 years. But there are some details that pop out. So notice the problem is definitely with hello.c.

So great, it is my fault. This syntax here means that line 5 is the problem. And this next 5 means character 5. So you can literally triangulate your bug, your mistake by going to line 5, and then over five, and it's somewhere in that area. Specifically, " the area is use of undeclared identifier string. Did you mean standard in ? I don't think I did. Like I do want string, and then there's some other complexity here. But what's important here is not the specifics of this error but really the implication that it doesn't recognize the word " string " or " get_string ". Now, why might this be ? Because we are using get_string, which I claimed is a CS50 thing that we'll use for a few weeks. C does not know about it out of the box, so to speak, I have to teach the compiler that get_string exists, just like I taught the compiler that printf exists by including the appropriate header file.

And in this case, quite simply, it's called include CS50.h. That now teaches the compiler, oh, someone else wrote this function already, get_string, and with it this type of variable called " string ". So now if I go back to my terminal window and rerun the exact same command, " make hello " - - maybe crossing my fingers - -

now nothing in fact goes wrong because the compiler has been brought up to speed with all of the functionality it needs.

And now if I do ./hello, Enter, there it is, what's my name ? And notice the cursor is one space over just because I thought that looked prettier than having the cursor right, next to the question mark.

D - A - V - I - D as my input, and Enter. And " hello, David ".

相关推荐
iCxhust2 小时前
C#程序,窗体1向窗体2的textbox控件写入字符串“hello”
开发语言·c#
椰羊~王小美2 小时前
嵌入式 和 单片机
java·单片机·嵌入式硬件
低客的黑调2 小时前
Redis-不止是缓存
java·开发语言·数据库
花间相见2 小时前
【大模型微调与部署02】—— ms-swift 自定义数据集完全教程:格式、dataset_info 配置、多格式兼容实战
开发语言·ssh·swift
噢,我明白了2 小时前
Java 入门,详解List,Map集合使用
java·list·map
Hello--_--World2 小时前
JS:闭包、函数柯里化、工厂函数、偏函数、立即执行函数 相关知识点与面试题
开发语言·javascript·ecmascript
ZenosDoron2 小时前
函数形参传数组
java·jvm·算法
一只幸运猫.2 小时前
字节跳动Java大厂面试版
java·开发语言·面试
xier_ran2 小时前
【C++】“内部”、“外部”、“派生类”、“友元“类
java·开发语言·c++