YTread Logo
YTread Logo

C Programming Tutorial for Beginners

Apr 22, 2020
Hello, welcome to Giraffe Academy. My name is Mike. In this course I will teach you everything you need to know to get started in the C

programming

language. C is an amazing

programming

language and in fact, it is one of the oldest programming languages ​​in existence. In fact, many modern programming languages ​​are based on C, so it is a very good idea if you want to get into C or even if you are trying to get into something like C++ to learn the basics and in this course, we will cover everything that they need to know.
c programming tutorial for beginners
I will talk to you about installing a text editor and using the C compiler and we will write some basic code. I'll talk to you about, you know, what a program is. and how the program works and how it works C. Read the instructions that you give it and we'll get into more advanced things. We'll look at things like if statements and loops. We will create different variables. talk about the different data types you can use and C, then we'll go further. We'll talk about things like structures and functions. We're going to talk about tips and basically I'm just going to give you a complete overview of all the basic concepts in C.
c programming tutorial for beginners

More Interesting Facts About,

c programming tutorial for beginners...

By the end of this course, you will have a very good understanding and a very good foundation. You can go ahead and learn a little more. I'm very excited to be able to offer you this basic course on C. I'm very excited for you to dive in and start using these

tutorial

s. So feel free to click. Check out all the videos and hopefully you'll learn something awesome about C. In this

tutorial

, I'm going to talk to you about how to set everything up to start programming in C now so that you can program in C. Actually, we're going to need two things First thing we're going to do What we need is an environment where we can write our programs in C.
c programming tutorial for beginners
Now there are many of these different environments. Essentially. All you need is just a text editor, so anything you can know that allows you to type text and then save it in a specific file format will work, although when we work with C it can often be useful. Use a special environment called IDE (IDE stands for integrated development environment). Basically, this is a special text editor that makes it much easier for us to write our programs in C. So I'm going to show you how to install an IDE called code blocks. The second thing we need to write our programs in C is to be something called a C compiler now C is a programming language.
c programming tutorial for beginners
It means that we can basically write the instructions that we want to give to the computer, but eventually, in order for the computer to execute those instructions, they have to be compiled, which basically means like translated or transformed into a language that the computer can understand. Basically, we will download and install a special program that will do it for us. So let's get started. I'm going to go to my web browser and go up to the Google search bar. And I'm going to look for a program called code block. So just type C Code Blocks. And this link should appear.
It's code blocks org now. This is an integrated development environment. It is basically a text editor that will make it much easier for us to write our programs in C. So I will go down here to downloads and there are a lot of options here, one says download the binary or at least download the source code. We want to click download the binary version. This will be the easiest option. If you are on Windows, Linux or Mac, you can install it from this page. Depending on what operating system you are on, you will want to click on it.
I'm on Windows. So I'm going to go down here to the Windows option. You will see that there are small options for us to download this material. So there are a lot of options here. What we wanted to download will be this one here. It says code blocks And then the version number and then it says min GW - configure Basically what this is is it will allow us to install code blocks. So we will be able to install that IDE program, and we will also be able to install AC Compiler. So remember I said we needed those two programs.
We needed those two things and this is actually going to give us both on Windows. So I'm going to come here and click on the link to download this from sourceforge.net. And you can Look, he went ahead and downloaded it. So now let's go to our downloads folder and I'll double click on this installer. This should open a window where we can configure the program and let's just click on it. I'll just click Next. I'll accept the license and basically leave all the default options and you should be good to go. When the installation is done, it will ask us to run code blocks so we can go ahead and do it. that and it's giving me this little window here.
It says automatic compiler detection. So you can click on the one up here. That's highlighted It says GNU GCC compiler and I'll just click Set as Default and click OK and that's it. Now we have code blocks installed, so in the next tutorials we will learn how to configure code blocks by writing the first one. Programs in C and just getting started with C. But for now, you have everything you need to get started. In this tutorial, I'm going to talk to you about how to set everything up to start programming in C on OS things.
Basically, the first thing we will need is a text editor. We will use the text editor to write our C programs and you can use any text editor in which you want to write C programs, but there is actually a special type of text. editor which is called IDE and it means integrated development environment and it is basically a special environment where we can write, run and manage our C programs. That is the first thing and the second thing we will need is what is called C. The compiler now C is a programming language. So, essentially, what we do when we use it is write instructions for the computer, but in C.
We are writing instructions that humans can understand and work with, but in order for the computer to be able to carry them out they have to be compiled, translated or transformed. into a language that the computer can understand and that's what the C compiler will do. It will take our C programs and essentially translate them into a language that the computer can understand. So the first thing I'm going to do is show you how to install that compiler and then we will get a text editor. So what you want to do on your Mac is go to the search bar and it will be I'll just type terminal and click enter and a window should appear like this.
The terminal is basically a program that allows us to interact with the computer using text commands. So we're actually going to need to use the terminal to be able to do what we want, so the first thing we want to do is check to see if you might already have the AC compiler installed, so in certain circumstances you might already have one. So you just want to type C C - V and click enter and you'll see here. I have the AC compiler installed. So I'm getting all this information. You can see here He says as if Lang and he's giving me, you know, a lot of information.
If you have that, then you have your C compiler installed and you are ready to go. But if you don't have it, I'm going to show you how to get it. So all you want to do is just type Xcode select and then space -- install and click enter and you'll see I'm getting an error here because I already have these commands. line tools installed But if you don't have them installed yet and obviously then you wouldn't have that C compiler, then this will kick in and install everything. You need the C compiler to work so once you run the Xcode select command just type CC - V again and you should get this version number.
Okay, now that we have our C compiler installed, what we want to do is download an IDE. So we want to download a special text editor that we can use to write our programs in C, so I'm going to go here to my web browser and I'm here in a website called code blocks dot org, and this is an IDE called code blocks. It is one of the most popular free IDEs for programming and C. So we will go down here to this downloads link and there should be some options here to download the released binary and download the source code.
We'll click on download the binary version and you'll see down here. We have a bunch of options for Windows Linux and Mac, click on the Mac option and if we go down here, you'll see that this is what we need to install the code. blocks on Mac So if you go to the right side here, there's a link to SourceForge where we can download it. So I just clicked on that link and it should start downloading automatically when it's done downloading, let's go to our downloads folder and you'll see here we have our zip file so I'll double click on it and now they should give us code blocks.
All you want to do is simply drag and drop code blocks into your Applications folder and you'll be ready to go. So now you have the code blocks installed and ready to start following this course and writing some amazing C programs. In this tutorial, I'm going to talk to you about how to set up our first C file. So we're going to load a C file and we can get everything working and test our program, so I'm going to open up my code block program and this is the IDE that I'm going to use during the rest of this course.
So we'll just double click on it and it should start opening once we have the code blocks open. You'll see here there are a lot of different options, like creating a new project, opening an existing project. So we're actually going to want to create a new project. So whenever we start writing some C files, we're going to want to create a new project in code blocks, so you can click this button right here, or you can go to archive new projects and, either way, It will take you to this window here and basically there will be a bunch of options here.
These are simply different types of C projects that we could create. What we are going to do is create a console application. This is like there is a basic C project that you can run on your computer. That's exactly what we need. So click on the console app and then click Go and I'll click on this wizard and you'll see here. It says C++ or C now C and C++ are different programming languages ​​for this course, we're going to work with C. So just highlight C and click Next and we'll give it a title that I'll call my draft. and we're going to have to put this project inside a folder, so I'm going to open it up and put it on my desktop.
So let's click OK and click Next and you can leave all of these options as default and we'll click finish, so now what we should have is our first C project in code blocks now here inside this little File Explorer , as you will notice. That Draf is here. That was the name of the project I just created. Here. We have this source folder. So I'll click down and you'll see that we have this file here called main point C. And this is a file that was automatically created for us using code blocks. I'll just right click and open. that and you will see it here.
We have default code, so up here are these little lines of code that say include down here. This says int main, so this is the program we get by default when we create our C project and it's kind of the simplest C program you can. Right, essentially what this program does is print hello world to the screen. So to run this program and test it just to make sure everything is working, I'm going to come up here and you'll see there's a green play button here and it just says run when I pass the cursor over it when you click this button.
This file that we have open here, in the main view, will be executed. Basically, we are telling the code blocks to run this file and run the program. So when I click the Run button, we get this message. It says the project has not been created yet, do you want to create it? Yes, and now this little window appears. Basically, this is the command prompt. So whenever I run a C program, this little window will appear. So if I exit this and run this program again, you'll see this window appear again. And basically it just runs the program and in the case of this program, all we do is print Hello world to the screen and that's actually what this instruction here does.
When we open this command prompt. It's essentially just running the program and printing what we told it to print. So printing hello world here and printing something on the screen is really easy. And that is an instruction that we can give to the computer. But as we go through this course we will learn all kinds of instructions that we can use, but the purpose of this tutorial was just to set up our C project and test our main C dot file. As long as everything works here and you. We are ready and now we can start to really learn how to program in C.
In this tutorial, I will show youI'll talk about the basics of writing a C program. So if you've been following the course up to this point, we've installed a text editor. I installed a C compiler and set up our first program in C. And our first project in C Within code blocks. Basically what we have now is this file here. This is the main dot C file. I just want to point out a couple of things that are here first. We have these little instructions called just include and I'm not going to go too much into what they do in this tutorial later in the course.
We're going to talk about what they're actually doing and the details of how they're helping our program, but for now just know that in order for us to use our program, we're going to need to have these guys active. The next thing we have is this little block of code called main and it just has this name main. You can see that there is an open and closed parenthesis. And this is actually what's called a method and don't worry too much about what methods we are. Again we will talk more about this in the future but essentially a method is like a container where we can put some of our code and this main method is very important because this is the method that will be executed when we run our program so that in C we can write a bunch of instructions to the computer and then we can run our program so we can tell the computer to run those programs and every time the computer runs our C program, it will look inside this main file. method in other words that will search within all the code within these opening and closing curly braces and execute that code, so I have this statement here.
It's called printf and basically this is just printing some text on the screen when we run our program it will go into this main method and execute all the instructions inside here i.e. this printf method. So if I were to run my program, then this will work now. Let's talk about running a program every time we want to run a program that we wrote in C. We actually have to do two things. The first thing we have to do is what is called building a program or compiling a program. And basically what it does is take all this C code and translate it into a language that the computer will be able to understand and execute.
So essentially it's taking this C program code, translating it into computer code and then the computer can run it and run our program. So the first thing we have to do is always build our C file. The second thing we have to do is run the file that is compiled, so that once we have created this program, we have compiled it into code that the computer can understand that we have to execute it, we basically tell the computer to execute that code and in blocks of code. It's actually very easy to do this. So we can come over here and this little gear icon will allow us to build our program and then this play button will allow us to run our program, but a lot of times when you're programming, you'll want to build and run your code at the same time, right? you'll just want to compile it and run it just to see what happened, you know?
See how your program did. We have this option here called compile and run and this will compile your program and run it at the same time. Typically, as you progress through this course, you will want to use this option. Those are the compile and run options. When I click on this, it will essentially compile my program, run it, and then appear here in this little window. This little window is basically a Console window that you can call. So a lot of people would refer to this as the console and it will just output whatever information we tell it to, so if we write a C program that generates no information that doesn't print anything to the screen, then you wouldn't actually see this, but since we're using this printf command, that window will appear.
So let's talk about how these programs essentially run when we write a program. We have a set of instructions. So when I'm writing. In a C program, I'm basically writing a set of instructions. Imagine you were writing a recipe or something. Right, a recipe is essentially just a set of instructions that, when executed correctly, will result in cooking or baking something delicious, right? And that's basically what these C programs are. It's a set of instructions that, when executed correctly, will make the computer do something for us and one of the cool things about computers is that we can program them to do anything. and C is one of the best. languages ​​to do that, so let's look at this instruction here.
It's called print F and this prints something on the screen. We'll note it here at the end. We have this semicolon and this semicolon is really important. So whenever we write a statement in C, we always want to end it with a semicolon. That tells C that we are done with that statement and want to move on to the next one. So you could copy this instruction and paste it here. Now we're basically going to run this statement twice, so if you were to run and compile my program, you'll see that we're not just going to print it. once, but we will also print it twice.
So when we run our program, the program drops down to this main method and looks at all the instructions in it. So start with this first instruction. You run it, right? So it prints hello world on the screen and then it also prints a new line. So this backslash n is like a new line character and will move the text to a new line. When it is done with this statement, it will move to the next statement and execute that statement. So as we progress and learn to write in C, we will learn more and more complex instructions and we will be able to use all of those instructions in conjunction with each other to specify some very complex programs, but for now this is really the only instruction that we know is printf , so why don't we use this printf?
Instructions for drawing a small shape on the screen. So I'm going to show you how we can draw a small triangle on the screen. I'm going to take this printf and actually get rid of it. Hello world, I'm going to copy this a couple of times, so I'm going to copy and paste it here, maybe four times. What I can do is specify instructions for the computer that will tell it to draw a particular shape. So down here I could make a diagonal slash and over here we'll do another one and I'll do another one and I'll do another one and you can see here that I've drawn like a little diagonal line. with these diagonal bars.
Now, I'm going to draw vertical bars that go all the way down. So I'm going to put one here. I'm going to put one here. I'll put one here and finally, why don't we draw some underscores and then we'll put one in. Basically, I'm drawing a little triangle shape and these backslash ends just print a new line. So every time I print this, I'm going to move it to a new line. So it will print this type now on the line below the line below. So if I were to save my program and run it now, the computer will check it.
We are going to execute each of these instructions in order and we can print the shape on the screen. So let's do what you see here. We get our shape and one thing I really want to emphasize. Does that order matter? So if I took this instruction and placed it here as the first instruction now, this will print first. So when I run my program, you'll see that we're printing this kind of Funky looking shape and that's because it changed the order of the instructions. So the computer changed the order in which it executed those instructions and you'll notice here again that we're using this dot and comma after each line of code.
That's kind of the basics of writing a program. That's essentially all you need to know. When we write a program, we specify a list of instructions that we want the computer to carry out and depending on what instructions we give the computer and what order we give them and how complex the instructions are. That will determine what the program does and as we progress through this course we will learn more and more complex instructions and we will also learn how we can take simple instructions. like printf and Do different things with them to make our programs much more powerful?
In this tutorial I want to talk to you about variables in C. Now when we use the C programming language many times, we will be dealing with data, so there will be different data values ​​or different information that we want to keep track of and use within our programs and Sometimes it can be difficult to maintain and keep track of all that data and that's where the variables come in. A variable is essentially a container where we can store different pieces of information, so different data values. We could sort things like numbers, texts or characters, and these variables make it much easier for us to track and manage all the data in our program.
So I'm going to show you basically how variables can be useful and how we can use them in our C programs But down here. I have a very basic program. I'm basically printing a little story. He says there once was a man named George. He was 70 years old. He really liked the name George, but he didn't like being 70 years old. So this is a simple program. We just print a bunch of different lines of text and then when I run this program, you'll see that we print the actual story. Here we have our history and this is great.
But let's say I'm reading my story. I'm reading my show and I'm thinking, hmmm, I think I want to change the character's name. So instead of calling the character George, why don't we name him John to make that change? I'm going to have to go through my entire show and find every place where we mention the character's name. So here's one, change it to John, remember, keep looking and here's another one. So we'll change this to John, right? So I had to go through and manually change every instance of the character's name to John and let's put it right.
That sounds pretty good, but maybe I'm thinking I want to make the character a little younger. So instead of 70, why don't we put it 35? so now again I will have to go through my entire program, find all the places where we mention the character's age and change it to 35. So there you have it. I changed the program, updated it and modified it now. If I were to run this program then all that information will be updated and we will have a new story. Here's the problem, although I'm dealing with a very short story. I mean, it's four lines.
We only mention the character's age and name twice. But imagine if I were writing a huge C program that had hundreds of lines in this story. So the story was hundreds of lines long and we might mention the character's name hundreds of times. times. His age is mentioned hundreds of times. Well, if I wanted to change the age of the characters, your name? You would have to go through it and change it manually at each location, which would take forever and be a huge follow-up. Basically, that's not a good way for us to manage and maintain data and our programs.
Very good. We basically have two pieces of information that we are consistently working with in this show: the age of the characters and the name of the character. What I want to show you now is how we can use variables to better track and manage this data. Remember that a variable is basically just a container where we can store some data, so I want to create two variables that will store the name of the character and the age of the character and I'll show you how this can make our program much easier to manage. here above these printed statements, I'm just going to make a new line and come over here and I want to create a variable.
Now, every time we create a variable in C, we have to give C a couple of data. Basically we have to tell C a couple of different things. The first thing we have to tell C is what type of information we want this variable to store. Now I'll get into the different types of data we can represent and look at more in the next one. tutorial, but for now just know that there are a lot of different types of data that we can store and C we can store things like numbers, characters, we can store text, we can store decimal numbers, all kinds of things like that in our case. to store the character name and the character name is basically just a collection of characters.
So to create this variable, the first thing I have to do is tell C that I want to create a variable that will store characters. So to do that, I'm going to type char just like that and that will basically tell C. Hey, we're going to store characters inside this variable. After typing char, I want to type the name of the variable I want to create. Remember about variables that they contain and it is a good idea to give these containers descriptive names and those names will basically let us know what that variable stores inside of it.
So I'm going to callto this character by name and now what I want to do is basically I want to store multiple characters, so here I tell you see I want to store one character. But in C we can also store instead of just By storing as a single character, we can store a bunch of characters and that would be like you know a name, so there's like four characters here to store a bunch of characters inside this variable after writing the name of the character Abel. We'll need to make an open and close bracket And that will tell C that we want to store a bunch of characters inside this variable.
So I'm going to type char a character name, open and close brackets. I'm going to set this equal to something. Basically, I'm going to assign a value to this variable and I'm just going to put open and closed quotes and inside here we're going to write the value that we want to store. So in my case, I'm just going to store the character name, which is John and then, as always, in C. We'll have to end this with a semicolon. Well, essentially what I did here was I created a variable and I told C what I wanted to store in the variable that I wanted to store a character.
I gave this variable a name. It was the name of the character and I used these opening and closing brackets to sell to C that I wanted to store a bunch of characters. Not just one. Now what we can do is create another variable to store the character's age. So in this case, we're going to want to do something similar, but we're going to want to store a number. There are many different ways. which we can store numbers and see, but for our purposes we're storing a name so we're going to use something called an integer and an integer is basically an integer so I'm going to write int and that basically means integer.
And now we're going to give it a name, so I'm going to call it Character Age and set it equal to a number. So in our story, the character is 35. So I'm going to write 35 and now again I can end this with a semicolon. So I have two variables here. This one is storing a collection of characters. This one is storing an integer. Now the question is how can we use the inside of our program? Well, basically what we can do is print them along with this text and we can use this print F statement to do that, so I'm going to show you how to do this and then we'll talk about Basically, I'm going to go here where we have the character name.
I'm going to delete this and replace it with % and an S and now over here I'm going to make a comma and I'm just going to type the name of the variable. So I'm going to type in the character name and basically what happens is when we use these percentages, we tell you that inside this text string here we're going to want to insert another string and a string is basically just a collection of characters to be able to call to this here as a string variable. So when I say %s, this is basically a placeholder and I tell C that I'm going to insert a string here and here I use this comma and then I type.
Take out the string that I want to include, so in our case it's the character name and basically here it will say that there once was a man named %s and it will replace %s with whatever we specify here. Then he will replace %s with the value that is stored inside the character name variable, so I'll save this and we'll run our program. Now you will see that it is still printing. Once there was a man named John. Although we don't really write. John over here. We just wrote % ass in the character name so we can do this in this other place as well.
So down here we are also using the character name so I can type %s and here we can type the character name over and over again. This will act as a placeholder for this character string that we specify here. Let's do the same thing with age, so here we have age 35. I'm going to do something similar and say percentage. but now since I have a number instead of using percentages, I'm going to use percent D and that basically means we're going to insert a whole number here. So I say I had the D percentage years and here.
I'm going to do and I'm just going to type in the name of the variable that I want to be placed inside of here, so I'm just going to have the character's age and we're going to do this one more time down here. . I'm going to type in the D percentage and the character's age, so essentially what I'm doing is printing the value. That's within the age of the character where I specify these placeholders, so let's go ahead and run our program and now you'll see it will look exactly the same as before it's the same story, the same name and the same age Except I didn't have to type the age of the character and the name of the character within the story, I just referred to the variable and what's cool. about?
Variables: I could come up here and let's say I wanted to modify the age of the character. All you would have to do is modify it here so we can change the name of the character to like Tom and we can also change the age of him to like him. 67 And now when I run my program, all of that will be updated in history, so it will now use the name Tom and the age of 67 in our case. We only mention the character's name and the character's age a couple of times. But can you imagine if I mentioned this variable ten, twenty or thirty times.
Having to change it at each of those individual points would be extremely difficult. So variables are great because we can store the value once and then if we want to modify it, we just modify it in one place. Another interesting thing we can do with these variables is that we can modify them. Let's say halfway through the story. I wanted to change the age of the character. In fact, you could come down here and make a new line right above these printed statements. I could give this variable a new value so I can tell the age of the character and I can just set it equal to something else so we can set it equal to 30 and again, we want to include that semicolon.
So now we're halfway through the story. The character's age is actually going to change. So you'll see it says that once there was a man named Tom, he was 67 years old and then down here. The age is 30, so we modify the value that was stored inside the variable by half. print our story and that's really why variables are useful. So, that's kind of a basic overview and introduction to what variables are, why they're useful, how we can use them. And here again, we are storing a collection of characters, which is called a string and we are also storing this integer.
But there are other types of data that we can also represent. So besides representing as a string or a number, we can also do other things. And I'm going to talk about that. in the next video But for now, this has just been a basic overview and introduction to variables and see In this tutorial I want to talk to you about data types in C and in the C programming language. We are going to deal with a lot of data So in general, when you write programs, you will have to deal with all types of information and in this tutorial I just want to talk to you about what types of information we can represent and use within our C program. and these are called data types.
Very good. So what are the different types of data we can work with in our program? So I'm going to show you all of those different types of data right now. Many times in C, when we work with data, we will store it inside variables, so I'll just show you. A bunch of different variables we can create and all the different types of data we can store inside those variables? so remember every time we create a variable and C we have to tell C a couple of data and the first information we need to tell C is what type of data we want to store inside that container inside that variable so Talk about the different types of data.
One of the most basic data types and probably one of the data types you will use the most. They will be the numbers. There are actually two very important types of numbers that we can represent in C. So the first type is called an integer and an integer is basically an integer, so it is a number to count, if you were counting on your fingers. 1 2 3 4 5 I'm just counting whole numbers and I'm not saying like 1 1 and a half to 2.3 like I'm counting whole numbers and that's what a whole number is. So to create an integer we could just say int and we'll give this a name so we can say like age and we can set the age equal to 40 or something like that.
Now when we want to use an integer in C. We always just want to write the number. So you'll notice I'm not putting quotes. marks or parentheses or anything around this. I'm just writing the number like this. And now that we have our age variable, another type of number that we can create is a decimal number, so it would be a number other than an integer. So it would be like 2.5 or eight point six seven or seven point five three eight, right? It is a number that has a decimal point associated with it. So there are actually two types of decimal numbers in C, there's something called double and something called float, and I'm not going to go too much into the difference between the two, but as a beginner, to see for the most part.
We'll just want to use a double. So I'm going to type double and this could be like someone's GPA. For example, it could be like 3.7 or whatever, you know, you can use double to represent decimal numbers. It's also important to note that you could also represent as the number three, so you could just say like 3.0 and that will be a decimal or you could say 40 O points. So the difference between 40 and 40 O points is that forty O points is a number. double It's a decimal number and 40 without the decimal point is an integer So that's something you need to keep in mind so you can Make this like three point six So those are the two basic types of integers and doubles and they will be useful all the time. time Now the next type of data that we're going to want to store or will be characters So in C we can store individual characters inside a variable, so you could just say CH AR and that means character and we could give it a value.
So maybe it would be like someone's rating and when we create a character in C we basically go to specify the character that we want to store in single quotes. So inside these single quotes, I could put a letter, I could put it as lowercase, we could put h C d AF, basically, you can put any character you want. to store So if I put an A here now, this degree character variable will store the single character inside it. It's important to note that you can only store one character, so you couldn't put a bunch of characters in here.
And that will actually throw an error. All we can do is store a single character, but these three are the basic data types in C. These are the basic types of information that we can handle in our C programs and just using these three data. types, we can represent all types of information. I also want to talk to you about an extra data type and technically it's not a data type in C. Technically it's like a modification of the character data type. So you'll notice when we create a character, we can only store one letter, a single character within it.
But many times we want to represent more than one character so we want to represent as a string of characters and generally this is called a string so it is a collection of characters that are grouped together and we can use this data type char to create a string, so basically I can say char. I can type the name of the string that I want to create so I can say as a phrase and then after this I can do open and close brackets and this will basically say that we want to put a bunch of characters together and what we're actually doing is creating something called array and I will get into arrays later in the course But for now know that we can create a string of characters using char Specifying the name of these opening and closing brackets, and then we can create this using opening and closing quotes So instead of these single quotes here We're just going to use double quotes and you could type as a phrase so you can type any string of text that you want here and this will be stored inside this variable which, like I said, is technically now called an array.
It's important to note that doing something like this or creating this string is a little different than what these guys here do. So, like these variables here, we could modify them. We can give them new values, but you can't do things like that with this. So you should know that this is like a special type of data and it's going to act a little bit different than this one up here. But that's one of the basic concepts of working with data and those are all the basic data types that we're going to use. to use in our C programs.
In this tutorial I want to tell you a little more about the print F function, so if you have followed this tutorial, we have been using a statement and our C programs called Printf and printf. It basically allows us to print different things on the screen in this tutorial. We will talk morea special start and end tag and any text, any code, anything you put inside that tag will not be executed or really examined by C. So we can use comments to do that. all kinds of things, you know. You can leave little notes or little comments within your program.
You can use comments to temporarily disable certain lines of code. They can be really useful. So I'm going to show you the basics here in my program. If I want to create a comment, I can start a comment using a slash and an asterisk and you'll see when I put these asterisks. All the code in my text editor changed color and basically what this means is that everything that comes after this forward slash and Asterix will be considered a comment to close the comment. I can use another asterisk and another forward slash. You will see that the code returns to normal code.
Inside these two asterisks I can basically write whatever I want and it will be considered a comment, for example, if you know. I wrote like my program and ran my program. C will ignore this whole thing. So I'm just going to print comments or fun. He's not going to do anything else and you know. Like I said, comments are really helpful, so I might leave a little note up here. I could write something like do something and many times. People use comments to explain certain lines of code, so you could put a comment here and you could say like this print text, right?
You might know that this was like a really complex line of code. We could use this text to explain it. You can also use comments to do something called commenting a line of code. So for example, let's say I want to run my program but I didn't want to have this line of code there. Well, one thing you could do would be to just delete the entire line of code. So I could delete this code and now I can run my program and it won't work anymore. print it. It won't be of any use anymore. But here's the problem with being able to do that.
I actually had to delete the code so that instead of deleting it, I could bring it back and comment it out to basically surround it with these comment tags and now the computer will just ignore it. So it won't render it because it's been commented out. Now when I run my program it will do the same thing. It won't execute that line of code. But I didn't have to delete the line. I could just comment on it. Many times when you write programs, you may want to try to run your program without a certain line or a certain series of lines of code that you know, and instead of removing them and then running the program. you can just comment on them and View will basically ignore them, so it can be really useful.
Those are the two basic uses of comments and essentially comments are open, right? It is just a way of writing information to our files. C will not process it. So you can really use comments to do whatever you want. Now I will say that one of the best practices with comments is to use them sparingly. Generally, you only want to use a comment when absolutely necessary, so we It can be really annoying if you open a C file and there are comments scattered everywhere and obviously if you want to put a bunch of comments in your code, I mean no one it'll stop you, but as a best practice in the future, I think generally only using comments when they're absolutely necessary is kind of a good practice.
But again, it's open, you know, you can use comments for whatever you want. In this tutorial I want to talk to you about constants in C. Well, constant is special. type of variable and C that cannot be modified So when I create a constant and C, I am basically creating a value that cannot be modified. So I'm going to show you basically how this works. Let's say I created a variable. Let's say I created an integer and I just called it num and set it equal to 5, and then I printed that integer to the screen so we can print this percentage D and we're just.
I will do pranaam well so that I can create this numerical variable. I can print it on the screen. Let's say down here I wanted to modify num, so basically I could say that num is equal to 8 so I can modify the value that is stored inside the variable num. And let's go down here and Print this too. So d And we will print num again. And I'm actually going to print a new line here. So when I go ahead and run this program and I forgot to send myself: When I go ahead and run this program, you'll see that we're printing five and then modifying. the value to eight and then we print eight, so I was able to modify this value down here.
I was Mabel to modify number. But in certain circumstances, you will want to create variables or create values ​​that cannot be modified. So, these would be values ​​that just like cannot be changed and are called constants. So, if I wanted to make a number a constant, in other words, let's say that for some reason I don't want the variable num to be able to be modified in my program. I could come here and I could say Const then Co NS T and I'll say it right before declaring the type now, you can also say Const right after declaring the type, but I've always preferred to put it before.
So I say Const int num 5 and now this program will throw an error. So when I try to run this, you'll see it down here. It is highlighted in red. That's because I can't modify a constant. So I'm trying to modify a number down here. But I can't because I gave this keyword Const because now it is considered a constant. So in many cases you will want to have variables that simply cannot be modified. It cannot be changed and in a situation like that you also want to convert them to consonants many times when we create constant variables in C.
We will give all the names in upper case and this is not necessary. This is kind of what a lot of developers will do, so they'll say like instead of n Um like this, they'd say like num or maybe this is like your favorite number, so you could say fav underscore num. That would be how you could distinguish two words, but In general, the constants will be all capital letters and that's so, it's pretty obvious that they are immutable, that they are constants. But again, you don't have to do that. But that's kind of a best practice, so this is a way that we can create a constant, and we basically create a constant variable that can't be changed, but that a constant could also refer to.
Like any number or any text that we're using in our program. So, for example, I'm going to get rid of this if I came here and just printed some text. So let's say I just printed as Hello. Actually, this will also be considered a constant. So, it's like a character string, a text string. That is also considered a constant because it is like text on the screen. We can't change it. We cannot modify it. It's there, so you would also consider it a constant if you were printing a number, let's say I came here and printed 77 T is also considered a constant.
So it's just considered as a piece of information or a piece of data in our program. That is something that does not change. Good? So, 70, this is not going to change, I mean, unless I physically come in here and change it like this. This value will always be 90 no matter what you can't modify it unless you want to go in and modify it manually. So that is also considered a constant But I would say for the most part this will be useful obviously doing things like this But creating constant variables variables that cannot change In this tutorial I will show you how to get information from a user in C Many times in our C program we are going to work with all different types of information and many times we are going to want to get some of that information from the user, so I am going to show you how we can ask the user to enter certain information.
We can take that information, store it inside variables, and then we can use it within our program. So this will be great. So down here I'm basically going to write the code for this and the first thing we want to do every time we get input from the user is ask them. So I want to ask the user, you know, what to enter. So I'll just say printf and here. I'm just going to print a message. So why don't we ask the user to enter his age? Well, I'm going to show you how we can get a number from the user, so I'm just going to say enter his age and now that we've asked you to enter his age, I need to do two things.
So, the first thing. What I want to do is create a variable where we can store the input that the user enters into the program. So I want to create a variable where we can store the age that the user enters. So I'll create an inn here. Oh, it's called age. And I'm not going to give this a value, so all I'm going to do here is just declare the variable. I'll just tell you that I want to use this variable, but in other words I won't give it a value. I'm going to allow the user who enters the age to give it a value.
So down here I want to be able to get information from the user so I can use another function called scanf and scanf will basically allow the user to enter certain information into our program and this works similar to printf, it's kind of like doing the opposite of printf right, printf is printing something on the screen Scanf allows the user to enter something into the program and we're going to do an opening and I close the quotes and here I basically want to say what type of information I'm asking the user to enter. So in our case, we are asking for an integer.
The correct age will be an integer, it's an integer, so I'm going to accept as input an integer and over here. We want to tell C where we want to put the integer. So basically I'll tell you what variable I want to store this in. So I'm going to save this inside my age variable and I can basically type the variable name here But to get user input, I'm going to have to type an ampersand in front of this. So instead of just writing age. I'm going to have to type in the sign age and when I say sign age, this is what's called a pointer and we'll talk about pointers in a later tutorial.
I'll cover everything you need to know about pointers. But for now, that's a little bit beyond what we need to learn, so all you need to know is that when you use scanf and you want to store information inside an integer, a float, or a character, you need to use this ampersand here . So down here now that we've scanned the age of the users. I'm going to go ahead and print it. So we'll print it like you and I'll say % D years and here I'll just print that age variable. Basically what I'm doing is asking the user to enter her age.
I'm storing the age they entered inside this age variable and then printing it to them. So let's go ahead and run this program and we'll see how we did. I go to run the program and it says: enter your age. So let's say someone is 50 years old when I click enter. He will take that value. You can take that integer that we entered, 50, store it inside that age variable and print it. You will see it here. He says you are 50 years old. This is how we can get an integer from the user. We can also get a double from the user, so for example, I could say enter your GPA and now instead of asking for an integer, we'll ask for a double.
I can come here and say double and we could call this GPA and now I can do the same thing. But instead of saying D, I want to say LF and LF will basically tell this scanf function that we're looking for a Double and then obviously instead of an age. We'll just put the GPA inside here. And now we can just say what your GPA is like and then we can put %F because we're going to print a double and I'll just say GPA. So you'll notice here in printf when we want to use a double.
We are using F percent to print it. But when we use scanf we are going to use the LF percentage. That's like a little difference and now let's go ahead and run this program. And we should be able to get a GPA. So let's say someone's GPA is like 3.1. Now. It says your GPA is 3.1. This is how we can get a double. I also want to show you how we can get a character, so why don't we create a character up here and we'll just call it rating and say enter your rating and now when we want to get a character, I can just say C percentage and again I can go down here and say commercial grade and then down here we can just say your grade is Percent C and we'll go ahead and print the grade.
So now we should be able to get a character from the user, so enter the grade, let's say I got an A and it says you're good, so we can use this scanf to get specific types of input from the user. Now we can store those specific types of input inside the variables. I also want to show you one more thing we can do: we can get a string from the user, so in addition to getting numbers and a character, we can also get a string from the user and this is going to be a little bit different from doing numbers and characters.
So I want to show you how we can do this very quickly, sohere let's create a variable. We'll just call it. Let's just call it by its name. So we're going to store someone's name and whenever we create a string of characters, we always need these opening and closing brackets and inside of here I'm going to specify how many characters I want to be able to store inside. this string of characters, so I'm just going to put 20 and In the past in this course when we've been creating strings. We haven't put a number there. We've just said whatever.
Alright, we basically put a value on this right away. But in this particular situation, we won't give this variable a value right away, in other words, I don't know what the user's name will be like, I don't know from the beginning. They will tell us what that is. So if I'm not going to put a value on this, right? Actually, I need to know how big I want this variable to be. In other words, I need to know how many characters I want this variable to be able to store and that way I can go ahead and allocate enough memory for this variable.
So I'm going to put in 20 and 20 basically means that this will be able to store 20 characters. And I think that's enough for a name down here. We'll just say enter your name and I can use scanf to get the input as a string, but instead of saying percentage, see, I'll just say percentage s and here, instead of saying ampersand degree, I'll just type the name of the string. So I don't actually need this sign here. I can just specify the name of the string, so down here it says printf your rating is and actually let's say your name is.
I would have said percentage s and then here we can print the name. So this should work just as it did. in the other cases, let's go ahead and run this and it says, enter your name. So my name is Mike and you'll see it says your name is Mike. That works great, but there is a problem when we use scanf to get a string from the user. So, for example, if I come in here and say enter your name, and I say my name is John Smith, when I click enter, you'll notice that he just says your name.
John, Smith is not included here and here is the problem. This is because whenever I use this scanf function I use it with a string. It will only grab characters up to the first slot, so once it sees this slot it will say, okay, we're done getting characters, right? So that's kind of a problem and that's how scanf works. It's not really, you know, scanf error. There is a way that we can modify scanf to be able to get input with spaces, but there is another function that I want to show you that we can use to get a line of text from the user and it is called F gets and Afghans is another function, it is similar to scanf but f gets will basically be more generous.
The F gas will basically take over an entire line of text. You won't be able to take it and store it inside. such as an integer, a variable, or a character. You can simply store it within a string of characters. So when we use a, we forget that the first argument we want to give it is the name of the variable where we want to store the line of text. So in our case, it's going to be just this variable name. The next thing we want to do is specify how many characters we want the user to be able to enter.
So this will essentially limit the number of characters the user can enter. And this is always a good idea when we try to get a string from the user because A common problem is that the user could try to enter like a million characters and the buffer would overflow, in other words, View would not be able to accept that many characters and the program could crash. So here with F. spaces We can specify how many characters we want to accept, so I'm just going to say 20 because that's how many characters we can store inside this variable and here I'm just going to say stdin and this means standard input.
Basically what we're doing here is telling the FCAT where we want to get the information from and the standard input is essentially set up like that little console that we've been using, so now I'm using F. Gatz and this will do exactly what scanf did, so we will be able to store the input inside this named variable, but now we will be able to store multiple words instead of a single word. So let's run this program and it says, enter your name. Now if we go into John Smith, you'll see that you can go into John Smith.
Now, the only downside to using this F is that you'll notice that it printed this. new line here. Let me demonstrate this a little. If I were to print, you know, just some random text here. Every time I get input using scanf, if I entered John Smith when I click enter. key So when I enter this by clicking the Enter key, that will actually represent a newline character and it will be stored inside the string. So when I click enter, you'll notice that it says your name is John Smith and then it prints a new line and prints this text.
And that's something you need to keep in mind when you use this F gets function. but for the most part, I would say that if you receive input from a user in string form, you can use F gets, you can also use scanf and you could use various percentages of asses and in fact, I'm going to show you that. guys, how could we do something like this in a future tutorial. But for the most part, I would say whenever they get a string from the user they just want to use F Guess, so those are the basics of getting input from the user like I said.
There are a couple more things we can do and I'm going to talk about at least one other way we can get strings from the user in a future tutorial. But this is something basic and you can play with this. things in your programs In this tutorial I will show you how to build a basic calculator in C. Basically, we will create a small program where the user can enter two numbers and then our program will add those two numbers. We put them together and spit out the answer. This is going to be great and we will also learn how to get numbers as input from a user.
So here, I want to show you exactly how we can do this, first thing I want. What you need to do is print a message. So basically I want to ask the user for some input, so here I can say printf and Inside here. We'll just give you a little warning. So I'm going to say enter the first number and essentially what we're going to have you do is enter two numbers, so I'm actually going to create two variables here, so we're going to make an int and we're going to call it number1. and then we'll do another inch and we'll call it num2 and I'm actually not going to give these values ​​from the beginning, we'll end up giving them whatever the user enters.
So after the user enters the first number we really need to take that number so we can use another display function called scanf and here we're basically going to say percentage d because we want to take an integer and then here I'm going to say the name of the variable Where I want to store the value that is entered now if you are following this course in the last tutorial. We were able to get a string of characters as user input. And we basically just write like the name of the variable. But when we receive an input, that's not a string of characters.
In other words, when we receive an input that is not percent s, then if it is like percent d or percent 4 percent C, then if it is a type, a decimal or an integer or a float or a character, we actually need to use a symbol special, this sign. So I'm going to have to say sign and then the name of the variable that I want to store this value in, so I'm just going to say sign 1 and basically What this means is that we're accessing the address of number 1 and we'll talk about addresses and pointers in a future video, but for now just know that you need this sign here to store the value that is entered inside this. variable So once we do that, now we can go ahead and basically we're going to do the exact same thing, but for the second number, I'm going to copy and paste this and now instead of saying, enter the first number. .
We'll say enter the second number and we'll just save this inside num2. Okay, once we're done with this, the last thing we want to do is add them up and print the answer. So it's just going to come down here. I'm going to say printf and we're just going to say response and here we're going to print the response so it's an integer and we're basically going to print num1 plus num2. So we will print the value of num1 plus num2. So we have our basic calculator. We're getting the first issue. We are getting the second issue.
We are storing them inside these variables. We are adding them and printing them. Let's try running our calculator. So I'm going to run my program and you'll see it here. It says enter the first number So let's go ahead and enter 6 enter The second number will enter A8 and now we will get 14. So 14 is the correct answer. So our program worked, it was able to add the numbers correctly and everything is amazing. But we go. I'll show you a problem with this program if I wanted to do math with multiple non-decimal numbers, for example, if I said 2 and then here I said like 6 point 8 when I added them together.
We're not going to get the right answer. We'll get 6 plus 2, which will be an integer 8, but we won't get 8 point 8, so we won't get the correct value. So instead of allowing the user to enter only integers, why not? Instead, we let them in on doubles, so this way? I'm going to change them to doubles, so I'm going to say number one is going to be double and number2 is going to be double, and since we're getting doubles as input, we're going to have to go down here and modify them. So right now, this accepts an integer, but we want it to accept a double, so normally if we're using Printf and we want to print a double, we would say f and that means a floating point number, but when searching for a number, If we want to use a double, we have to say LF just like that down here.
I'll also say LF and we'll change this to just F so that printing F is a little different than Scanf right and scanf if we want to accept a double as input. We need to use LF in printf. We just use %F. So let's run our program now and we should be able to add floating point numbers, so the decimal numbers here I'll do four point five plus six point seven and we'll get 11 points. two, so that's fine with me. Everything seems to be working correctly. And now we have a basic calculator. So the user can enter any number he wants and the calculator will be able to handle them.
Now this calculator is not 100% secure, for example, like if I came up here and entered. If you type a string of characters instead of a number, you will find that it will break the program, right? So it's like not doing what we wanted to do. And as we go through this course, we're going to learn all kinds of ways that we can check to see if certain things are being entered correctly and we can basically mitigate circumstances like that. but for now, this is just a sort of introduction to how we can take numbers as input and do things like addition or multiplication once we have them.
In this tutorial I will talk to you about how to create a small game in C. Specifically, we are going to create a mad libs game now. If you've ever played the game mad libs, it's basically a game where you write down a bunch of random words so it can be like Do you know nouns or verbs or someone's name or do you know a verb that ends in ing, something like that, you take all those words you enter and sprinkle them into a story and then overall the story is kind of funny because it has all these random words.
Actually, it's if we go to my web browser. You'll see I have a picture of Madlib up here. Basically you just add a bunch of random words to the story and then reread the story and it could be kind of fun. So I'm going to show you how we can build something like that in C. And we're also going to talk about a few more ways we can use that scanf function that I showed you in the last tutorial. So here we have a little story that I printed. It simply says roses are red, violets are blue, I love you, like a classic poem.
But I think this poem would be a lot more fun if we turned it into a crazy bookstore. So that's exactly what we're going to do. I'm going to replace the roses that are red. I'm going to replace the red with a random color. So we'll simply have a user enter a color. I will replace violets with a plural noun. So we'll make them go into a plural noun, and instead of saying I love you, we'll say I love and then a specific celebrity. So I'll write celebrity right there. Okay, so this is basically what we're going to print.
They will print roses for us and then any color they enter. Plural nouns are blue and then I love any celebrity. so let's talk about creating this program. So, we're going to need to do a couple of things. And actually, the first thing I want to do is create variables. Let's create variables to store. The color the user enters. the plural noun that the user enters and the celebrity that the user enters we're going to create three variables and these are going to be basically strings of characters, so they're going to be collections of characters and we can store them in Variable, so I'm going to create some of these variables.
Why don't we create one for color? And do we remember it everytime we create a String or like a collection of characters? We need to make these open and closed brackets. And what I also want to do because I'm not going to color value right away. In other words. We are allowing the user to determine the color value. I just need to say how many characters we want this string to be able to store like that. If you know how much memory you need to allocate for this variable, then I'm just going to say. 20 and we will basically say that you can enter a color up to 20 characters, we will do the same for the plural noun and again, we will let you enter 20 characters maximum and finally, I will do the same for celebrities.
Again, 20 characters sounds good. Now that we have our variables created. I really want to get information from the user, so I want to ask the user for information and I want to take the information that they entered and I want to store it inside each of these variables. The first order of business is to ask them for input, so I can say print F and in here Basically we'll just type enter a color. Once we've asked them to enter the color, we can get any color they enter and store it inside a variable. I'm going to use a function called Scanf and here.
I'm going to accept a string, so I'm going to accept a string of characters and we're going to store it inside our color variable just like that and remember, if you saw the last tutorial, we used the ampersand here when we got numbers or you would also do the same thing if you got a single character. But when we receive input for a string of characters, we don't need that ampersand, so you can get rid of it. Let's copy these and I. I'm going to paste this two more times, so the second thing we want to get from them is going to be the plural noun and I'm going to save it inside the plural noun variable so you can see now that we're getting the plural noun. and finally, we will do the same with celebrities.
And again, we're going to have that celebrity. Well great. So now I'm understanding color. I get the plural noun and I get the celebrity. So the last thing we need to do is take all of these variables and put them into our story. Okay, so we need to be able to print the story. with all those variables So I'm going to go down here and just say percentages and around here. We will pass the color. The same here. I'm going to replace the plural noun here with a percentage s and we'll pass the plural noun and finally the same for celebrities here.
Alright, everything seems to be connected and you'll notice that I have new lines here, so let this story print on new lines, let's go ahead and run this program and we'll see how we did. Here, it asks us for a color. Why don't we go into magenta? Enter a plural noun, let's do Microwave and enter a celebrity. Why don't we just say it? Prince, when you click enter, will say that roses are magenta and microwaves are blue. I love prints. So we were able to ask the user to enter all that data. We take everything they entered, store it in variables.
Then we print all those variables into our story and we have our Madeleine. The program seems to be working very well. I want to show you one way this program could fail. So let's go ahead and run this again. Let's enter in a different color. I'm going to enter as blue in a plural noun So why don't we enter? phones And now enter a celebrity, so I'm going to show you a way we could really break this program. If I enter a celebrity with a first and last name like Tom Hanks, when I click enter now, you'll notice that instead of saying I love Tom Hanks.
It just says I love Tom. Here is the problem when we use the scanf function. scanf will only capture characters up to the first white space, so essentially when we put this space here. We're saying we don't want to capture any more, but we actually want to be able to capture the full name. Do you want to be able to capture the first and last name of celebrities if necessary? So this is a situation in C where we would have to modify our little program, so what I could do is, instead of just getting a variable like the celebrity I can actually get to, so I could say here like Celebrity F and that represents the name of the celebrity and then down here.
We can create another variable called celebrity l which will represent a last name. So now when we scan instead of just searching for one string, I can scan two strings and we'll have celebrity F and then celebrity l and Down here. We'll want to do the same thing so we can say celebrity F and celebrity L and we just need to add another percentage here. Then it will say I love celebrities first and last name. So let's run our program and see how we did it so we can enter as red and Microphones and now we can enter Tom Hanks and we are printing the actors first and last name.
That's one way we can remedy this program and make it be able to accept two inputs with a space in between. And it also shows you a little bit more about how scanf works, so it will stop scanning. You will stop receiving input in that first space now. This is what happens with this. However, if you wanted to enter a celebrity with Who Only Had One Name? So if I only wanted to enter one, the program won't really be able to handle it, so if I said "I like hats" and down here, if I said "I like Gandhi" and I click "enter", you'll notice that it's still expecting. to enter a last name Right, so I could enter something here and then the program will work But it was waiting for me after I entered just that first name So that's something you'll have to play with. in your programs Basically, C will force you to be very specific about what the user enters, so if the user needs to enter two things, like two words, you need to specify that if the user will only enter one word.
You have to specify that. So you have to be very specific when you get user feedback like that. In this tutorial I will talk to you about using a raisin seed many times when we write our C programs. We will be dealing with a lot of different data and one of the things we can do to control, manage and track and organize that data is putting data inside things called arrays. An array is really useful because it is basically a container where you can store a piece of information. But what happens in your programs when dealing with enormous amounts of information?
Specifically, large amounts of related information, right? Imagine you had a list of a bunch of names or a list of a bunch of numbers that you needed to keep track of. Let's say you had a list of a hundred numbers, right? I wouldn't want to have to create a hundred different variables to store all those different numbers and this is where arrays come in. An array is essentially a data structure where we can store a bunch of different data values. So, inside an array, unlike a variable where I can only store one value. It could store hundreds, thousands or even millions of values.
So within a single array, I could store five things, seven things, ten numbers or twenty characters, you know, I could sort a bunch. of different pieces of information and then all that information would be nice and neat and organized in my program, so I'm going to show you how to create arrays. We can create an array many times like we create a normal variable and a variable in an array. They are very similar variables that are basically used to define a container that stores a single value and an array is used to define a container that stores, you know, any number of values.
So the first thing we'll need to do when we create an array is give C some information and the first information is what type of data we want to store inside the array, so for example I could say int and now I would be creating an array which would contain integers. If I said char, I would be creating an array that would contain characters. Or if I said duplicate the same thing but for double numbers, then why don't we create an array of integers? And I'm going to call these lucky numbers every time I create an array.
I always want to use a little special symbol and this symbol will tell you that instead of just creating a normal variable we want an array and it's an open and closed square. brackets every time I use these opening and closing brackets after the name. He will tell C, it's okay. We want to store multiple pieces of information. Now there are many different ways. We can create these arrays, although the easiest one is to just say equals and I can make an open and closed brace and inside this open and closed brace I can start writing some numbers that I want to put in this array, so we could write like 4 8 15 16 23 42 Now I can basically type in as many numbers as I want and I could store them inside this array, so here, you know, I printed six numbers and I could print 600. if I wanted to.
You know, the only thing that limits me is the amount I can actually put in here. So you'll notice that I put the data as 4, for example. And then I put a comma and a comma is We will separate all the elements of the array. So we would say that 4 is the first element of the array. 8 is the second element of the array. These are all considered elements within this array and the nice thing about this array is that it now stores all of them. of this information Unlike a variable where now we can only store one value in this array.
I'm storing 6 different numbers, so if I wanted to I can print this and I'm actually going to print it just to show you, so remember, we're going to print a number and I'm going to show you how. we can access specific elements within this array. So all these numbers are stored in this array of lucky numbers in this array structure. But the question is how do we access them? Ok, how do I access one of these? well I could say present so I'm going to print this and now I can say lucky numbers so I'm going to refer to the name of the array and when I want to access a specific element I can open a closing bracket and inside this opening bracket and closure I want to put the index of the element that I want to access so that all these elements here have a specific index and I can access them by putting the index inside this square bracket, so if I wanted to access this for example, I can put a 0 inside from here.
And now when I run my program, you'll see that we'll be able to print it. So here, I'm printing four. If I wanted to access these 15, I can put a two here. Now when I run my program I will access 15 as you can see one thing you may have already noticed is that we start the indexes and arrays at 0 so when I wanted to access this 4 instead of putting a 1. here I put a 0 here, right? You might think that 4 would be at index position 1 because it is the first element of the array, but in C we start the array indices at 0, so actually the first element, this 4, will be at position index 0, so if I were to write indices, I would say 4 is at index position 0, this 8 is at index position 1, this 15 is at index position 2, 16 is a 10x position, 3, etc That will allow me to access all of these elements within the array individually, so I could print them by simply referencing the index within these square brackets.
Another thing I can do is modify some of the elements inside here, so let's say I want to modify the lucky numbers in position 1 of x, so let's say I want to modify this 8 so that I don't like this 8 anymore, so basically I can assign it a different value like I normally would so I can say lucky numbers and I can do an opening and closing bracket, we can say 1 and I can just set is equal to something else, so I could set it equal to 200 and now when we print the lucky numbers 1 instead of printing an 8, we will print a 200, so let's go ahead and do that and run my program and you.
We'll see that we're printing 200 instead of 8 so you can modify any of the individual elements within the array simply by referencing the index, and really, when you think about this, conceptually an array basically contains a bunch of variables, but those variables don't They have names. So when I access lucky numbers 1, it's the same as when I access an integer variable. It's just that you know, the array contains potentially hundreds, thousands or millions of these different variables. So a matrix is ​​very useful. The structure, especially if you are storing data, is very similar. So like I said, you could do this with an N.
You could also do this with a double. You could do this with a float. I can do this with whatever I want. But there will be situations where you may not necessarily know all the elements you want to put inside your array. So let's say I'm creating an array and I don't know yet what numbers I want to put in it. So maybe I just like it. I want to create it I want to tell C that we need it but I don't necessarily want to put anything in it right away Okay, up here. Basically we arecalled else.
If so, down here you could write else. Yes and I can make another open and closed parenthesis and inside these parentheses. In fact, we can verify another condition. So if this up here in these conditions is false, then it says let's go down here. Let's check another condition. So here I want to do basically the same thing. I am going to check if number 2 is greater than or equal to number 1 and number 2 is greater than or equal to number 3. So if this condition is true if num 2 is greater than or equal to num 1 and is greater than equal that num 3 So we know for sure that the result will be num - because that is the largest now finally down here We can just say something else and we can do another one to open and close braces And remember that this code will be executed when none of these conditions are true .
So here we can set the result equal to the number 3. So let me explain this one more time here. We have this if statement. and in addition to checking if num1 is greater than or equal to num2, we also use this special operator called and and we are checking if num1 is greater than or equal to num 3. If that is the case, then the result is will be equal to the number 1. Otherwise Otherwise, if this condition is false, then we will go down here and check this other condition. So we will check if the number 2 is greater than the number 1 and if it is greater than the number 3, if that is true, then we will do this.
Finally, if none of these conditions up here are true, we'll just go ahead and set the result equal to the number 3 and then return the results. So this seems fine to me. Let's try this and see if it works. So when we call max we need to include three numbers. So why don't we just do 1? 2 and 3 and this is basically going to spit out the largest one, which should be 3, so let's run our program and you can see here that we get 3, so let's try to make one of the others the largest, so we'll do the 1 in the first slot, the largest one, let's run this again and you can see that it returns 10.
So this function is working. This is how we can use these and and we can also use else--if to check additional conditions, so and is used to check more than one condition inside if Parenthesis and the else if is used to check another condition when this condition is false and that will actually work quite well. Now I want to show you a couple more things. There are a couple more things we can do with these if statements to For example, I'll come down here and show you some of these. In addition to using that, we can also use something called or like that.
I'm going to write a simple if statement that could say if 3 is greater than 2. and instead of using and like this, I can use or and or will basically allow us to check two conditions, and everything will be true if only one of those conditions is true, so you could say if 3 is greater than 2 or 2 is greater than 5 Then only one of these is true, right 3 is greater than 2 that is true but 2 is not greater than 5 so this type is false, but when we use o One of these two conditions has to be true for everything to be true.
So if I like to print something here as true. Now when I run my program, we should print true because we're using that or and you see we do that. But if I were to make this type false, then if I made this 3 less than 2 now it won't print true and we can just print false. So now we're going to end up renting something fake because both types are fake. As you can see below. We are saying false. So the difference between and and or when we have and so Both conditions have to be true for everything to be true and for us to run this code.
When we have or only one of these types it has to be true So that's the difference and I want to show you a couple more things that we can use. Here we are using a less than sign, for example, if 3 is less than 2, I can also use a greater than sign. sign You could use the less than or equal to sign. You could also use a greater or equal sign and if you wanted to check equality you could use a double. You could use a double equal. So double equal will check if 3 is equal to 2.
We can also use one more. which is this exclamation point, an exclamation point basically means not equal, so you could say that if 3 is not equal to 2, then we'll print true down here. Let's run this and you will see that it becomes true. So yeah, there you have it. One more thing. What I want to show you is how we can negate an entire operation. So, for example, if I said that 3 is greater than 2, this will be true, so we would end up printing true here because 3 is greater than 2, but you could surround all of this with parentheses and you could put an exclamation point. dot right before this and this is called the negation operator and it's basically going to negate whatever it ends up being, so this is true.
And we put this negation operator here, all of this will be false. And now you will see why this is true and we are using the negation operator. We're not going to end up printing true. And you can see we just don't print. But if I put a false condition here, like saying that three is less than two, this is false. But since we are denying it now, it will end up being true. So when I run my program, you can see that it returns true. That's kind of the basics of using if statements and I cover almost everything you can do. use ands, we can use all these different comparison operators.
We can use ORS and we can use this exclamation point to deny something. In this tutorial I will teach you how to build a calculator. See, have you been following this? course? You will know it at the beginning of the course. We actually created a calculator and it was basically a very basic calculator. We allowed the user to enter two numbers and took those numbers, added them, and printed the answer on the screen in this tutorial. I'm going to take some of the things we've learned since then and show you how we can build a fully functional function calculator.
So, this calculator will be able to add, subtract, multiply and divide, and we will let the user decide which one he wants to do. We'll let them decide if they want to add or subtract or whatever. It's going to be cool and we'll end up using some of the things that we've learned recently in the course, like if statements that we'll also use like getting input from users and I'll show you how you can get characters as input from a user as well. So down here. We want to start making our calculator, basically what I'll do is ask the user to enter a number.
Then I'll ask you to enter an operator like plus minus division or subtraction, finally we'll ask. ask them to enter a third number and then depending on the operator they specify, addition and subtraction, we will perform the correct operation and print the number. So the first thing I want to do is create variables where I can store the numbers and the operator, so I'll make some double variables and I'll just call it Num 1 and then we'll make another double, call it num2, finally we'll create a variable that will store the operators that enter , so this will just be one character and I'll call it Operator op4.
Alright. So now we want to get information from the user. I want to find out what numbers they want to use and then what carrier they want to use as well. So down here. Why don't we get any feedback? The first thing I'm going to do is print a message, so I'll just say: enter a number. And now what we want to do is be able to take the number that they give us and store it inside one of those doubles that we created. So I'm just going to use this scanf Scanf function and here I'm going to specify that we want to get a double.
So remember when we use scanf if we want to scan. for a double we use F and L F is going to look for a double and now I'm going to use a comma and we're going to put the variable that we want to store here, so I'm going to use this sign and I'm just going to say number 1. So this is exactly what we need to do to be able to get the users input and store it at number 1. The next thing we want to do is get the operator. So I'll write another message and just say "enter operator" basically.
They will enter the plus sign, the minus sign, the asterisks or the forward slash depending on what they want to do and again now we are going to search. a character now, when we look for a character and scanf and actually, I don't think I've talked about this yet in this course. We want to do something special. So I want to put a % and a C. But before putting %C I want to put a space so that whenever we get a character from the user using scanf, we always want to put a space right here before %c otherwise it's not going to function.
And again we want to store this in that Opie variable, so I'll just say o P and once again we'll get another number. So I'm just going to copy this and paste this here and we'll get this for num2. Now we should have all the tickets for our program. So I get the first number, I get the operator and I get the second number. The last thing we need to do now is do the math. So we'll have to figure out which carrier they wanted to use. Okay, so we have this Opie variable and this is stored as a plus sign, a minus sign, an Asterix, or a forward slash.
So depending on what it's storing, we want to print something different, so we can use an if statement to do this , so I can use an if statement to check and see which operator is inside our operator variable and depending on which one it is? We can do a different operation to be able to say if and the first thing I'm going to do is just check if the operator is equal to a plus sign, so if the operator that the user entered is equal to a plus sign, then we're going to want to add the two numbers.
So I'm just going to print these two numbers and say num1 plus num2 because we're going to add them both together. We can also use a safe L. So I'm going to want to check some other conditions. I'm going to check if the operator is equal to a minus sign and if the operator is equal to a minus sign, then instead of adding the numbers we're going to subtract them, so here I'm just going to copy this and we'll paste this guy down here. So it will be num1 - num2. And I can actually copy all of this and paste it. down here.
So it makes more room down here. We will paste this other elsif and here we will check if it is division. So we will check if there is a forward slash. And again, we're just going to want to print a decimal number, so it's going to be num1 divided by num2. Finally, we will check the multiplication. So if it's multiplication, then we'll multiply them together. Now we are verifying each of the operators. We are checking addition, subtraction, multiplication and division. But this is what happens if the user enters an operator that we didn't want, for example, if you don't enter any of these operators, we're going to want to tell you them, so I'll have it print a little error message.
We're just going to say else and basically the code inside this else block will be executed. If none of these conditions are true up here and down here we can just make a print. F and I'm going to print an invalid operator. Now we have our if statement or if block setup and this should be all we need to use our little calculator. Alright, let's go ahead and run this program so you can see. here it says enter a number so we can enter a number. Let's enter five point nine and enter an operator, we'll add numbers and now let's enter 4.0.
So we're going to add five point nine and four, so when I hit enter, we need to go through all of those if statements and figure out what. we have to do and you will see here that we are adding five point nine and four together and we are getting nine point nine. It seems that the program worked. Let's try it again. We will try another operator. Why don't we try multiplying some numbers? So let's multiply six and by I don't know five point seven, so we get thirty-four point two, which seems about right. Okay, and then we'll run this one more time and I want to try entering an invalid operator.
So we'll say like 5.7 and I'll just enter a G. So that's an invalid operator. That's not going to work and I'll enter my number. Let's do 8 and you can see it tells us an invalid operator, so basically we have a four function calculator. This calculator can multiply, divide, subtract and add, and if you don't enter a correct operator, it's smart enough to yell at you and tell you that. you have an invalid operator. So this seems to work pretty well. I'm quite happy with this. And you can see down here that this is a perfect situation to use something like an if statement, right?
We have this opie variable, right? It is storing some type of operator. We don't necessarily know what's inside there. But I can use if statements and I can answer the different scenarios. So in the situation where it is a plus sign, I can answer in the situation where it is a minus sign. I can respond etc. And this makes our programs much smarter and helps us do different things when different situations arise. In this tutorial I want to talk to you about using switch statements in C. Now a switch statement is basically a special type. of an if statement that will allow us to compare a value with a bunch ofdifferent values.
And it essentially does the same thing as an if statement. It allows us to check different conditions and whether the different conditions are true. So we can do certain things, but one change. The statement makes it really easy for us to compare a specific value with many other values. Now I will show you how we can use it in this tutorial to create a small test application. So I'll create a little program. that will take a letter grade and depending on the letter grade, it will tell you if you did well or poorly on a test.
So if you got an A on the exam, you'd say, "Hey, you did a good job." If you got a B. at home, it would be like hey, you did it. Alright, if you got an F on the exam to tell you that you failed the exam, then we're basically going to have a variable where we're going to store a grade that you would get. on a test and we will be able to respond to that grade using a change statement. Then it will be great. So down here, the first thing I'm going to do is create a variable and it's just going to be a character and I'm going to call it rating and I'm going to set it equal to a.
So we will say that whoever took This test got an A and they did very well. Now what I want to do is create a switch statement. So I'm just going to say Change and I'm going to make an opening and closing parenthesis and inside this parenthesis I'm going to put a rating, so I'm just going to put a rating and make an opening and closing brace. So This is kind of the basic structure of our switch statement. Now, this is what I'm basically going to do. I'm going to take this variable here that I pass to the degree in parentheses and I'm going to compare it to a bunch of different values.
So I'm going to take this, I'm going to see. If it's equal to a value then I'm going to see if it's equal to a different value or a different value or a different value and depending on what value it ends up being equal to we're going to do something different and this is what they're called cases so down here I can say case and I just want to type one character, so for example, I could put a right here and then I'll put a colon and when I put a right here, basically what this says. is the case where the grade is equal to a, so I'm going to put some code here that I want to run so I can put it as a printed statement and I could say that you did very well because they did very well on the exam and basically What what this says is that in the case that the grade is equal to a, we will print this and then I want to put one more thing here called pause and I'm going to explain what the pause does in a second.
I could make one of these little case statements for every possibility, for every possible rating that we could get, so I'm actually going to copy all of this and paste it here. So now I can put a B here. . So we say that in the event that the grade is equal to B, I will say yes. All right, and then we're also going to break and fall here. I'm going to make another one. So I'm going to say case and this time we're going to do C in case they get a C. I'm going to print out the e portal and there's a couple more that we can check so I can check if they got a D or an F.
So in the case of who got a D and I forgot to put a break statement here and I'm going to put one here too. And finally, we'll just do one more. because if they didn't pass the test, then I'll just say in the case that they got an F, we'll print. You failed, so essentially, what I'm doing here is writing down all these different cases. I'm saying that in the case that you got an F. I'm going to print that you failed and in the case that you got a D, I'm going to print that you did very poorly.
And then we also have this breakout statement here and this breakup statement basically. It tells our switch statement that we want to exit. So every time we execute this switch statement, every time C executes the switch statement. You are checking if the rating is equal to each of these cases. So, for example, let's say rating is equal to a will run this print F But if I don't put this break statement here, then it will go ahead and keep checking these different cases. So it's always important to put a break statement in there because I basically just want to break out of this switch statement, I don't want to be here anymore.
Finally we can check one more thing in case the user gets an invalid rating. So for example, if someone entered as t or G or something that's not a valid grade, then I can use something called default and Default is basically like something else, so default will run whenever none of these conditions up here or Matt, so I can just put something like Invalid Rating or something like that and this will basically be like, hey, you have an invalid rating. So let's take a look at this switch statement and talk a little bit more about what it's basically doing. , I'm passing this in a variable or a value or something and I'm going to compare this value with all these different conditions, so in the case that the grade is equal to a, we'll print that you did very well and in the case of that B is equal to the grade you will print.
Very good, etc. and this is basically doing what an if statement does so I can replicate this whole statement this whole switch statement as an if statement which I can write if etc else--if else--if else--if But this just does it a lot easier. So I don't have to constantly write all that down. So I hope we did everything right and let's check and see how our program works. So the grade is equal to let's run our program and see what our message is It says hey, you did great because we got an A if I made this an F instead of an A.
Now it should tell us that we failed. Then he says you failed if I made this a C. He'll tell us we did it wrong and he says you did it wrong. So this is a super easy way for us to check a single value against a bunch of different conditions. And that's really the point of a switch statement is that you're checking a single value as a rating against a bunch of other values ​​and that's where it can be extremely useful. So consider using switch statements. They are not appropriate in all situations and I would say that there are a very select few situations in which switch statements will be useful.
But when they are appropriate, changing statements will save you a lot of time and a lot of hassle. In this tutorial I want to talk to you about strux in C. Now a struct is a data structure where we can store groups of data types, so inside a struct you can store like an integer next to a string next to a character next to a double, you could sort all of these different data types into a single data structure and there are tons of uses for structures and one of the things we can do with them is model real world entities.
Basically, you might like a model. Something in the real world inside my program and I'll show you how to do it in this tutorial. We will see how we can use a structure to represent a student within our program. Let's imagine that we were writing software that would be used by students. So it was like storing student records or something. I could use a struct to represent a student in my program, so here in my program I'll just go up here on top of the main method and actually create a struct. Guys will see how they work and how we can use them, so I'll just say structure and over here, I'll give it a name, so I'll call it Student and a lot of times in C when We're doing structures.
You'll give them a capital letter like this and now I'll make an open and closed brace. Within this structure I can begin to specify what types of data will make up a student in my program. So basically I can Define different attributes of a student and place them here. This will act as a template and you will see how we can use it later. So, let's think about the different attributes of a student. Well, one thing would be like the student's name. So I could say char name and I'll make this an array of characters so that this represents the name and in fact, why don't we give it a number?
So I'll say 50 so this string can contain 50 characters at most. and now we are going to do another one for the specialty. So we're going to want to store the students' major and again, we'll make this 50. I also want to make an integer for the students' age and a double for the students' GPA. I have this student structure and here I have a bunch of post columns from a student, like students name their major. An integer that stores your age and a double for your GPA. Essentially, what I did was create as a type of student. data type So I'm basically allowing myself to represent a student within my program, so let's go over here to this main method and I'll show you how we can use it so I can basically instantiate this student structure.
I can create as a real student within my program and the way I do it is by simply saying Structure and I want to type the name of the structure that I want to create in our case. It will be a student and I want to give this a name. So I'm going to call it student 1 and I can basically use a semicolon here. What I did now was create a container called student 1 that will be able to store a name, an age major, and a GPA. So if you are familiar with arrays in C, you will know that an array is a special structure that can contain multiple pieces of information.
But all pieces of information within an array must be the same data type and also do not have names with a structure. I can have a bunch of different data types like this and I can also give them names like name, primary age, and GPA. Now let me show you how we can assign some values ​​to these. So for this particular student, student 1. I can give them a name, I can give them a major, a GPA, and an age, so I could say 1 point age of the student and I can set this equal to something so I can say let's say that the student's age is 22, so basically inside this student. 1 container I'm saying that this particular student's age will be 22.
I can do the same for the GPA, so I can say that the student's GPA is equal to maybe he has a 3.2. So I'm saying that this particular student's GPA is a 3.2. . I can also do the same with those chains. So for the name and specialty now, this is what happens with working with strings. Remember that in C a string is actually just an array of characters and when we have an array we cannot give it a particular value. So for example, if I wanted to give this student a value name, this is just a string of characters.
So I can't just come down here and say student name equals something. That's not going to work because you can't do that with an array. I can use something called a string copy function and this is a function that will take a string and give it a value that we specify, so it will basically do what we want to do. We did it here with the age and the GPA, but with the name so we could say Str. C py and inside here. I need to pass these two parameters. The first thing I want to pass is the destination of the chain.
So, we'll pass the point name to student 1. The second thing I want to pass is the string that I want to store inside the name of student 1. So in our case, let's call him Jim. Now that we have the name value of student 1 is equal to Jim and I can do the same for the major, so I'm going to copy this and we'll go down here and I could say student 1 major and we'll set it equal to Business, so let's say Jim is a business student. Basically what I did here is I created a student and that student had all the attributes that we defined here.
So this particular student had a name, major, age, and GPA and I gave him all of those values. So I assigned this student 1 an age of 20 and a GPA of 3.2, etc. So now what I can do is print everything. These different values ​​are stored within this structure, so if I wanted to, for example, I would like to Print GPA so I can print Student 1's GPA and now we're going to print 3.2. And it actually looks like I have a typo here. This should be student 1, not student. Okay, here we are printing 3.2. So we are printing the value of the student's GPA.
I can also print it as his name. So why don't we do the name of student 1 and now this will print? As you will see, we print there. So a structure is a really useful structure. And another cool thing we can do with structures is we can create another student. in order to create another instance of that student structure. So I'm actually going to copy all of this and show you how we can do this. I'm just going to come down here and paste it in and instead of student 1, why don't we call this student 2 and we can set this student's age to maybe 20?
Maybe his GPA is 2.5. Let's say her name is Pam and they are studying art. So, nowI have a completely different student. And if I wanted to, I could print the attributes of this student so I could print as the point name of student 2 and now we'll get Pam instead of Jim so you can see how. that works there. So I could create as many students as I want and the nice thing about Structs is that I can define the basic template for a student in my program and then I can create individual students down here that I can work with. with.
Now that I have this student variable here, I can do whatever I want with it. You could pass it to a function. You could print it on the screen. You could use it in something like an if statement. I could do basically anything I wanted with it. It acts much like a variable or an array, so remember variables and arrays. or just Containers, we can do almost anything we want with them and that's the same with a structure, so this has just been a kind of introduction and what you could do as an exercise is think about other things that you might want to model in your program.
So maybe something like a book or something like a phone, you could model a phone using basically any type of information that you can store using a structure like this. In this tutorial I want to talk to you about while loops in C now a The while loop is basically a structure in the C programming language that we can use to continuously loop through and execute a specific block of code until a certain condition is false. So I can specify a condition and then I can basically step through a bunch of code. Until that condition is no longer true and loops can be really useful, there will be many times in our programs we will either want to let it continually do something as long as something else is true or as long as a certain condition is met and I will show it.
Guys, a very basic while loop in this tutorial and then in the next tutorial. In fact, we're going to create a really cool guessing game using while loops, so it should be pretty fun. And they should have a pretty good idea of ​​how these things work. The first thing I want to do (and this is not directly related to creating a while loop) is I want to create an integer. So I'll just create an integer called index and set it to 1 now. What I want to do is create a while loop and you'll see where this index comes into play.
So to create a while loop, I can simply type while and open and close parentheses and then an opening and closing loop. bracket And this is basically like the outline, kind of the template for our while loop. Basically how this will work is here in parentheses. I'm going to specify a condition and as long as that condition is true, I'm going to loop. all the code inside these braces So inside these braces, I'm going to put a bunch of code. And as long as the condition in parentheses is true, I'll run all that code, so up here.
Let's specify a condition. I'm going to specify a condition where the index is less than or equal to 5, so as long as the index is less than or equal to 5, I want to run all the code inside here. So I'll put some code here. The first thing I'm going to do is print the index value, so here I'm just going to say that we're going to print an integer and we're just going to print the index, so we're going to print the index and in fact, why don't we print a new line every time? Here we go.
And finally after this. I want to increment the index variable so I can say that X is equal to the index plus one. Basically I'm adding one to the index at a time and I actually want to show you something in C; There will be many situations. where you'll want to do something like this, basically add one to an existing variable and in C there's a shortcut that we can use, so I can come here and I could say index plus plus and this will do the same thing. . So this will simply add one to the index.
Basically what I'm doing is saying I want to print the index and then I want to increment the index and I'll keep looping through this code as long as the index is less than or equal to five So let's go ahead and run this program and maybe you can predict what will happen . You will see it here. I'm printing the numbers one through five. So I'm printing one, two, three, four and five and let me explain exactly what is happening in this loop. We create this index variable and we set it equal to one and Basically, when C is going to run this program, the first thing it's going to do before it does anything else is look at this condition and check the condition and say okay if the index is less or equal to five.
Well, the first time you go through the index it's equal to one. So they will definitely spend less than five C. It will execute this line of code by printing one. It will then execute this line of code incrementing the index. Then what C will do is come back over here and check this. condition again, so it will say okay, the index is less than or equal to five and the index at this point is equal to two, so it is less than five, so it will run this line of code, run this line code now the index is equal to three. and C will go back to the top, check this condition again.
So before C goes through this loop, it will always check the condition first. So before you're allowed to run that code again, you need to check the condition to make sure it's true. Over time, we will get to a situation where the index increases and increases to 6, then C will rise again. here and we will check if 6 is less than or equal to 5, it will be False and we will continue with our lives in the program and those are the basics of while loops. This seems very simple and it's because all we do is specify the condition As long as that condition is true.
We're still doing all of this here and there are tons of situations where while loops will come in handy. This is just one of them where we can print similar numbers between 1 and 5. I also want to point out one thing that you might want to keep in mind which is called an infinite loop and an infinite loop is a situation where this condition up here It never becomes false. So this condition will always be true and there are certain Circumstances where you'll want to use something like an infinite loop. But in many circumstances, you don't want to go near something like that.
So let me demonstrate it so you can see if I stop increasing this. Yes I got rid of index plus plus. Now when I run my program, you will see that it prints continuously. I mean, look at all of these being printed, you can see the text editor is going crazy. This program just keeps running through that loop and is still continuing right now. So this would go on forever if I let it go on infinitely. Hence the name, so I'm just going to end this, but that's going to show you how an infinite loop works and you're definitely going to want to keep an eye on that because you're going to run into situations as you work with while loops where it's just going to go infinite and you know you have to be careful. with that, especially if you're doing something a little more advanced where you're extracting memory or you're creating processes or something that can slow down your computer. quite.
So this is a while loop and it's a really cool loop. There's also another type of loop that's similar to a while loop that I want to talk to you about and before I do that, I just want to do something really quick. So I'm going to set this index to 6. Alright, I'm going to set this index to 6 and I'm going to run my program and what you'll notice is that nothing is going to print, so the code inside this while loop is not going to print because The first thing we always do when we were in this while loop is to check this condition.
So the first thing C will do before printing something before incrementing something, it will check this condition and if it is false. He's not going to do any of this. So when I run this program, you'll see that nothing will print and that's exactly what happens. There is another type of loop that is similar to a while loop that is called a do-while loop and I am going to show you how to make one. All you have to do is take this wild thing up here and paste it down here and then up here I'll just say do and A do-while loop is similar to a while loop But the first thing a do-while loop does instead of checking this condition down here.
Does all this code run? So even though the index is equal to 6, we'll still print 6 and increment it and then check the condition to see if we can continue looping. So let me show you this and this. should illustrate what this is doing. And I actually need to put a semicolon here. So you can see here. We are printing 6, unlike before, when now we only use that while loop because we are using a do-while loop. We're doing whatever's inside. that loop before checking the condition and there will be certain circumstances where a do-while loop will be useful.
I would say that by far while loops are much more common, but you know, it's good to know about do-while loops because there are certain circumstances in which they will be useful. Basically, any time you don't want to check the condition first, you can use a do-while loop. But those are the two basic types and there's actually another type of loop that we'll talk about later in the course. It's called a for loop, but for now we're going to have a little fun with while loops and in the next tutorial, we'll create a little guessing game.
In this tutorial, I'll show you how to create a little guessing game and you'll see. This will be great because we're going to use a lot of the things we've learned up to this point in the course to build it, so let's basically get started. This guessing game will allow the user to guess a secret number, so we will define a secret number and give the user a chance to try to guess it, so if he tries to guess it, if he guesses it correctly, he will win the game and if he does not guess it correctly, then We'll let you guess again.
Well, basically we'll let you keep guessing what the secret number is until you have it here. We want to create a couple of variables. The first variable I want to create will be an integer and it will be the secret number. So I'm going to call it the secret number and why don't we set it equal to five? The next variable we want to create is going to store the user's guess, so it's also going to be an integer and I'm just going to call it guess and I'm not going to give it a value yet because, right off the bat.
The user would not have guessed a number. So what we want to do is be able to ask the user to enter the number until they get it right. So as long as the user hasn't entered the correct number, we want to keep asking them to enter it so we can do it, to keep asking them to enter it until they have guessed it and we can use something. It's called a while loop. So I'll just write while and when make opening and closing parentheses and opening and closing braces. Let's think about a good condition for our game, basically we want to keep asking the user to enter an input for so long. since they haven't guessed the secret number So it's there, we could keep doing the loop as long as the guess is not equal to the secret number, right? and basically this will always be true until they guess the correct number.
Now inside here we want to ask your guests to enter and then we want to take what they enter and store it inside our guest variable. So let's do this first, we'll say printf and I'll just say enter a number and then down here. I'll use Scanf to get any number that you enter and store it inside that guest variable, so I'll just say "guess" and this will take any number or any integer that you enter and store it inside our guest variable. Basically what happens in this loop is that every time we loop through it we get a different number and then we'll come up here and check to see if it's equal to the secret. number if not, then we will continue again and continue doing this.
But if the guest is equal to the secret number, then we will exit the loop and that means we will be down here. I'm going to print out a success message and basically say "hey, you win." Alright, cool, so you can see, it's actually not that many lines of code, I mean, it's probably like ten lines of code. Write all this down and it will look like everything is set up. So this should work correctly. Let's try to run this and see if we can guess the secret number. Remember that a secret number is five. I'm going to run the program that says: enter a number.
So let's just fail guesses two seven eight nine three so you'll notice that I can keep entering numbers and it will keep asking me to enter a number as long as I didn't guess correctly. So now let's write five. And remember that five is the secret number when I click enter, now that loop condition is going to end up being false, right? Because the guess will be equal to the secret number and it will run and say hey, you win, right? So we break out of that loop, run the print statement that said you win and the program ended and that's a little gamereally cool, so one of the cool things about this game and it's different from the programs we've written in this course. in the past it will continue to run essentially infinitely until we enter the correct value.
So it will continue and keep asking us to do something and enter a value until we enter correctly. So this is great now. It's a pretty good game and it's pretty simple. But maybe I want to make it a little more complex. One of the things that sucks about our game right now is that the user has unlimited guesses, right? Basically, you can guess every number and you'll get it eventually, right? So why don't we do this? We should impose a limit on guesswork. So why don't we say that the user can only guess three times and that if he cannot get the secret number in three guesses, he will lose the game?
So let's think about how we can go ahead and add that functionality to this little app. I will explain to you how we can do it. The first thing we're going to need to do is create some variables. I'm going to create another variable up here and I'm just going to call it guess count and Guess Count is basically going to tell us how many times the user has tried to guess the secret number and I'm just going to set this up. equal to zero because initially, the user will have guessed zero times, so remember that this guest count variable will basically be incremented every time they guess correctly, so I'm going to go down here.
Every time we go through this loop. The user is guessing. So every time we go through this cycle, I'll increase the guest count. So I'm just going to say Guest Count plus plus and that means that every time the user guesses, the guest count will be incremented and we will have an accurate count of how many times the user has guessed. Alright, the second variable that I want to make is going to be another integer and I'm just going to call it guess limit and this will tell us how many times the user can guess, so this will basically determine how many guesses the user gets.
So why don't we set this up? equal to three? I think three guesses is a pretty good limit and finally we want to create one more variable and this will also be an integer and I'll just call it no guesses and set guesses equal to zero and no guesses will basically tell us if the user has more guesses or not and This may not be very clear what you are doing at the moment. But keep watching the video and you'll see how this comes into play. This is really going to be very important. So we're actually going to have to modify our code a little bit, so down here we have this configuration.
So that the user can continue guessing as long as the guess is not equal to the secret number. But we want to be able to limit the number of times that the user can guess and in fact down here I'm going to create an if statement and every time we go through this loop I want to check and see if the user has any more guesses left. In other words, I want to see if they have reached the estimated limit, so I will do an if statement and check if the guest count is less than the guest limit, as long as the guest count is less than the guest limit.
That means they haven't guessed as many times as the limit specifies. Basically you can guess again so here inside this if statement I'm going to put all this code and I'm just going to surround this with curly braces so basically the user can only guess the number this code will only be executed when the guest count is less than the guessed limit, when the user has additional correct guesses when they have not guessed as many times as the limit specified Otherwise, however, if they have guessed too many times. Right, if the guest count is equal to or greater than the estimated limit, I'm actually going to set this variable outside of guesses equal to one, so I'm going to say outside of guesses are equal to one and basically what this is going to tell us is that if the guesses are greater than zero, that will tell us that the user has no guesses.
So essentially, if the value of guesses is greater than zero, we'll know that we've run out of guesses, and if you ever use other programming languages ​​before, this is what we would refer to as a boolean variable. It's basically storing. as a true or false value, which we represent with zero or one. So if the guest counts less than the estimated limit, he can guess again. Otherwise, we will say that the number of guesses is equal to one. In other words. Let's say they have no guesswork. However, there is one more thing we have to do: we have to come up here and we have to change this condition because now we are going to keep looping through this code while the guest is not equal to the secret number But actually there are two situations where we want to get out of the circle.
The first is when they have guessed the number correctly. Well then. We want to come here and print that you win, but we also want to get out of the loop when they've run out of guesswork. So I want to check if you have run out of guesses or not, so let's say that the guess is not equal to the secret number and the number of guesses is equal to Z. So as long as the number of guesses is equal to zero, that means they haven't run out of guesses and still have some guesses left. But if the guest counts less than the estimated limit and the number of guesses is set to one.
Then the number of guesses will no longer be equal to zero. So, let's get out of the circle. Basically, there are now two scenarios that will take us out of the loop. Either they get the word right or they are left with no guesses. So one more thing we have to do in this program is come. down here and you'll see down here. We are always printing that you win. So no matter what happens, no matter what happens. We are printing that you win and that is because before, if they managed to get here, if they managed to get out of that loop.
That means they guessed the secret number correctly, but there are actually two scenarios down here. where they would have left the circuit where there is, they were left without guesses or they guess the secret word. So I want to check which one we are dealing with. So I'm going to check for guesses and I'm going to check if it's equal to one and if it's equal to one. That means they lost and were left with no guesswork. So I'm just going to say about guesswork, otherwise it means they won, so I'm going to say You win and this will basically be like, hey, you won the game.
So this is a lot of code. I know and just run this, see how we did it and we'll see if our games work correctly and then I'll explain all the code to you. And you actually know what? I just noticed the typo below. I said by guesswork that double equals one. This just has to be a simple equal. So we are assigning value to this. So yeah, it was a small typo. It had two alike and it must be one. So let's go here. We'll run our program and it says: enter a number. So why don't we try to lose the game?
I'm just going to say two, two and now we're on our third and final guess, if I don't get it this time. That means we're going to lose the game. So I'll go in and you'll see what it says. no guesswork So we couldn't guess it in our three tries and the game told us we lost. Let's run it one more time. We'll try to win, so I'll say like two one now. We are on our third and final assumption. So if I don't get it this time, we're going to guess five and we're right. So it's going to tell us what we want, that's basically how this little program works and I'm going to walk you through one more time so that we have all of these variables here.
The secret number is simply storing the number they need to guess. The guest keeps track of what he guesses each time. The guest count will tell us how many times they have guessed. The limit of numerical guesses will tell us how many times they can guess and Out of guesses will tell us whether or not the user runs out of guesses. Below we are checking two conditions for this while loop. We are checking if the guess is not equal to the secret number and we will continue doing the loop as long as the number of guesses is equal to zero each time we go through this loop.
We check if the guest count is less. than the estimated limit If so, we ask you to guess, we increase the guest count. Otherwise, we say no guesswork equals one and that will get us out of the loop here. There are two situations, the first situation is that they are over. of guesswork, the second situation is that they won the game and we are using this if statement to check both, this is basically how this works and this shows how we can use things like while loops and if statements and also, variables together in as a single program In this tutorial I'm going to teach you how to use for loops in C.
Now a for loop is a special type of loop that we can use in C that allows us to use something called indexing. variable and this indexing variable will basically tell us what iteration of the loop we are currently in and we can use that indexing variable to do a bunch of things like we could loop through a series of elements where we could do all kinds of things inside a loop . Down here in my program you'll see that I have a while loop set up and I have kind of a basic while loop and I want to show you what it's doing.
Basically up here I'm saying that int I is equal to 1, so I give this integer I the value of 1 and say wow, I is less than or equal to 5. I'm going to print the value of I. and then I'm going to increment I, so basically this is a very simple wire loop, we have this variable I and every time we go through the loop, we print it until it is greater than 5, so let's run this program and we'll see what it does. So you'll see over here. We are basically printing values ​​between 1 and 5 and I want to point out what is actually happening, so the first time we go through this while loop we print 1.
The second time we print until the third time. We are printing 3, in other words, this variable basically tells us which iteration of the loop it is currently in, so on the first iteration of the loop it tells us that we are in the first iteration of the loop, right at the moment. The first time we go through the loop, I equals 1. The second time we go through the loop, I equals 2. The third time, I equals 3. So this variable I here basically tells us how many times we have gone through the loop. loop So the third time, it tells us 3 fourth time for, etc., and believe it or not.
This is actually a very useful thing for us when we are looping. There are a lot of situations where you'll want to know what iteration of the loop you're currently in when you're looping, so with a normal while loop I can basically specify a condition here and I can do whatever I want, right? It's very open. You can do whatever you want with it, but in many situations and there are tons of these situations, we will have a variable like I. That will tell us what iteration of the loop we are on. currently or you know Basically just a variable that will keep changing every time we go through the loop and this is such a common situation where we see that there is actually something called a for loop and a for loop allows us to take all this code and Condense it in its own unique loop.
So we take this situation where we want to have a variable like I and it basically allows us to do it much easier and much cleaner. So I'm going to show you how we can create a for loop. here below this while loop and I'm going to create a basic for loop structure. So I'll just say for. I'm going to make an open and closed parenthesis and an open and closed brace. Now the difference between a for loop in a while loop will basically happen inside these parentheses. So, inside the parentheses of a while loop, we have our loop condition or our loop protection Right.
This specifies whether or not we can continue looping through the for loop. We will have three different things. So instead of just one loop protector, we're actually going to have three different things that we want to put in here. So with a for loop, the first thing I'm going to put in here is this I variable, so you'll notice that in the while loop we have our I variable here and this basically allows us to loop through and keep track of how many times we've gone through the loop. So what I can do down here is something similar, I could say like int I and I'm not going to give this a value inside here.
I'm going to say it's equal to 1. So basically I'm going to take my variable I and give it an initial value of 1 and now I have my variable I. I have my variable that will change every time we go through the loop. The next thing I want to do I include my loop condition, so up here we have I as less than or equal to 5, that's our condition. I can put the same here. So I'm going to say that I is less than or equal to 5. The third thing I want to do is increment I so you can notice it here.
Every time we go through this loop, we are incrementing that variable. I'm going to do the same thing here in this little third quadrant. So you'll notice that I have these similar little sections. Here is the first section. We are initializing the same thing that said that I is equal to one. Here we are specifying our loop conditions, so I say let's keep doingloops as long as I is less than or equal to five and here it is. a small line of code. That will be executed. Every time we go through the loop So here I'm saying plus plus I could also say like I minus minus and that would decrease I.
I could say like I equals I plus two Etc. Like I can do basically anything I want about here. Let's keep it at I plus more for now so you can see that I was able to basically take all of this code and condense it into its own little type of loop and now, instead of having to print this and create this variable, here. I can do all of that Inside this for loop now, I can basically take this line of code, I can paste it here and we essentially have the same thing. So now this code block and this code block are at 100%. equivalent They're doing exactly the same thing So let's go ahead and get rid of all this code and try our shiny new for loop.
So I'm just going to run my program and you'll see it here. We get exactly the same as we got before. We print one, two, three, four, five. So it's the exact same program that does the exact same thing and that's why for loops are great because we can take that little structure where we have an indexing variable and we can use it with something like this, so I want to show you another situation where These four loops can be useful and we can use them to loop through all the elements inside an array, so up here I have this array that I created.
They're called lucky numbers and I'm going to take this and bring it here. So we have this matrix of lucky numbers. It has 4 8 15 16 23 42 and what you could actually do is loop through all the elements inside this array from this for loop, so I'll show you how we can do that now. Let's first see how. many elements that we have we have 1 2 3 4 5 6 so we have 6 elements in this array so what I'm going to do is say I'm equal to 0 and you'll see why we have to do this in a second, but basically the indices of the array start at 0.
So this first element of the array is at index position zero and just to review a little bit, if you wanted to access one of these elements from within the array, you could say that the lucky numbers were zero. and this will give me access to this element. If I said lucky numbers two, then I'm going to have access to this element this 15. That's basically how we can access an element inside the array, so I'm going to set I equal. to zero and I'm going to say I want to loop as long as I have less than six and six was how many elements did we have in the array?
So I want to keep repeating as long as we are less than six and I will say more more now down here. I'm going to do this Exactly the same thing, but instead of printing I, I'm going to print the lucky numbers. So I will print the array element at the index position. I and the lucky numbers Basically, the first time we go through this cycle, we're going to print out the lucky numbers zero. Because I will be equal to zero the second time we go through the loop. We will print the lucky numbers one. because I will be equal to one, etc.
And we're going to keep doing that until we get to five, which will be the last element, which is this 42. So let's go ahead and do that. So I need to return this to I and run this program. So you'll see here. Basically, we're doing exactly what I said. So the first time we went through the cycle we printed four. We are printing that first element. The second time we looped, we printed eight, which is the second element 15 16 23 and 40, so we're printing all of those. elements in turn as we go through this for loop. There are many situations where for loops will be useful, but this is a very, very, very common situation where we want to loop through all the elements of an array and print them. or you know, do something to them, whatever, those are the basics of working with for loops and I just want to say that anything you do with a for loop can be done with a while loop, you know, I basically showed you guys .
However, how do we transform that while loop into a for loop, the problem with for loops is that they are very convenient, so it's really easy to do something like this without having to go through all the trouble of creating a while loop. In this tutorial I am going to talk to you about two topics in C, the first topic we are going to talk about is our two-dimensional arrays. A two-dimensional array is basically a situation where we have an array where all the elements of the array are actually arranged by themselves. The second thing I want to talk to you about is nested systems.
Loops So we'll see how we can use a loop structure where we have a loop within loops. It's going to be great and in fact these two topics can go together very well and I'm going to show you how we can use nested loops and two-dimensional arrays to create an amazing program. So let's get started, the first thing I want to show you is two-dimensional arrays, so down here we can create a 2D array and in fact, the concepts that I show you in this video can be applied to multi-dimensional arrays, so not just two dimensions but three four or five actually as many dimensions as your heart desires.
So to create a two-dimensional array, I will simply make an array of numbers. So I'll just say int and we'll give it this. a name. So why don't we just say numbers and? Normally, when we create an array after saying the name of the array, we make an opening and closing bracket. But when we create a two-dimensional array, we're going to make two open and closed brackets like this, these two open and closed brackets will basically represent the width and height of our array, so you'll see what I mean in a second, but basically we'll have similar elements in the array and then each of those elements will be an array and we will have elements inside it.
So these two squares will allow us to manipulate all of those things. So what I'm going to do is set this equal to An open and closed brace now, this is how we would normally create an array. So normally I would just say like 1 2 3 4 whatever I can put all my numbers in here But with a two dimensional array all the elements will be arrays. So instead of just saying 1 2 3, we're actually going to create arrays inside of here. So for example, I'll put an array here and this text editor is very annoying when creating them, so for example, the first element of this array, I could just make an array, so I'll say this is like 1.
So , you can see here. I have two elements inside this array, which in turn is the first element of the numbers array. So let's do another one. We'll go down here and do another one and this is still trying to format differently. Now we're going to say three four. So this here, this whole array, that's the first element in the numbers. Right array and that array has two elements inside it. The same goes for this one. This is the second element of the number array and has two elements inside it. So it's basically an array within an array, let's do one more and this will be five six.
Now we have three array elements, one, two, three and each of them has two elements inside them. So every time we create a two-dimensional array. So we always have to specify the number of elements and then the number of elements inside each array, so in our case, we will have one, two, three elements in the array and each array has two elements inside. it looks like this, so we would say three and then two and that's basically how we create. Now let's talk about access to these items. So I'm going to create a little print statement here and we're going to print out some of these things.
So I'm going to say %D and show you how we can access individual elements. Basically, I'll say numbers and let's say I wanted to access this top left element, this is the first thing I want. What you have to do is specify the index where the value I want to access is stored. So this would be index position 0, this would be index position 1, this would be index position 2, so we'll say 0 and then I want to specify the index position of the individual element within 0, so I could say that this is element 0, this is element 1, so why don't we access element 0?
So I print the numbers 0 0 and this will print the numbers that come out. He will print it for us. You can see here we have that one. Let's try a different one. Let's try to take this right here. So this is going to be an index position one and then one, so it would be at 1 1 and now we should get that to see. Yes cool. So we get the 4. Okay, so this is basically how we can access the elements inside these arrays and I also want to point out that if you didn't want to give this an initial value, you could just put a semicolon here and you could just like manually define each index location, so I could say that 0 equals 7 or something like that I don't have to give it a value like from the beginning though in our case.
Let's do it because it's much easier. Okay, so we have our array of numbers and we figured out how we can print the elements. Now I want to talk to you about another concept called a nested for loop and you guys will see in a second why I'm teaching this along with 2D arrays, but a nested for loop is a situation where we have a for loop and inside that loop we have another loop, so I'm going to show you this real quick. . Let's say we create a for loop. I'm going to create two variables here in I and int J and I don't know if I showed you this in the course yet.
But if I want to declare two variables, I can just say comma J and that will declare both variables. I'm not going to give you real values ​​yet. We'll do it within these four loops. So I want to show you how we can use a nested for loop to print all the elements inside this two-dimensional array. , then I'm going to say that I is equal to zero? and we're going to keep looping as long as it's less than Three and the reason I say three here is because that's how many elements are inside this array of numbers, so I'm going to keep looping as long as it's less than Three. three and then I'll say more more.
Now, inside these curly braces, I want to create another loop, so every time we go through this iteration of this top loop, we're going to completely run another loop, so I'm going to say, because J is equal to 0, J is less than 2 and The reason I say 2 here is because that's how many elements there are inside each array inside the array of numbers and you'll see that in a second. Because? this is going to work and then I'm going to say J plus more, so now I'm going to do more open and closed braces and down here.
In fact, I'm going to be able to print all the elements inside this array. So I'm going to go ahead and do that. I'll just say printf and we'll print an integer because these are all integers. and we're going to print numbers and I'm going to print numbers I and J. So whatever the value of I and the value of J, that's the position of the index inside the numbers that we're going to print and now just so this is formatted a little better. Why don't we put a comma? Right there I'll also put another printf here that will just print a new line.
And you'll notice that this printf is outside of this inner for loop, so I'm going to run this program. We'll see. What are you doing. You guys will see exactly what is happening and then I will guide you a little further. What's going on? Let's run this program. And you'll see here when we run the program. Basically, we are printing this entire 2D array. So I'm printed 1 2 3 4 5 6 So let's talk about why that happens. Here I'm saying that I is equal to 0 I is less than 3 I plus plus So I'm going to go over this four loop three in particular. times I'm going to run all the code inside this four loop right here Three times is as many times as there are elements inside our 2d array.
Now, every time I go through this top loop, every time I go through it, I'm going to run this loop in its entirety. So I will go through this cycle to the end. I'll go through all of its iterations and this loop says that J is equal to 0, J is less than 2 and 2 here is how many elements are inside each of these increments. ? So this element in the noms array has one, two elements inside. This element in the number array has one, two elements inside, right? That's where these two come from. Then I'm incrementing J. And I'm going to print, so I say print F and I'm printing numbers at index position I and index position J, so the first time we go through this for loop I will be equal to zero and remember that The first time we are going to go through this loop twice.
So we'll print the numbers 0 0 and the numbers 0 1 and then Going down here and printing a new line the next time we go through this loop I, we'll go down here and print the numbers 1 0 and the numbers 1 1. Finally, the third and last time. We're going to print numbers up to 0 and numbers up to 1. That's basically how it works. So two-dimensional arrays and nested for loops are a perfect combination and there are many situations where you know, other than just looping through 2D arrays, that we're going to use nested loops. But I hope this gives you a little introduction. what they are and why they are useful In this tutorial I will talk about how to access memory addresses in C.
Now, in theC programming language many times we will want to store different information. Okay, and there are There are a lot of different ways that we can store information. We can use things like variables, we could use arrays, we could use structs, but the basic point is that whenever we use c we're going to want to keep track of and maintain a bunch of different pieces of information and one of the easiest ways to do that is by creating variables, so down here you'll notice that I have a bunch of variables. I have this integer called age and it just has the value of 30.
We have this double GPA. 3.4 and we have this degree of character and it has the value of one character. I have a ton of different variables in my program and it allows me to keep track of data very easily and I can use them to maintain and I can even modify these different information variables it's great. But I want to talk a little bit about how they work in the actual physical memory of our computer. So whenever I create a variable, for example when I create a variable called age and give it a value of 30, this value 30 is actually stored in our computer, so all computers have memory, so Many times you will hear people refer to this as RAM.
It is called random access memory and basically RAM is the memory that your computer will use. use when running programs. So for example, if I ran this program in C, my computer would use Ram. You would use that memory to store and keep track of all this different information. Correct. So when I create a variable like int age C will actually store this value 30 in a specific memory location so it will take that value 30 and store it somewhere in RAM just when I create this double called GPA C will take this value . 3.4 and will store it within the physical memory of our computer when it creates this grade C will store this character in the physical memory of our computer.
Now, when I create these variables, I give them descriptive names, right? So I gave this a descriptive name so I know what's stored inside, right? And when I want to access this value, I can just reference the name, right? I can just refer to the age that I could modify and I could print it. I could do something else. I can do whatever I want with it and the way I can access this variable, in other words the way I can access the value 30 is by simply referencing the variable name, the same thing happens here if I wanted to access the grade.
Right, if I wanted to access this capital, I could just refer to the variable name. But again, here's the thing. This information is stored in our physical memory somewhere whenever C needs to access that information whenever our computer needs to access this value 30 or this value 3.4. In reality, it will refer to a specific memory address. So when I want to access this variable age, I can just type age and then I can use this variable. But when C wants to reference this value, it's not actually using age. It will use the memory address where this value is stored. from our physical memory So all these values ​​are stored inside a physical address in our memory So all these values ​​have an address where they are stored in the memory and when C needs to access them it can use that address So I am I will show you how we can access them to that address, how can we print the address on the screen.
And basically I just wanted to give you an introduction to what memory addresses were. So down here I'm going to say printf and I'm going to show you how I can print the physical memory address. Where is each of these values ​​stored on our computer? So down here if I want to print a memory address. I need to use % and P, so normally if I were just going to print as a number, I could say %D or I can say %C for a character when we want to print a physical memory address. Let's say %P and this actually means pointer.
We are not going to talk about pointers in this tutorial. We are only going to talk about memory addresses. But you should know that you need to say %P and now I'm going to type the name of the variable whose memory I want to access. so I will access the old memory address and Before variable. I'm just going to say ampersand so I'm going to do an ampersand age and I'm going to say percentage P and now we should be able to print the memory address where the age variable is stored. In other words, we can print the memory address where this value 30 is stored.
So let's go ahead and run our program and you'll see it here. We get this number. So I get this number 0 0 6 0 FF 2 0 0, so it would be like a hexadecimal number. I guess it's not technically a number. It's like I think it's hexadecimal. But basically this is the physical memory address where the value of 30 is stored, in other words, it is the place where C stored the value inside this age variable. You could do the same with these other variables, so why? Didn't I format this a little? I'm going to stay here and then I'll make a new line and we'll just say GPA and again, I'll print another one of these addresses and I'll make one for the grade and we'll print the address, so now I can come here and I can modify this a little bit so I can say GPA and Grade and you'll notice for each of these.
I am using this sign before writing the variable name. So now when we run our program, we should get a small list of all of these variables and their corresponding addresses, so you'll see here that we have the age and it's stored. memory address up 0 0 6 0 FF OC GPA is stored in this memory address and grade is stored in this memory address so if you want to enter our computer or enter my computer and Go to the address of memory is 0 0 6 0 FF 0 C which is actually going to store the value of 30, it's going to store the value that is inside that old variable.
If you went to this memory address, you would see the value Inside GPA, if you went to this memory address, you would see the value Inside GPA. I see the letter which was a capital A, so basically all these variables are stored in different memory addresses when I want to access the variable information. I can just refer to the name of the variables so I can say an age or I could say GPA. But when C wants to access the information and the variable it has to use these addresses, then C would use this address. Would C use this address and this address?
I can use variable names and that's why C is amazing because it allows us to do things like that. But when C and our computer wants to access these values, it has to use these memory addresses and that's why they are useful and when we are programming and in C, it can actually be quite useful to be able to know the addresses, like the actual physical addresses of these certain variables in memory. So a lot of times in C, when we want to work with these variables, we can just query them. them by name and we can do all kinds of things with them.
We can access them, we can modify them. But in other circumstances, we're not just going to want to be able to access the value. We'll also want to be able to access its physical memory address; In other words, we're going to want to know what that's like. The address is and we can use it for a lot of things. In future tutorials, I'll talk about why this can be useful, but for now I just wanted to give you an overview of what memory addresses are. we are using them in our program and how can we print them, so again I just use this sign and then I type the name of the variable and use this percentage P to print it.
In this tutorial I want to talk to you guys about pointers in C Now, a pointer is basically a data type that we can use within our programs. So up to this point in the course, we've been using a bunch of different types of data, right? We've been using things like integers and an integer is just an integer. We've been using things like doubles and a double is a decimal number. We've been using characters and a character is basically just a character and now I want to introduce you to another type of data which is called a pointer and a pointer is basically just a memory address and a memory address basically refers to a physical address within the memory of our computer where we are storing a value. true and I think the advice tends to confuse a lot of people who learn about it and this is one of those topics that everyone fears because, you know, traditionally it's very confusing to understand, but actually the advice is extremely simple and the problem is that simply become too complicated.
So what I want you to do is think of a pointer as a data type. That's all. It is just a type of data that we can work with in our programs. It doesn't have to be more complicated than that, right? I can work with an integer in my program, an integer is an integer, right? I can work with a double in my program a double is a decimal number I can work with a character in my program a character is just a character I can work with a pointer in my program a pointer is just a memory address That's it.
A pointer is simply another type of data that I can use and work with within my programs and it happens to be a memory address within memory or RAM on our computers. That's all. It's very simple, people complicate things too much. pointers and they don't have to be too complicated because it's just another data type. Like an integer or a double, but instead of being like an integer or a decimal number. It is a memory address. I hope that makes sense and I hope you think not to try to overcomplicate this because it doesn't have to be that far down here in my program.
I have an integer that I created and it's called age. So I said in age and within this whole number. I'm storing an integer I'm storing an integer in the right third And if you've followed this course in the last tutorial we talked about memory addresses and we talked about how all these variables actually store these values ​​at physical addresses in our computer's memory , so down here I have this printed statement and I'm just printing the ages memory address and I'm using this percentage P and I'm just printing the sign age and we talked about how this sign when Put this in front of the variable basically.
It will give me the physical address in memory where this variable is stored, in other words, where this Thermia value is stored. So if I run this program, you'll see it here. It says old memory address and it's this hexadecimal number, right? This is the physical memory address of eight, the variable age. What did we just talk about earlier? Remember what I told you about pointers? Memory addresses, that's what they are, it's a data type, a pointer is a data type in our program and it's a memory address right when I use this sign and type the name of a variable.
This gives me the memory address, right? I can print the memory address, so actually this is a pointer. This is a data type in our program. That is a memory address just like an integer is a data type. That's an integer and a double is a data type. That is a decimal number and a character is a data type. That's a character. A pointer is a data type. That's a memory address and this here. This guy here is a pointer. So it's just the memory address of the variable age, that's it and when I want to print a pointer I can use this percentage P and it will allow me to print it to the screen and everyone will be happy.
Basically, what I'm doing. here I am printing a pointer when I say percentage P. I am telling this print function F that I want to put a pointer and then I give it a pointer. Here I give a physical memory address. That's what a pointer is now. We understand and hope we understand that a pointer is just a data type, right? It's just a memory address and we can access the memory addresses of specific variables by using this sign and then typing the name of the variable and that can be quite useful. Now that we understand that, I want to show you how we can work with these. pointers, here I have an integer variable called age and this integer variable stores an integer value 30.
I'll say it one more time. I have an integer variable and this integer variable stores an integer value 30. I could also create a double variable so I can create a double called GPA and inside this double variable. You could store a double value like 3.4. You could also create a character variable called rating and inside this character variable. I can store a character and I could also create a pointer variable and inside that pointer variable I could store a pointer. Okay, so like I stored a character inside this character variable and I stored a double inside this double variable that I was able to store. a pointer inside a pointer variable so that we can store these pointers inside a pointer variable.
Now here's where this is going to get a little complicated. Whenwe create a pointer variable, we actually need a physical memory address, so when I create this Integer variable like I can get a number out of my head, right? I can just say like 30 when I create a double variable. I can store any number I want to store, but when we create a pointer variable, remember that we are going to store a memory address and I don't necessarily know any memory address like that. At least off the top of my head. I don't know of any significant memory address.
So when I create a pointer variable, what I want to do is store the memory address of a variable that is already in a program. So I'm going to say that once again, when I'm creating a pointer variable inside that pointer variable, I'm going to store the memory address of another variable inside our program, so let's say I wanted to create a pointer that would store the address of memory of this variable here so that the memory adjusts to the age, right? I can create a pointer like this so I can say int and asterisks and now I'm going to give this pointer variable a name and remember that this pointer variable is going to store the memory address of the age. variable and generally when you name a pointer, you will start with a lowercase P and then write the name of the variable whose memory address you are storing in the pointer variable.
So I'll say P correct age because this pointer variable is storing the physical memory address of the age variable. Now what I want to do is set this equal to the memory address of the age variable which we can access using this ampersand so I could say ampersand so now this pointer variable stores the memory address of the age variable. I'm going to do the same thing here for this GPA. So if I wanted to create a pointer variable that would store the memory address of the GPA variable. I could say double asterisks and I'll just say P GPA like this and I'll set this equal to the memory address of this double variable that we can access using this sign and then the name of the variable like this.
I'm going to do the same thing down here for this character variable. So I want to create a pointer variable that will store the memory address of the character variable, so I'm just going to say char Asterix as qualification, and I'm going to set this equal to Ampersand degree. Well, that's all I'm doing, right? So now I have this integer variable and I have this pointer variable that stores the memory location or the memory address of the old variable. I have this double variable that stores a double and I have this pointer variable that stores a pointer and it turns out that the pointer is the memory address of the GPA variable.
I have this char variable down here. And then I have this pointer variable, which stores a pointer that happens to be the physical memory address of the big variable in our memory. Okay, and that's basically all you need to know about pointers to get started. a pointer is just a data type. It is just one type of information that we can work with in our programs. It turns out that they are memory addresses. Write a whole number. It is a type of data that we can work with in our programs and it turns out that it is an integer, a double is a type of data that we can work with in our programs and it is a decimal number a character is a type of data that we can work with in our programs and it is a character a pointer is a type of data that we can work with in our programs and it is a memory address Hopefully that makes sense and just as we create integer variables and double variables, we can also create pointer variables, the only difference is that every time we create a pointer variable we will store the memory address of another variable in our program.
So you'll see more here when I create this pointer variable. I am using the data type of the variable whose address I am storing, so here when I store the address of an integer variable, I say int here. When I store the address of a double variable, saying double here when I am storing the address of a char variable. I'm saying char here and that's the basics of using pointers and also creating pointer variables and storing memory addresses inside variables. In this tutorial I want to talk to you about pointer dereferencing in C Now, a pointer is basically a data type that we can work with in our programs.
That is a memory address. So sometimes in our programs we want to work with memory addresses. We'll want to use them in our programs and do certain things with them and those memory addresses are basically called pointers in our programs and when you dereference a pointer. Basically, what you're doing is going to the memory address of that pointer and taking the information from there. Okay, so all the dots are our memory addresses and when we dereference a pointer, we go to that physical memory address and we get the information or we get the value that's stored inside there, so I'm going to show you how we can do that. .
That and you'll see that it's actually very easy and can be quite useful, so down here. I have this integer and it is an integer variable and it is storing the integer value 30 and more here. I have a pointer variable that stores the memory address of 30, so it stores the memory address of our old variable, as you can see here and down here. I'm just printing this pointer. So I'm printing the pH. I'm using this percentage of P and just printing it to the screen. So why don't we take a look and see what we get?
So you'll see here. We just got this memory address, probably exactly what you were hoping to get, so here I want to show you how we can dereference a pointer. So this pointer variable, the page stores the memory address of this old variable. , good? So when we printed it, we were basically given the physical memory address where the number 30 was stored. So the page stores the memory address of 30. It's storing the memory address where this value is. Inside our computer's memory if I want to delete the reference. a pointer Basically what I'm going to do is go to that actual physical memory address and grab that information.
So the way I can dereference a pointer is by using the asterisks. So when I'm printing this point, I can dereference it using this asterisk. But now since I am dereferencing this pointer, I need to print a number instead of a pointer because when I dereference the pointer it is no longer a pointer. This is no longer a memory address. It will be whatever is stored at the memory address. The pointer was pointing to, so this will be an integer because this pointer variable was storing the address of an integer. Okay, so when I delete it, this will actually be an integer.
So let me show you guys, I'm going to print. or I'm going to run our program and it should print 30. So you'll see that we get a 30. Basically what's happening here is we're dereferencing the pH pointer variable, we're dereferencing the pH pointer, which basically means that Let's go and we take the actual physical value that is stored at the memory address that the pointer was pointing to and that's why we get 30. And that's basically what dereferencing is, you know, you have a pointer. You can dereference and you basically get the value it was pointing to. That's essentially all we're doing, so I want to show you how we could do this.
Normally, for example, I have this age variable if I write this. I wanted to get the address of this age variable. You could use this sign, right? That's basically what we did here, but what I could do is just remove the reference to this so I can say the age of the ampersand and then I can put an asterisk here and this will actually remove the reference to all of that. So now when I run my program, it prints 30, so it won't print the memory address. Will only print 30 so you can see here. We get 30, so I could do this as many times as I want and this maybe just illustrates what's happening, so I could put an ampersand here and now I'll get that memory address again.
So you'll see we are. getting this memory address and I can do the same thing so I can dereference this one more time and you'll see that now we're getting that value 30 again, so basically what I'm doing is constantly like here. I get the memory address where the age variables were stored. Then I'm dereferencing and getting the value. I get the memory address again and then they dereference it again. So, you know, I hope that makes sense when you dereference a pointer you're basically getting the value that that pointer points to or you're getting the value that's stored at the memory address of the pointer.
In this tutorial I want to teach you how to write two files in C. One of the interesting things we can do in C is that we can modify, change and also create new files. In this tutorial, I'm basically going to show you how to do it. We'll talk about creating files, writing files, and adding files to files. So let's go ahead and do that here in my main function. In fact, let's create a file. So I'm going to create a file and I'm going to store information within that file and the way we can do that is we can just write the file all in uppercase like this and essentially what we're doing here.
We're creating a pointer to a physical file on our machine, so I'll just say file and make an Asterix and give it a name. So I'm going to call it F. Pointer and that's just going to represent the file pointer, and I want to set it equal to F Open and F open is actually a function that will open a file for us. So, inside these parentheses, I'm going to give you a couple of different parameters. The first parameter will be the name of the file that I want to open. So I'm just going to call this employee dot text and the second parameter is going to be a file mode and a file mode will basically tell C what we want to do with the file that we're going to open.
And there are a lot of different file modes. The three most basic types are R and R stands for reading. So if I put R in there, I mean we want to read the file. W means correct so if I put right there it means I want to create a new file or write over an existing file and there is also a which means add and that means I want to add information to the file in this tutorial we will look at W and a so what What I want to do here is put W and Right. now employees punctuate the text.
This file does not actually exist. So what I'm going to do is create that file. We'll put information inside that file and then that file will be saved on my machine and so we can do that. We will need this writing mode. So it's just going to be a W. Every time we open a file, you always want to make sure you close the file, so here I say F open. And down here I want to use another function called F close and basically what this will do is close the file, so it will delete the file from our computer's memory and if we make any changes, it will save the file. and everything will be like locked, so you always want to make sure that when you're working with a file, you close it and inside this close function F.
We just want to pass this pointer F. So I'm going to say Pointer F right there. So let's talk about what's happening here. File is basically like Essentially like a data type, I guess, and we're creating a pointer to a physical file on our computer. So if you remember, a pointer is basically just a memory address. Correct, a pointer is a type of data that we can use in our programs. It's essentially just a memory address and this pointer stores the memory address of this new employee points text file that we're going to create, so essentially what's going to happen is we're going to create the employee points text file. this employee and the pointer F is going to be pointing to the memory address of that file on our computer.
That's essentially what's happening here. And what I want to do is write information to this file. So let me show them to you real quick. What's going on here? So without doing anything else, I just use this line here when I run my program, it will create a new file called Employee Point Text, so here I am in my little directory and if you don't specify an absolute path or as a relative path to this. It's just going to create it in the directory where your C files are located. So if I wanted to create this on my C drive, I could say like C Whatever and then like employees text and this will be created. on my C drive, but if I don't specify an absolute path or even if I don't specify a relative path, this will just be created in the same directory as our C file and you'll see it here.
My main dot C file is here. It's in this folder. So when I run this program, let's look here and we can see this employee query text file is created. So I will runmy program and you will see here that this employee file was created. So yeah, actually, show this a little bit. You will see that it is a text document. Your employees have taxes and there are zero kilobytes inside this, so we haven't put any information in there. But this file was created. So now let's talk about putting some information into that file inside this file.
Basically, I'll list a group of similar employees who might work in an office and the easiest way to write information to a file is to simply say F Print F and this is a special function in C, which actually works very similar to printf function. So if you've used the printf function and you're following this course, we've been using it throughout the course, the printf function basically allows you to print information in that console window. F Printf, can you allow us to basically just write information to a file? I need to give this a couple of parameters.
The first one is going to be this file pointer, in other words, I need to tell this function, you know what it should be written to, so I'll just say pointer f and then here we can give it some information so it can just use like after we put the pointer of this archive. I can use this like I normally would with the printf function so I can print like some employees. So you could say that one of the employees is Jim. And we will put what they do. So I can say salespeople and then we could say, let's make a new line and we'll create another employee, Pam and she's the receptionist, and then we can create other employees, so now I have three employees that I'm basically going to write in the file of my employees.
So when I run this program and we can go here to this employee file, you'll see when I open this that we have these three lines here. We have Jim, the salesman, Pam, the receptionist, and Oscar, the accountant. Basically, I was able to create this file and write all this information to the file from down here just from down here in this program. That's great. And I want to show you what it's like when I use this, right? This will actually overwrite a file. So, for example, if I change this to from all this text and I just put Overridden here now when I run this program and we open the text file with points from this employee.
So let me reopen this. You'll see it just says overridden, so I actually overridden all the text that was there. And that's basically what happens when we simply write to a file. So let's go back to what we had before. I'm going to run this again. So we have this text. And you'll see now we're back with the employees, so let's say inside this file, we wanted to add an employee at the end, so we wanted to add a new employee to this file, right? Well, I can use something called append and what append will do is allow us to add a new line to the end of this file or just add text to the end of the file instead of saying w I'm just going to say a and you'll notice that I still keep the text of the employees here and down here within this F printf.
I'm going to print another employee to the file. The first thing I'm going to do is print a new line because, in fact, let me show you this in this employee file. Actually, there is no line after this last line, so the last similar position in this file is here. Actually, I can't access the line below Oscar. I'm going to want to go ahead and print a new line and this will move the next employee to the next line, we could say like Kelly and she will work in Customer Service. So when do I run my program instead of overriding what was in the employees text file?
This will simply add this new employee to the record. So I'm going to come up here, I'm going to run the program and we're going to open up this employee's file and you'll see that now we have another entry, Kelly, from customer service. So when we write a file, we create a new file and add content to it or just override the content of the file. At least we use this F printf function and when we add, we upload pending information to the end of the file. So using those two things, I mean you can basically write whatever you want and it's important to note that you can write more than just a text file, so you could write as an HTML file or you could write as a CSS file.
I really mean any type of file that you want and that's basically how we can write to a file and add it to the end of a file. In this tutorial I will not teach you how to read information from a file in C. In the last tutorial we talked about writing to files. So we looked at how we could create a file, overwrite a file, and add information to a file. In this tutorial, I will show you how you can read information from a file. here you will notice that I have my file pointer set.
So I just say file and the asterisks and I called it FP and remember this is just a pointer you know and a pointer is just a memory address and it's the memory address. of this file in memory, so it's the memory address of this employee dot txt file that we created in the last tutorial and I'm using this open function F which basically opens this file and then I'm specifying a file mode of the what we talk. file mode W and file mode a now I want to talk to you about our file mode and this means read.
So when I use our file mode, I can read information from the file and this can be pretty cool. So in order to be able to read the information in the file, I'm going to show you how we can read the individual lines from the file. So let's first look at the file. This is employees dot txt. It just has, you know, basically a group of employees, their names and then what they do. We'll be able to read this file inside our program, so the first thing I want to do is create a variable and I'm going to create this variable here before I create the file pointer and it's going to be just an array of characters and I'm just going to call it line and give it a initial size of 255 to be able to store 255 characters and then down here to be able to read this file.
I can actually read the file one line at a time and we can use a special function called F Gets and F gets will basically let us read the lines from the file one by one so I could say F Gets and I need to give this some parameters different, the first parameter I need to give it is a place to store the information it gets. So this function will go and read the information from the file and store it somewhere. The first parameter that we must give to this function is where it should store that information, so in our case we will store it inside that alignment variable that we created there.
The second thing we need to give it is a size and it's basically going to be like The maximum size that we can read at, so I'm just going to say 255. Basically, this number needs to match this number here and then we also want to give it that file pointer, like this which I'll just say f Pointer and basically what this will do is read the first line of the file and store it inside this line variable. So if you came here and said print F and you printed a string and you just printed the line now, when you print a line, it will actually have the value of the first line of the file.
So let's run this program and you'll see it here. We are printing salesman Jim. And if you don't remember that was the first line within the employee's points text. Basically what F does is read the first line. And another thing that he's doing is moving this pointer, so remember that this pointer is essentially pointing to where the file is in our memory and every time we use F, this pointer will be incremented. So basically it will move past the first line of the file. So if I were to use this again? For example, if I say f is reused when I use this, it will store the value of the first line of the file inside the line.
And when you use it again, it will now take the second line of the file. So if I print this now, this should store the second line of the file. So when I run my program, you'll see that it's storing the second line, Pam Receptionist. I can basically do this for all lines in the file. This is basically how we can use F to read the individual lines within a file.

If you have any copyright issue, please Contact