C Lesson 1 (part 2)
Welcome to part 2 of C lesson 1. So now, say if we wanted to print this week, we could use a print function to put it on the console. Let's say we want to assign it something, so name one. We do space equals space and then single quote because whenever we're making a care, we do single quotes.
And I'll make it a. So the second character of name is going to equal a. So the name character is going to equal nothing, a, and then 510 more nothings. So we, if we want to assign something, they type in to be multiple characters in this name. That seems very complicated at first, so let's make it easier.
There's a function I think it's in string called fgets
. So we do fgets(
left bracket name semicolon space then the maximum that we can get 512
semicolon space stdin
left parenthesis semicolon. So standard input is yet another file stdin
, and it's only read, so we can only read to it, and that's what they type in in the terminal.
So that's a file we can read from anytime to pop them to give us input. So right now we're getting 512 bytes from the console, shoving it into name from standard input. So right here, before we do this, I'm going to say printf("name please")
.
Okay, so there's not going to be any enter after this, so they'll take their name right here, say they took Joe. Then this character array name, once they're done typing that in and they've hit enter, will equal Joe.
So now say we want to put out "hello space" and then their name. We get rid of this world then and replace it with a %s
. What the heck is a %s
? Magic values with printf
and other printf
like functions. %s
means that you are promising this thing that now we're going to give it a string to put where the %s
is.
Say we do present some other letter other than s, it'll probably be another code that it expects, another type of data like a number or something similar to that. %s
is a character array, %c
is one character, etc. So we're going to do %s
. So now, how do we give it the string to replace %s
with? We do after both quotes, we do comma space name.
Now say we have two %s
because you wanted to print something else at, so the second %s
would be the second thing right here, so they go in order. Now say you have three %s
but only two strings here, that would be totally messed up. So you had two strings here, but you had no %s
, that would be messed up, so you don't want to mess anything up.
So right now, it's going to print "hello space" and then whatever they entered in the terminal and then a new line. It gets back \n
and is the equivalent of a new line. Okay, so say that's good.
So now when we build and go this, and I'm going to tell you about that terminal emulator built into Xcode. Now, when it's running, you can go up to run console, so console is like the terminal app built in. So whenever I've run, it will run in this terminal.
You see this is from the original run I ran, and it said "hello world." Now it's asking me to enter my name because we typed it there, so I'll type Alex and you're going to get a little surprised here right now.
So don't think I'm not ready. "Hello Alex!" New line! And then another new line! Are there two new lines here? Why is there a new line after my name? I just do %s
right in there, should be no new line right here. It's not like I put this here. fgets
adds a new line to whatever someone typed, because it stops getting stuff from the console after they've typed it in and hit enter, but before it stops, it records the enter.
So we have to use something called an if statement to check if the last letter that equals something of name is a new line, and if it is, we're going to make it nothing. But before we do that, I'm going to clear something else up with you. fgets
, aside this thing called 512
, that's the maximum they can get in, so it only puts the first 512
letters that they've entered into name.
Now the cool thing about that is that if we did 513
here and they entered 513
characters, even though this character array name is only 512
, it would probably cause a big problem in the memory of your computer. Now, they've figured out how to fix stuff like that. It's called stack smashing that they figured out how to get rid of, but it is possible to overwrite other places in memory or at least it used to be, which is why fgets
is a very good function, and I'm glad we're using it right now.
So back to checking if the last letter of that thing is backslash n
or new line, and if it is, we're going to get rid of it. Right here, we do if
left parenthesis name less than. So remember we're trying to access a character of name. You can do left bracket a number or you know left bracket a number right bracket.
But how do we know what the length of name is? And then how do we like subtract one from that to get the last letter? Because that's how it works. We do strlen(left parenthesis name right parenthesis)
, and it will return the length of the string. So now say the string is five letters long, and we get that and it'll be five here.
Remember how it starts out at zero, so to get the last letter we actually want four. So we do strlen(name) - 1
. So right here we have name and then the length of name minus one, so we're going to access the last character that is something in name.
So now we do space equals equals. When you're comparing two things, you do double equals, and when you're assigning a value to something, you do single equals. But since we're doing double equals, we do equals equals space single quote. So that's like an apostrophe \n
and another single quote.
So if this was a string, we do regular quotes, but since it's a character, we do single quotes. So that's good. So now we do um left parenthesis, so we're seeing if the last letter of name is a new line. If it is, we're going to use more curly braces.
Everything between this and this only happens if this equals this. You see, that's the beauty of an if statement; there is no if statement in assembler, so you should be glad you have that. So inside of this in this, we're going to type our code, and you don't have to use indentation like I'm doing with the tabs, but it's nicer.
So now we're going to, instead of comparing this to something or going to assign it to something, we're going to do the exact same code. strlen(name) - 1 == '\0';
so 0
is nothing, it's a null bit nothing.
And since we're assigning the last letter of name to be that null bit, we're cutting off the new line from the end of the name and changing it to be nothing. So now that we've done that, we'll save it, build, and go.
And we'll go up to run console. So now it's asking me for my name again because we've run it again. So I'll type Alex, hit enter, and it says "hello Alex" with no new line, unlike this behavior we got before, before we did this if statement.
So that's the first C lesson. I'm sure you have plenty of questions, so ask them. I will try to respond to all your questions as soon as possible if you don't get it.
So thanks for watching! Bad kids! And try this out! So this is really officially our first C lesson. So goodbye.