Transcripts
1. 1 Introduction: Hello and welcome this new
course in which we will teach you C programming
for beginners. This is actual form
educational engineering team. Learning C programming
will help you get career all change
your career path. You can become a better
developer and you must know that c is the number one programming
language in the world. You might know Python. Python is based
on C programming. And you can look this up
using any search engine. In this new course, you will learn C
programming from scratch. We'll start with the
basics of C programming. Let's talk about
conditional statements, NC. After that, we will cover loops, different types of loops
like while for and do-while. And we will take examples. We'll talk about C
functions and arrays. Then we'll move to more
advanced topics like pointers that are
modifiers, datatypes, Nc. At the end we will have our processor directives and
dynamic memory allocation. Each of these sections will have more than five assignments. Last two quizzes to make
sure that you understand each of these aspects
will give you examples. And we'll do examples together, which is basically a
practical coding exercises. And there'll be a homework. I'm sure that at the
end of this course, you will understand
the fundamentals of the C programming language. And you can easily
create C codes. You will learn how to create
even your own library in C. We'll also cover how to apply. See in real time programming, projects, problems,
and functions. You will learn how to write
high-quality code in no time and you will learn
the best programming or C programming practices. This is a comprehensive
course that will be updated in weekly basis. We will follow up
with our students, answer their questions and help them with the assignments. Of this course, there will be
a project that you must do, NC to make sure
that you understand everything we explained
during this course. Thanks for watching
this introduction. See you inside.
2. Download and Install C Editor and Compiler: Hello and welcome
this annulus all in which we are going
to download and install the software
that we will use to write codes
in this course. Now they're out of
software that you can use to write C codes. But the one that we
will start with, the one that we would
like you to start with is called Notepad Plus, Plus. As you can see, it's
basically a notepad. Simply click the Download and
choose the latest version. Choose depending on
your operating system. As you can see, Support for almost all of the
operating systems. Simply hit the Download button. And you'll download will start. Now click once to start
the installation process. Choose your language. Click Okay. Now click
Next. Click, I agree. Next, Next. And you can create a
shortcut on the desktop. Then click Install. Won't take long. It's a very small software, simply lived on and hit
the finished pattern. Now here we will
write our sequence. Now in this white area, you can choose the language
that you are writing for from that language,
drop-down menu. Simply go to C and select C. Now, you are good to go
regarding writing codes, but we also need to
compile our code. In order to combine the codes, we need to use a compiler
called GCC compiler. So again, go to google.com. And in here, right,
GCC compiler. As you can see, GCC
compiler collection. You can open it up. And here you will find
a lot of information and resources about
this compiler. This compile includes
front ends for C, C plus plus Objective C, Fortran, other goal, and D, as well as libraries
for other languages. Now, you can select
any of these versions. But let's finish, feels
overwhelming for a lot of people. So we will go with
direct installer. So go back to Google and
write glenn GW C compiler. Once you like that, you will see that we have sourceforge.net.EXE file that we can download and install. And we also have the original site called
Men GW dash W 64, that all this is it. Now you can go to the
download section, make sure that you are
inside this website. Once you're inside
the download section, scroll down until you
reach this point. Men, WOW, 64 builds
running for Windows. The support C, C
plus plus fortran. Click once. And here you will find that
installation location. As you can see, men GW builds. Click on the source
fall. And here it is. Now, make sure that you are
installing the software from dislocation
source for this name. Click once. Click Yes. Now click Next. Leave it to the default. Click Next, change the location. We need it to be inside the
sea directory and leave. Everything is as it is. C, D w dash, W 64, slash or backslash.
Now click Next. Now make sure that you
normalize dislocation. Click Next. And it will start
downloading and installation of the
compiler files. Now there are other
options to write C codes, but this is the first one, and we'll explain all of
them in this section. Now, you are free to choose which one you
want to go with. Now once you are done, click Next and Finish. Now, if you went to that original folder in which
all of the files were sold, it's inside our C directory. As you can see here. And here we have the main GW. As you can see. Here, we have the flat file. This is the original
file that we need. We need the bin folder. So make sure that you
are inside this folder. See mainly WOW, 64, when GW 32. Click once and
right-click, then copy. Now we need to make
windows understand that we have a C compiler installed. Now, you need to go
to your desktop, right-click on your PC. Select probabilities. Now, select advanced
system settings. And from this window you can select environment
variables. You will see this list. Go to the system
variables down here. Scroll until you reach the path, then hit the edit button. Now, this cell is too fast. And what we need to do
is to add a new path and paste the path for our
GCC compiler inside here. Now once you do this, you can simply hit
the Okay button. And this is how to make windows understand that we have a
C compiler and installed. Click Okay. And Okay, and close this window. Now you can go back
to Notepad Plus, Plus and write an essay
called similarly like void. I will add anything like n, n. That's why the void here. And let's include a library. We will talk about that
in the coming lessons. Now, once you are
done with this, you should be able to go
save your file as a C, fine C. And you can go to the File, select Open containing folder, and simply select CMD. Now you will see the Command
Window or not you need to do is simply write GCC, this dot c. And as you can see,
the code did compile. Now if you run GCC, as you can see, fatal error,
combination terminated. This means that we have the
GCC compiler installed. Now if you want to
print something, you can simply write print f, hello and save the file. Go back here. Gcc. And once
you have compiled the file, you can't seem to write a EXE. And you will see the printed
word. As you can see. Hello. Andy will explain these
steps in the coming lessons. But this is how
to make sure that your compiler is installed. Now, we will use Notepad
Plus Plus and GCC compiler, the command window, the
cmd to compile our files. And if you face any problem, I'm here to help, please for me. And I'll help you
install the software. And the next two or three
lessons we will explain different options
to write C codes, not just using
Notepad blast, blast, but we have other
options as well. Thanks for watching this lesson. See you next.
3. Write and Compile C Codes in your browser : Hello and welcome
this new lesson in which I'm going to show you the second and easier option to write and compile C codes. Go to animal browsers and
lie right online gdb. This is the word online, gdb. Now once you do that, you will see the first result outside called online gdb.com. Click once and you
will see a compiler. As you can see, this is the
area where you write codes. And from the top
right menu here, select the language, simply
hit the C language and run. Now once you run this compiler, you can see that word
would print f hello world. So it will print hello
world down here. So this will be
your command window in which you will see results. And this will be the area
where you write code stuff. You have any problem
in the code. It will indicate that in
this window, as you can see, it's a very proficient on online compiler
that you can use. Now, what we recommend is
that you go and look in using your Gmail
account or sign up with your Gmail account or Facebook
account or GitHub account, or simply by writing an
e-mail and password. I usually prefer
going with Google. So we will sign in
using Google. Now. Once you sign in using
your Gmail account here it's asking me
for my security code. So once you are n, similarly write the code. Once you are in, you will see a project's window or tab here. Or you can create a new project. Now if you created
a new project, you can easily save the new
project to your library and you can go back to
that project anytime. So simply hit the save button, name the project distinct. Click Save. Now we have a new
project called testing, and you can create more than one file
inside the same project. So let's call this test. So now we have a C project
with more than one file. Now, once you hit
the save button, you can go to My
Projects and you can see that testing
project that you made. This is a very, let's say,
comprehensive platform. It's for free as well. And you can access it from
your mobile phone if you don't have a laptop
and you want to start coding in C. Now what we will do is we will
create a classroom and we will nancy link
to this classroom so that you can submit the
homeworks for this course. Inside this cluster.
As you can see, you must create an account in order to submit the homework, save your projects,
and save your work. That's it. Now, once the
homework is submitted, will explain to you how to access the homework
and how to solve it. Thanks for watching this lesson. If you have any questions
about how to create a new project and how to like a C code inside this platform. Feel free to ask this.
4. Download and Install Visual Studio Code To run C programs: Hello and welcome to
this new lesson in which are going to show
you how to download and install Visual
Studio Code and how to enable C and C plus plus
programming using the software. So the first step is
go to our browser and right visual Studio Code. Now, as you can see, it's basically a software
provided by Microsoft. You can simply download for
Windows by clicking here. Now, as you can see, this is a quick overview
of the software itself. And it's asking you if the
download not starting, try this direct link. Now once you click
on the direct link, the download will start. As you can see here. I will use another software
to download it. Now as you can see,
the downloads started. It won't take long. It's about 76 megabytes. So after that, you will have the Visual Studio Code and setup file and we just
need to install it. Now, Visual Studio Code
is a software used for writing course and other
programming languages like C plus plus C-Sharp, Java, Python, PHP, and go. Click Open, click Accept,
next, next, next. And if you want to
create this icon or add open with code
action to the File menu. You can click on this. Then install. And
as you can see now, it's extracting the files. Then it will start the
installation process. Usually, it doesn't take long. It's a very light software, but it has a lot
of capabilities. Now, as you can see, we have the software installed, click launch Visual Studio
Code, and hit Finish. Now once you are here, then you are good to go. You can choose the
look you want. We can go with the
light or the dark. Now, since I'm recording videos, going with the light one is
better than going with a dark one or the
high contrast one, but most programmers
prefer the dark one. Now I will choose the
right because it will show you the codes in
a more clear way. Now, once you are done, you can also browse
color themes. There are other themes as well. Now you can complete this checklist or go
to the next section. As you can see, redefine
your editing skills. You can open editor playground to change different things. Now, you can also go here to this area and
create a new file. As you can see,
or open a folder. Now once you have the
software installed, then you can go
to the next step. The next step is to
install the extension C, C plus plus extension. So go here to the extensions
on the left side. In the search bar, right? See hex connection. Now as you can see,
here, we have the C, C plus plus, saying extension. The first one, and this
is the second one. This is the third row. Now we need this one. We don't need this one. Just make sure that
you have the one that says CC plus plus Intel. Since debugging and
called browsing. Now once you are inside this, you can click the Install and then installation
process will start. Now, you need to wait a few seconds for the
installation process to finish, because now it will call the extension for
Microsoft servers and download it and install it
or on your operating system. Now, once you see the
uninstall button, it means that we have
the extension installed successfully and we are
done with this step. Now, the next step is to
install the C compiler. We already did that
and appear as lessons, so we will do it
quickly in this lesson. So go right, men, GW, dash W 64, and go
with this link. Now we have more than one. You can choose that minimalists. So manger, WOW, 64 minimalist and click once on the
source form here. And you will see
the download page. Click the Download button. Make sure that you have
this name on your browser. And hit the download. Now it shouldn't take long
as it is an online compiler. So here it is. Click once. Now you need to follow my
steps as it is click Install. Now once you click Install, you need to choose what do
you want to save this file. So let's call it when GW. Or you can add 2020. Whatever you want. I will
leave it as this man, GW, and click Continue. Now make sure that
you have dislocation, memorized or copy it. Now click Continue. Now let's you seconds for it to call that list of things
available on the server. Now click Continue. You will see this
list. Here. You can see the list of things
available for you to download. We need to download this
map for a solution, and we need the GCC,
g blasts blasts. Now, make sure that both
of these are selected. And once you are done, go to the installation
and hit Apply Changes. Now it will ask you if you
are sure, click apply. And it will start connecting
to the server and downloading and installing
all of these components. Now that shouldn't take long. Now once you are done
with the download and installation
of GCC compiler, you need to go and sit
the environmental path. So make sure that you go to the Start Menu and right
environment and hit here. Now sometimes these patterns
are not enabled by default. So in this case,
you need to go to your PC, right-click,
select priorities. And from there go the
advanced system settings. In here, you can see
environment variables. Click once, and you
will see this list. Now go to the system variables. Make sure that path is selected. Double-click on it, and
you'll see this list. Now all what you need
to do is him visiting, click New and paste the URL for diamond UW and make sure
to add the pin folders. Now, once you have done, since I already have it here, I will leave this one.
I already have it here. As you can see, when you
w and w 64 slash pen, you make sure that
you are referring to the pin folder or the
bin folder, sorry. Now click Okay, and Okay. Now the last step is distinct. So go back to that
Visual Studio software, which is this software. And let's start by making a new folder to
store our C codes. In order to do this, I will go and add a new
folder to the C directory. Let's call it C codes. New Folder. See codes. Double-click. And here it is. This is all for. Now. Once you have this
folder and go back to this menu and click Open Folder. Go back here and
select the folder. Now once you select the folder, it will ask you if you trust the authors are
not select trust. And yes, I trust that authors. Now we are inside of the
folder called C codes, and we can start writing
our she calls inside. So click here, new file. Let's see. Now inside here we will write the same
code that we wrote before. Hash include std. Now int main, void. Now once you are done, simply go here and
write print f. Hello testing. Now when you finish. Getting your first C code, simply right-click
on this C code and you'll see a list of items. First, you need to make sure that you have already
saved your file. Now, as you can see, do you want to install the
recommended extension for C? Click Install. It's written on the right side of this window. Now here we have the C, C plus plus extension. As you can see, it's
installing a lot of extensions related
to C programming, CC plus, plus extension back. But even without the
extensions that are now uninstalling since we already
installed this extension, you can go to your C file, click run and hit
the Start debugging. Now it will ask you
if you want to debug using GDB or the
Windows select GDP, you want to combine
using GCC compiler or another compiler will compile
using the GCC compiler. Now as you can see, it's compiling the code. As you can see,
exited with code 0. And we have 0 errors. So the code did run correctly. So if we want to run the
code without debugging, simply go back and hit
run without debugging, and it will simply run the code without debugging the errors. Now if you want
to make sure that the program did run correctly, you will see added EXE
file beside the main file. And this is the executable
file that you can run. And we'll show you
a command window. Now, right-click on it, orbit real and File
Explorer, and double-click. It will show you
a command window, but it will quickly disappear. Now if you want it to stay, you need to add a
scan f sentence. Now, save the code
and run the code. Now once you have done
or the running the code, you can go again and
open up the text, color taste that EXE file. And you will see the hello testing world wishes this work. That's it. This is how to enable debugging
on Visual Studio Code. We won't use this software. We will use the Notepad Plus, Plus, but this is
something for you to know. Now some people might want to see the output and this window, like when they are
running that C code, they need to see the output
inside this output window, not inside that EXE file
that we just launched. And to do this, we first need to
do the following. You need to go to the tasks, the JSON file, which is
basically just as fine. And here you will see
the label line GCC, that EXE build active file. Now, this is the written
thing inside your label. You need to go back
to the JSON file, which is this file. And you just need to add
the external console here, change it to true. Save, and now hit done. As you can see, now you can see the output results for
your pod hello testing. Now it did run the output directly
after running our code. And this is one of the easiest
way to implement this. If you have any other
questions I'm here to help. Thanks for watching this lesson. See you next.
5. Write Your First C Code and Compile it: What is C programming? In this new lesson, we'll talk about a C program, the basic structure
of a C program. And what is a C
program has made to form C is structured
programming. It means that the C program
is composed of small parts. We call function. And you will hear the word
function a lot while you are programming in C. The first
function to be executed, which is the entry
point of the program, is called main, which
is the main function. Some functions may be a
standalone file for organizing. These are called Library. Now the basic structure of a C program is basically
the main function. Now this main function can be or can include one
or two functions. As you can see, function
one and function two. Now, the C program that has
more than one function. These functions can be composed and stored
inside a library. And we will create our own libraries at
the end of this course. What I need you to understand at this point is that
your C program, we have a function call,
the main function. This is the first function to
be executed in our program. Now, this might be
vague for some people, but we will see it in
action in the next lesson. Now, library is basically a collection of functions
that do specific thing. Like you can have a library that print things
on your display. You can have another
library that has functions that help you
read data from the user. This can go on and on. So a function is a piece of code that
does a specific thing. The main function,
the first function to be executed in our
program by the compiler. The library is a collection
of function that does a specific thing and that you can easily use inside your code. Now, let's take a quick
look at our symbol C code. As you can see, this is the first score that you
will see in our course. The first thing up here is
called a multi-line comment. Now this multi-line comment
means that this is not code. This is something you
usually write for yourself. Like to remember what is
this course is all about. Now, to write a
multi-line comment, you need to make sure that you have that backslash
and allosteric. Now in this area,
as you can see, we have a backslash and Asterix, and you must end it with
an asterisk backslash. This is the beginning,
the beginning, and the end of a
multi-line comment. Now, another thing
written between these two signs is
called a comments. So this is not a code and
won't be executed as a code. This is only for you to see. Now, the second thing here
is that include command. Now, when you want to include
any file, any library, or anything inside
your C program, you must use hash include. This is called hashtag include. Now usually it is followed with these signs and the name of the thing that we
want to include. Here we are including
a file called stdio.h and we don't use we don't want you to
understand at this point, what's the meaning of this file? All what you need to
do is that when you want to include a
library or a file, you must use the hash
include hashtag, include the name of the file
between these two signs. We also have something called
a single-line comment. Now, we already mentioned the multiline command
up here, this area. But what you want to make
only one line comment, not multiple,
multi-line comment. You can easily add two
back slashes here. And these two slashes will make this line a
one-line comment. So anything that you like
and this line will be considered as a comment
and it won't be executed. Now, we already mentioned that your program must
have a main function. The main function,
basically the function that will be executed
first and our program. As you can see, this
is the function name. Now, void and void are two
words that have meaning, but we will not explain
them at this point. You just need to know
that you must add two parentheses at the
word void between them. And leave a space here and add the word void before the
name of the function. Now, this is a curly brace. This is the opening and the
closing of the curly brace. Here, we added another
multi-line comments. Call the main function
and parse string to it. Now, this is the main function. And inside here we are writing
our very first C code. This is a print f,
and from its name, it's basically for
printing text onscreen. And as you can see here, we are printing this text Hello World. Now you must add two
parentheses and two, as you can see here.
Quotation marks. Now, these two quotation marks, we'll include that text that
we want to print in here. Again, this is how to write
your very first C code. Now, anything that you
write inside that print f, two quotation marks will
be printed on screen. And to make things more clear, Let's write this code and try to execute it as our
very first C code. Now we already mentioned
that we will be using a website called online gdb
for some of our exercises. Since this is the first
exercise, we will use it, but you are free to use the
Notepad Plus Plus as well. Now, we already
mentioned that in order to write multi-line comment, you need to add these two signs and close them with
these two signs. Now let's slide. This is our first C
program. This thing. And now these two lines
are basically a comment. It's a multi-line comment. Now, let's add a
single line format. This is a single line comment. Great. Now let's start by
including the library. So we will include that std IO, or standard input output library to allow us to print on screen. So right, include the larger, less than sign and
the library name. That's it. Now, this means that we are asking the
compiler to include the functions inside this
file inside our C program. Now, let's apply
our main function. Usually the best way to write things is by writing the syntax, then writing the
code inside of it. I like to ride the
syntax in one line. As you can see, void leave
a space and the right main, which is the name
of the function. Then add two parentheses. This one, this one, add the word void between them, and add two curly braces. Once you have this, simply, put your mouse here. Press Enter what your mouse between the two curly
braces and press Enter. This means that we
are good to go. Now, let's print something. What we need to print here
is this is our first C code. Now, you need to choose the language from the
top right corner. Select C and hit the Run button. As you can see, this
is our console window. This is our first C code
and it's working properly. Now if you did reach this point and you managed to write this, and you saw this output means that everything
went well for you. Now, you can copy
the whole code. Go to the Notepad Plus, Plus. Here. Create a new file
and paste your code. Make sure to choose the C language as your
default language. And save yours. Leave it has new. Now, go to the file. If you've already followed
our installation video, the download and install
software section, you should be able to
use not by the blast, blast for compiling your code. So click cmd. And once
you see this window, you need to write GCC. After that, you need to write the name of the file
New three. Let's see. Now, this means
that we are asking the compiler to
compile our C code, which is the first step. Let's say playing or making sure that our code
doesn't have any orders. Now if you combine the code
and you didn't see anything, it means that
everything went well. Now your documents folder, we'll have a new file
called a ducky XP, which is basically the
file that has the output of this C cold press Enter, and you will see
the very same code. This is our first see cold. Now let's review
the steps of GCC, which is the compiler,
that filename. Then a dot EXE. And you should see the output. If you saw the output means that everything went well for you. Now if you compare this output with the output from before, you can see that
they are identical. The same sentence here is rated and our CMD or
command line window. That's it. This is the end of
our first line.
6. How to search for solutions as a Programmer: Hello and welcome this new
lesson in which are known to explain how to search for a specific thing and C.
Now let's say you were writing a C code or you run into one of our quizzes
or assignments, and you didn't know what to do, or you didn't
recognize a function, line of code or anything. This happens as we add
assignments and quizzes, we try to diversity. So you might find a
quiz question that is not explained in the
previous lesson. And we need you to
search for a solution. This is to make sure
that you can search for things that we didn't say
xy plane and our lessons. And to make sure that you
have this, let's say talent. We call it a programmer
search talent. Now, go to Google. The first thing
that we need to do is to make sure that you are writing your search term or your search term, right way. Let's say that we want to
search for a specific function. It's called get Nc. Now you need to make sure
that you are writing C without PSI-BLAST are
solid C programming. You must write C
programming to make sure that you don't
get results for C sharp or C plus
plus because they are C but with
different approach. So as you can see, see Get function, this
is the first result. And this is the explanation that gets function
enables the user to enter some character
followed by the enter key. All the characters
entered by the user gets stored in a
character array. Then I'll characters added to the array to make it
as trained by default. Now, gets allow the user
to enter the space for it. It's thinks this is
the whole explanation. This has everything you need. Now how to use it? You can go to the first result. Now, as you can see here is that declaration gets and
this is how to use it. You must first make an array, then use the gets function. These things would be explained during the lessons are
raised and these things, but this is how to get adult. Now, other places to look for
the results are tutorials, point Tutorials,
gateway, gigs for mix. These are very great
websites and you can go and write Stack Overflow. This will make
sure that you will get results from Stack Overflow, which is like
programmers, google. Programmers go to
StackOverflow to write down their problems so that other
programmers can help them. As you can see, you can
check more than one result. And let's take the first result. Here you can see that
there are a lot of sea problems posted
by a lot of people. You can get. And see. Now, it's asking for
whomever vacation. As you can see, these
are some of the results. So whenever you have a question, you can ask it and a
lot of Q and the board will be sure to help
you that question. If you found something I loved the quizzes or the
multiple choice questions that we didn't yet exit plane
or you didn't recognize. You can ask in the Q&A
board so that we can explain it for you or give you proper
information about it. Again, will sometimes
add few questions to these assignments
and quizzes to make sure that you can look
for answers yourself. Then come back to solve
the quiz and assignment. This is something if the
programmer must do to be able to do great
projects in the future. That's watching this lesson. This is.
7. Comments in C: Hello and welcome to
this new lesson in which we are going to
talk about comments. Comments and C are basically on an executable text used to provide documentation
for the code. It provides clarity
to the C source code, allowing others to better understand what the code
was intended to accomplish. It is always recommended to
use comments in your code. We already mentioned that we
have two types of comments, that single-line comment
and a multi-line comment. The single-line
comment is basically any line preceded by
two forward slashes. As you can see, the
forward slash is the sign that you can easily
derive using your keyboard. And this is the final
shape, two forward slashes. And this is your comment. While the multi-line
comment is any text starts with a forward slash
followed by allosteric, and ends with an Aztec
followed by a forward slash. And this is how it looks for slash asterisk and
then forward slash. This is the multi-line comment. Now, we already showed you
how to derive these commands, but let's refresh our memory. Now as you can see, this is the forward slash
followed by an asterisk. And this is an asterisk
followed by four slash. Now phew close this. You can see that these two
commands are together. These two forward slashes
followed by one line, which is the one-line comment. Now, another piece of information
that we can mention is that when you write two
forward slashes anywhere, even inside the main function, they will be treated
as performance. Now if we want to
commend this line, print f, we simply add two forward slashes,
as you can see here. Now, these two forward
slashes would mean that this sentence
won't be executed. So let's save this file. Let's open it. Using CMD. We need to use the same name, gcc 1, first C code. But c does. You can see because our filename
has all of these spaces, it won't be compiled correctly. So in order to compile a C code, then m must include no spaces. And this is a very
important note. Now, save your file again
and remove less spaces. Now, let's do this
one more time. Let's file, go to File open CMD and write gcc by the filename 1, first C. C. Now as you can see, we didn't leave space. Okay, let's leave
this space. Now. The code compiled correctly, if we try to run that EXE, nothing will be
printed because we commented the line
called print f. This is our first C called. Now if you remove that and let's save the file again
by clicking here and went back to
this command window. We can run the command again and we can
use the a dot EXE. See our output, this is our first C code, as
you can see in here. This is how you can use multi-line comments are
single-line comments, and this is how you can use
them in debugging as well. So if you suspect that this
line is causing a problem, you can simply commented
using single-line comment with two forward slashes
or double line command, using the forward
slash and asterisk, then the asterisk forward slash. That's it. Thanks.
Flushing. This is Ashraf.
8. Strings and Escape characters in C: Hello and welcome
to this new lesson, interatrial going to talk
about a very important topic. And C, strings, NC sinks and C are basically
a way for you to explain or type things. It is compromised of a set
of characters that can also contain spaces or special
characters and numbers. Now, in C, string is represented
between double quotation, like these two
double quotations, as you can see in this image. This is astray, or this is
simply anything that you write between these
two double quotations will be considered as a string. Print f function, the function that we already used twice. We'll print the string
passed to it, as it is. It's called string literal. Now, escape operators may
be used inside the string, like the backslash, as
you can see in here, with a letter following
the backslash. This operator means Here's
tab, newline or codes. As you can see, if
you use this one, it means that this sign, please add a new line. If you use this one, means add a horizontal
tab after this location. If we use this slash v, means that we need to add a vertical tab inside our swing. If we need to show the single quotation or double quotation
inside our string. Once we print it, we can use the slash single quotation or double
quotation from double-quotes. Now, things might be more
clear when we execute them. So let's write this out. Let's pull fluids. As usual, the std IO library
and write the main function. The same way we
mentioned before. Press Enter here and press Enter here and write
your code here. So print f here. Between the double quotation, we will add our string. This is our string. Now, if you want to print a
string and add a new line, this is that second line. Now without adding the slash n, this text will be
printed like this. Let's add a semicolon. As you can see, this is out string and this
is the second line. Now they were printed
on the same line. We want to add a new line. At the end of the first line, you can add a slash
n and run again. Now as you can see, this is the first text and this
is the second text, and we added a new line here. Now let's say that we want to print a single quote
or double quote. If you added a
single quote here. And try to run that code. As you can see, it can
directly be printed. Now in some compilers, this will not print
directly to cause an error. So you need to add a slash
and then run the code. As you can see, this
printed as well. Now, what if you want to
add the double quote? You can add the
double quote here. But if you try to run the code, it will show you an error. As you can see. Now this own scores because
we know that the string is something we wrote between
two double quotation. And since we added a
quotation here, it is. Considering that string that we want to print is this thing. And it's not recognizing
this part of the string because it's not included in the first
double quotation area. To make sure that this
won't cause a problem, we can simply add a slash. Now when we run the code, it will print the double
quotation or double quote. Now that single code is printed, the double quote
is also printed. If you want to add a space or a tab space between
these two works, we can simply add
slash t. Then run. As you can see, slashed, he added a horizontal space
between these two words. Now this space can
be either four or eight depending
on the compiler. Now, we also mentioned
the vertical spacing. If you want to add the
vertical spacing, spacing. Here we add level
horizontal spacing. Simply add slash v. And we'll add a vertical space between this part of
the text and this part. Now, click Run, and you'll see that this is printed
on the first line, and second line is printed here. So we added one vertical space
between these two lines. These things that we
wrote here, here, here, and here, and here are
called escape characters. Now, skip characters are
written using the backslash, not a forward slash. As you can see the commend, the singular comment is written
using the forward slash, but the escape character are
written using the backslash. So it can be slash
t slash v slash single quotes or
double quotes slash n. These are the one that we
introduced in this lesson. Slash t will add a
horizontal spacing. Slash v will add vertical
spacing as we saw here. Slash single quote will add the single quote
or on printed on the text slash double code will print a double
quote on our text. Slash n will add a new
line, as we saw here. We added slash N here, and it inserts the new line. So these texts are not like
printed on the same line. These, this is how you
deal with this thing. Now, to print saying we already mentioned that
you can use the print f function and this is a good piece of information
that we need to mention here. Print f function takes two, as you can see, parentheses. And inside the two
parentheses you must bring to double quotation
or double quotation, sorry, and end it
with a semicolon. This is the syntax for
print F. Now print f function is a function
defined inside this file. It's called standard
input output, the texts, or the header file. And we'll talk
about it, a file in more details in the
coming sections. But what you need to
understand is that without including
this header file, our main function
won't recognize the print f function that
is used to plain text. Print here means printing. F here means format,
means format. Now, this is the string literal. So print format,
we'll print the text or the string as it
is, as we saw here. And output window. That's it. Thanks for watching this lesson. If you have any questions, please ask Kentuckian the board. This is Ashraf.
9. Variables in C: Hello and welcome this annulus one in which are
going to talk about variables in C. Variables. And C is basically a place
that holds a piece of data. So we all know that memory inside our computers or
in embedded systems, microcontrollers
will hold variables. Now, we won't go into the tails how memory
process variables. But all what you
need to do is that a variable is something
that holds a piece of data. It's typed values in data and size that
needs to be saved. In most, let's say, Use Cases and embedded systems. We have limited data types. These primitive data
types are plotting types, as you can see in here. These ones types.
The floating types is the float and double. While the decimal types are the character and the interior. Now, the floating types will hold information that
has a float like 3.21.4. And these things, normally
the flaw size is four bytes. And this is a very good piece of information if you want to
keep track of your memory, especially in embedded systems. While double all the same type of information
that afloat holes. But the size is
doubled eight bytes. That's why they are
called floating types. This human tribes, other hand, are the character and integer. Integer is the most
common used data type. In C, It's about
one byte in size, while the character is
about eight bytes in size. Character is usually used
to store characters like a, b, and c, while integer is
usually used to store numbers. Float is used to store numbers with a floating point like 3.2, envoy to these things. And these are the most
commonly used data types in C. We call them
primitive data types. You can split them into floating
types and decimal types. Floating types are
float and double. One. Decimal types are
characters and integer. We use them to store information like username or password. All the calculation results. You need to take along
that datatypes may vary from each
compiler to another. So in one compiler, an integer can take one bite. In another compiler, an
integer can take four bytes. Now, the next thing
that we need to mention is the syntax. How do we define variables in C? To define a variable, you need to follow
a specific syntax. First, you need to
write the datatype, which is the first piece of
information in the line. The data type is character, integer, float, or double, depending on, depending on what type of data you
want to store inside it. Then you must leave a space. After the datatype. After that, you can write the name
of your variable. Here, the variable
name is X, Y, and Z. Now the next step is defining or choosing whether you want to add an
initial value or not. Now if you want to add an
initial value to a variable, you can simply add the equals
sign and add the value. You must end the line
with a semicolon. Like every line in C must
end with a semicolon. Now in the first line
we define char and initialize it with
the value nine. While in the second line, we initialize an
integer named it y. And the initialization
wasn't done. In this case, it will
have a random value. We call it a garbage value. This is not a use case that
we would love to happen. So usually we initialize
all of our variables. Here. We are defining
a float and initialize it with a floating
number like 6.52. Again, we prefer
to initialize all of our variables to an
initial value of 0. Let's say if you don't have
a specific value in mind, but don't leave
it uninitialized. Otherwise you will end
up with a garbage value. Now, let's see what
will happen inside our memory when we
initialize a variable. Now let's say that
this is our memory. Now inside this memory, we have different areas, places. And we initialize a variable called x with the
initial value of five. Now what the compiler will do is we'll go to the RAM or memory. It will name this location x, and we'll give you an
initial value of five. When you initialize another
variable, we call it y. Without adding a value, it will go to this location, or this memory will say, this is y, and it won't
give it any value. Now, whatever the previous
value that was here might be as it might be assigned by default
to this variable. And we usually call
it a garbage value. Now since integers
takes only one byte, I gave it one byte. So this is one byte of memory. But let's say that
we want to define a character M. And
let's give it a, let's not give it a value. Now when we go to memory
and want to define m, It will take this
byte and this one. And this one. And this one. So depending on how many bytes
the character will take, which is eight bytes, it will take eight locations. So it will take also the dislocation,
dislocation 12345678. So it will take all of these locations because
it tastes eight bytes, while integer takes
only one byte. And this one takes
only one byte. So why is one byte
and x is one byte? So in total, these
three variables stock one byte of memory plus
one byte of memory, plus eight bytes of
memory. In total. They took ten bytes. This is a very good
piece of information, especially when you
are dealing with systems that have
low memory size. Like an embedded systems. It ten bytes for
three variables. Again, if we haven't
defined anything here, like we haven't said,
equal five or something. We just ended with a semicolon. This will be a variable
that we didn't initialize. So it will take garbage values. We call them garbage
values because these values are values
that we haven't said. And it might be a
previous value from previous variable declaration in memory from any other software. Now, let's test this out by
going to the Notepad Plus, Plus and typing some variables. Now as usual, hash
include that std, IO, the edge, and write void, main. Void. Between the
two curly braces, we'll start typing
all variables. Let's change the language to c. And x equals 0 into y equal ten. Hello, The equals 10.5 and
double c equals 4.555. These are the different
types of variables. These are initialized variables. And this is an uninitialized
variable, has garbage value. Now some compilers say or
define the garbage value as 0. Otherwise leave it like to be
a random value from memory. But the better use case or the bizarre programming
practice is to define this to 0. This equals 0. And we already
mentioned that we need to add the datatype, leave a space and
the variable name, add the equals sign
and the value, and in the line
with a semicolon. That's it for this lesson. Thanks for watching.
This is Ashraf.
10. Rules in naming variable in C : Hello and welcome
to this new lesson notes you are going to cover some rules and
naming variables. Now, the first rule is that a variable may
contain the following. May contain underscore, may
contain a number between 09. It may contain capital
letters from a to Z and small
letters from a to Z. The first character of a
variable should contain an alphabet or an
underscore symbol. So the variable name cannot
start with a number. For example, it can be
integer A4 for integer for a. Now, a is the wrong one. Let's mark this. As you can see, an A4
started with a letter, while in for a
started with animal. So a variable name cannot
start with animals, as we already
mentioned, can start with either a letter
or underscore. So this is the wrong one. Now, blanks and columns
are not allowed. And blanks is basically when you add two words and you
leave a space between them. Commas are also not allowed. And we are going to see these
in action in a few seconds. Variable names should not
be reversed or served work. Now reserved word is
basically a word that is reserved for our compiler, like print f for
example, or end. So there is no way that you
can name a variable n because n is already reserved for
our compiler as an integer. So blanks, commas, and
special symbols are not allowed with the underscore
as the only exception. So any special symbol is not allowed except for
the underscore. Example, the add sign that asterix that dole
out of the blast and many more signs
are not allowed. A variable name cannot be
repeated in the same scope. So if you have
named a variable x, you cannot create a new
variable and limit x as well. The variable name must
not be reserved word, as we mentioned earlier. Now, let's see these
things in action. Now as you can see here we
are inside the notepad. You cannot name a variable or with other
served or like int. N is basically the
integer data type and you cannot name it float. All of these words are
reserved for the compiler. You also cannot name a variable
starting with a number. This will give you
a compile error. You can name it A9, starting with an alphabet, and it will work just fine. You mentioned that you can add special characters like this and this and this to
the variable name. However, if you want to
add a special character, you are allowed to add
the underscore sign. This is the only thing that is allowed in the variable naming. You can even add underscore
sign at the beginning. So our variable
name can start with underscore or with a letter, but it cannot start
with a number. You cannot use the
reserved words, as we already mentioned,
like integers. And you cannot name a variable or use the
same variable name twice, like int x, x, it will give you a
compilation error because here you are defining a
new variable, naming it x. And you are telling the compiler
to go to the memory and assign a location with
the name x as an integer. Then you are, you are telling the compiler to go
again and assign that location with an x
and the type of integer. And there's already
a variable named x. So this will give you
a compilation error. You cannot use the same
name more than once. Now the last thing that we need to mention is that you cannot leave a space or a blank
in the variable name. Let's name it new variable. Now this will give you a
compilation error because n, nu is the variable blank and the other word or
something else. So the compiler is
not recognizing them. If you want to leave a space, you can either use
the underscore or you can use a naming convention
called camelCase, where you name the
first letter of every word with a
capital letter. So new variable, name like this, or simply use the underscore. Underscore. And here is another underscore. Now, this is another solution. You cannot use commas
inside the variable name. As already mentioned. You cannot use blanks and you
cannot use reserved words. You can use special
characters set for that underscore sign. And you cannot use the
very same name for different variables of the same variable
in the same scope. Now when we say scope, we mean these two curly braces. This is the scope
of these variables. Now if I tried to call x
outside the scope like here, it won't recognize it, but this is something that
we will talk about later. So let's summarize. A variable name may contain
underscore numbers from 0 to nine alphabets and
capital and small letters. The first character
of a variable must contain either on alphabet
or an underscore sign. It cannot start with a number. Blanks and commas
are not allowed. Variable names should
not be reserved word. And plus the fact that special characters
are not allowed except for the underscore. Thanks for watching this lesson. This is optional.
11. Printing Variables with Printf in C: Hello and welcome to
this new lesson n, which are going to talk
about print function. Now, look at this code. Let's discuss what's
happening inside this code. First, we are
calling the library. Then we are defining our main
function as we usually do. Then we are defining an integer valuable
variable named x and y are giving it a value. Often. This is a semicolon to indicate that this is
the end of this line. We are calling the
print function. And inside it we have
written the variable value is We ended the line
with a semicolon. What is the expected
output of this program? Now, pause the video and try to guess the
expected output. I'll give you five seconds. Hopefully, you will
pause the video and try to figure out the output without
running the code. Now if your answer was this, the output on the
display will be the variable value is X. Then you are right. This is a string literal, so it will print the
string as it is. It won't print the
value of them, won't replace x with them. Here. This is what this
lesson is about. Now, the print function flashes the variable
inside this thing. The print function can print a variable inside the string. It could be done by
inserting something we call format specifier. The format specifier
is basically something you add inside the string that indicates that you want to
print something. The format specifier will
be replaced by the value specified in subsequent
additional arguments. Inside the format specifier
to be replaced by the specified value after
the additional arguments. If things are not yet clear. Let's first talk about the
format specifier syntax. Most common specifier
used for print function, our percentage f,
person C, and person D. Percentage f inside the string
indicates that you want to replace this with
a floating value. While format specifier
or percentage c, means that you want to replace this with a character value. The last one is the most common one of these
three is the percentage d. It indicates that you want to replace this format specifier. The string with a decimal value, usually follows this pattern. The variable value is. Then you add the
format specifier between the double quotation. Inside that x is. Now the new thing
is that you must add a comma and
the variable name. What this print function
or this line will do. It will replace the
format specifier, which is percentage d, with the variable value of x. So it will print the
variable value is ten. And this is called
a format specifier. You need to memorize this world, as we will mention it a lot. Now, let's see this in action. Now, let's write, as usual, includes STD or the head of void main void as usual. And try to write the code from
scratch every time so that you are getting used
to writing C calls. Now, let's define a variable. A variable, give
it a value of ten. So we define an integer
value, an integer variable. The name of the
variable will be x, the value or the initial
value will be ten. Now let's print f. The variable. The x variable value is here. We need to add the
format specifier. I added percentage d, because integer is
a decimal value. If it is a floating point
value, we need to add f. If it is a character value, we need to add C, D here. Then add the new thing, which is the comma and
the variable name x. Now this line will print the x variable value is
replaced this with ten, which is the value
of this variable. Let's save our work. Let's call it three variable. And let's try to run it. Here we have the compiler GCC, three random variable C and E. As you can see, the
x-variable value is, the x variable value is. And that percentage d, which is the format specifier, was replaced with, then. That's it. This is how easy it is to print a variable within the
print f function. Now let's talk about
common mistakes. Most people write
x here instead of writing the common value, which is all the
format specifier. And when they combine, this is what they see. The x variable value is x. So we need to
replace this with d. Now the format specifier or
be replaced with the X value. Another thing that you
can do is to blend more than one variable on
the same print F function. If we want to print y, you can add that value. Let's print it on a new line. So add percentage n, which is the escape character
that we already introduced. The value of y is add
another format specifier, percentage d, and add
another comma, then y. Now what will happen
here is assembled. It will replace the first
format specifier with x and the second format
specifier with Y. Now you have another variable. You can add another format
specifier and it will be replaced with the field
variable, and so on. This string will be
printed as it is. Now. Only thing that will be changed
is the format specifier. The first one will
be replaced with x, the second one will
be replaced with y. Let's save and compile
the code one more time. Now I want to clear that CMD or the command window
simply lights CLS, or clear screen and
will clear everything. Now let's compile it. Gcc and C file name. Then run. As you can see, the x-variable value ten, which is this line. And then slash n means
that go to a new line. The value of y is, instead of percentage d, we will see the value of y, which is 15, as
you can see, 1015. Now, what I need from you to make sure that
you did understand everything that we've learned in this lesson is dried
this very same code. And make sure that you are looking to the same
results on your screen. Make sure that you don't add the closing of
double quotation here. Because in this case, it means that you
are printing this. Now let's see what will
happen if we did that. Let's clear our screen. Run the file. As you can see, the
x-variable value is a very huge number, and the value of Y is
a very huge number. Now, this is the default
value for decimal variables. And this compiler,
as you can see, now, it did print x and
y as a normal text. It didn't replace
any of these things because we misplaced
the double quotation. So you need to
remove it from here, add it to the end of your text. Make sure that you have the commas right place
and the variable names. That's it for this lesson.
Thanks for watching. This is optional.
12. Reading Input from User in C: Hello and welcome
to this new lesson in which we are going to talk about scan f or scanning
a value from the user. Now what we covered
so far is how to print a value on
the console window. But what if we want to read
the value from the user? This is where scan f is use. The scalar function
is used to get value from a user and save
it to a variable. It is a part of the
std IO library, the library that we include
at the beginning of our code. Now, the syntax for this
function is simple. You just tried scan f. Then you open two parentheses. Now, first part is
the format specifier. Here you must add a format specifier between
two double quotation. Let's say that we want to
read an integer value. So we'll add percentage d between these two
double quotation. Now, the second part
is adding a comma. Then the add sign. After the add sign, we need to add the
variable name. So in this case,
let's say x, right? And x. Now, this is an example. As you can see. Here, we defined a variable, we call it x, integer x. And then we use scan f
with these two parts. The first part is the
format specifier, then a comma, then the add sign, then the variable name. You need to make sure that you follow the very same structure. Like in this line. Scan f, percentage d, which is the former swift file between two double quotation, Acoma, then the and sign
and the variable name. Then close the parentheses
and add a semicolon. Scanf function,
again is a part of the studio or standard
input output function. So there is no need to add any
or include any other file. Now let's see this
function in action. Now let's do the regular thing. Let's include the function that had the libraries
or print f and scan. Then let's add a main function. Now press Enter here, press Enter here, change
the language to see. Now, let's define a new variable and give it an
initial value of 0. Now print something for
the user to inform him that we want him
to enter a value. Enter your age. And let's change
this with h. Now, this will print this sentence
on the console window. Informing values are that we
want him to enter his age. After that we will use
the scan f function. Now, the first part is the double quotation that
coma and the, and sign. And the second part. Now between these two
double quotation, depending on the variable type, you must add a format specifier. Here it would be percentage d. Now, if this is a character
which will be percentage c, If it is afloat, to
be percentage f, as we already mentioned. So we'll add
percentage d. And here we will add the add
function or the and sign, sorry, the variable name, age. Now this will take
anything that they use are right up to the point
that he enters. Enter. Which means that he
finished entering the text. And we'll store it inside
a variable called age. Now, this line will read
the input from the user. So let's add another
line to plan the age. Let's write print def. You age. And as we already mentioned the previous lesson
to print a variable, we must add the format specifier inside the two double quotation. Then add a comma after the W creation and the
variable as it is. Now, to make sure that
we see all of these, each one line or a new line, we need to add a slash, slash n. And here are the slash n
at the end of each line. Now, once you are
done, click Save, and let's call it scan variable. Now, let's go to file as
usual, open then CMD. Now in here, right, gcc, c. We are done. Let's
zoom in to see. Now write a good EXE, which is the executable for
our C file after compiling. So as you can see, integer h, Let's compare
to what we have here. This is the first print f
function, enter your age. Now after this line, as you can see, this
is a blinking cursor. For the scanner.
I would write 32. Then I'll press Enter. Once I press enter, it will
execute the third line. To write your age is
the age value here. So please enter. Your age is 32. That's it. This is how easy it is to
scan a value from the user, store it in a variable and
print it and a new line using print f. Now if you have any questions
about this, please ask. In the Q&A board, I'll be more than happy to help. But as you can see, it's a
very simple thing to do. Thanks for watching this
lesson. This is Ashraf.
13. Arithmetic Operators in C: Hello and welcome this
and you will have some charcoal talk
about operators. Operators are very important
thing when you are programming because you'll need them when you are
doing calculations, when you are checking if a
condition is true or not, or when you are inside the loop and you want to
increase a value. We have different types of
operators and in this section, we will explain them one
by one with an example. And we will try to make sure
that you understand each and every one of them with I
use case and a scenario. The first one is that
it's not a cooperation. Now, cooperators, there
are two types of them, uni and Pi, and we'll
explain them in a minute. There is also the
width wise operators which are used to play
with pitch level. The assignment operators, relational operators,
logical operators, and other operators
like the size of operator, the ternary operator, the address operator,
the difference operator, and that subscript operator. These are only the names. So let's get started with
the arithmetic operators. Now this is a table that has all of that. It's
not a cooperators. Most of them are
straightforward. First thing that we need
to explain is the world. By. Now, there is the
union and the by. The by is basically operators
that takes two operands. So they must have two
operands to be called by. An example of this
is that summation, which is basically this
side, and a subtraction, which is basically this
sign multiplication, which is the asterisk
sign division, which is the slash sign modulus, which is the percentage sign. Now let's talk
about each of them. The summation is basically
used to some two operands. This is the first operand, and this is the second overland. Now here we have x equals
ten and y equals five. So x plus y is basically a summation operation because we are using that
cooperator, the blast. So the result will be
stored of the sun, will be stored inside
this variable. So x plus y, then plus 15. So ten plus five, sorry, it will be 15. And this is the result. So some variable will include
the value of the summation, which is x plus y. Now regarding the subtraction, as you can see, the
same example as above, two operands, x minus y, which is ten minus five. The final result is five. So five will be stored
inside the sub. As you can see here,
sub d equals ten. Now in this case,
subs wrong because ten minus five is only five. Now, let's move on to
the multiplication. X multiplied by
y. X here is ten. Why here is five? Now when you multiply ten
by five, it will be 50. So here we have this answer, 50. So the van, the variable called
multiply two will be 50, which is an integer. Now, we also have
the division X, which is thin by five, which is basically an integer, ten divided by five equals two. So the result will be stored inside the division variable, which is basically two. Now we have the mod psi naught
sine is basically assign, that gives you the
remainder of a division. So if you are dividing x mod y, now, I need to
concentrate on this one. When we say x mod y first, you need to make sure that
you have x divided by y. So if x is ten divided by y, which is 510 divided
by five equals two, there is no reminder
in this division. So the result or the final
result for the percentage, which is the
reminder, will be 0. Now, if y is divided by x, now, this is a reminder sign. Again. You need to do it
first as a deficient, then you take the remainder of that division as the
result of the MOD. So y divided by x. Y here is five divided by x, which is ten. Now, when we divide five by ten, this is called a division. So five by ten here, phi one f2. So the final result will
be one divided by two, which is basically 0 or five. So in this case, the final result will be 0 and the remainder
of this will be five. So that division value will be 0 and the remainder
value will be five. That's why you see the
results here as it is here. As you can see, odd equals five. Now to make things more clear, Let's try this example. Ten mod 310 divided by
three equals three. Now if you multiply three by three, which would equal ten. So ten minus nine
will equal nine. Sodium ten minus
nine equals one. And this will be the
result of the reminders. Let's try this again with this. Now, line. Divided
by one equals nine. No fractions, no reminders. So the mode will equal 0, just like in this example
at the beginning here. Now, let's try this one. This is the mode division. So 17 divided by 917 by nine. It will equal one. So here, right, one. Now one multiplied by line
equals line 17 minus nine. The reminder will be eight. This is the reminder and
this is the division. And as you can see
here, equals eight. Now what you need to understand
is that division gives you a value without a fraction, a fraction to be printed when you are
using the division sign, it will just print that value and it will
leave any fraction. So in this case, if you need the fractions
or if you need a reminder, you must use the reminder Sine. Let's try this in action and
let's print the values to make sure that everything
we said here is correct. Now let's start by
including the std, i, o the texts library. Void, main void as usual. Now let's change the
language to see. And I need you to do this from
scratch every single time. Now let's solve the value
five arithmetic operators. Now, let's define two values, and x equals and y equals five. Now let's do some operations
and sum equals x plus y. This blend some equal to b. Let's add a slash n. Here. We can add some. To
see the results. Now I'll do all of
the operations. So let's do that. Division as well. The multiplication that holds. All of these are pi operators because they take two
operands, as you can see here. This is the second orbital and this is
the first overland. Now to print the results. Print f equals to d slash n. And here, you can
copy this line and paste it here with this slide and paste it here
with some modification. Mode, also, just
to save some time. Now here it is. This will
sum these two operands. So we'll divide them. This will multiply them and
this will give us the modes. Now, let's hide
the results here. Now first the summation
results would be 15. Divisions, results will be two multiplications
and will be 50. And the result will be 0, because we already know
that x divided by y, ten divided by five equals two. And we don't have any fractions. As we already mentioned, the mode will be or the
reminder will be 0. Let's save the work
and the code again. Now, let's go GCC. Let's add the filename five. Now here's the filename, as you can see, GCC. To compile it now we
have the executable, so like a, E, E. And here we have the result, this 152 for the division, 50 for the multiplication 0 for. Now, to make things look
more like, interesting, let's say, let's move this. Now, let's play with
the print f function. Let's change this
with her son through D plus person to D
equals. That is up. Now what I need to do here
is to add the x value, the y value, and the results. So to replace this with x first, we need to add x here, as we already mentioned in
that print f. Last one. To replace this with why
we need to replace y, x. And to replace this
with the summation, we need to place the summation. So this will print x plus
y equals the result. X will be replaced
with the value of x. Y will be replaced
with the value of y. This will be replaced
with the sum value. The first variable, second
variable, a third variable. And this was explained in the printing or printing
variables lesson. Let's do the same
for all of these. Now we just need to
change the sign here. This will be the division. This would be the asterisk sign, and this will be the
demand of sine or the percentage sign
on to slash n sets. Now, let's go back to compile
the code one more time. Clear the screen using CLS
and from compiling command, and then run the file. Okay, now as you can see here, we have the first one
in the right order, the second, third,
1, the fourth one. There are problem with these
because as you can see here, we didn't add the x
and y before the div. As we already mentioned, each of these format specifiers will be replaced with
associated variable. So save it one more time. Here it is. Ten plus five equals
1510 divided by five equals to ten multiplied
by five equals 50. Then the last one. Okay? We have a missing D here. Now, this is a very nice
piece of information. As you can see here, we have percent d and we
have four centers. And we have their centers me. Yet, it wasn't
printed correctly. That's because we
have a percentage here inside our string. Now this percentage will
be treated differently because we usually use it
to add a format specifier. So if you want to
print person to sign and our final print f, When do we need to add
double percentage like that? Now once you add them like that, the print f function
will understand that you want to show the percentage
sign ones on the screen. And this is a very good tip. Now going back, let's
combine the codes. Entity x e. Now, as you can see, it was printed correctly. Otherwise I won't be
printed correctly. So you need to take an oath that you must add
double percentage, sign to print it on
your console window. So ten minus five equals 0. And these are the main
arithmetic operators. That's it for this lesson. If you have any questions regarding anything,
I'm here to help.
14. Uni Arithmetic Operators in C: Hello and welcome to this
new lesson in offshore. Don't talk about
the uni operators. Now in the previous lesson, we covered that by operators. And here we are talking
about arithmetic operators. Now, the evaporators
takes only one operand, so there is no need for
two operands like x and y. One variable will do the job. Now in our case, we have two types of opera. Now. We have the
increment operator and the decrement operator. And we have them separated because using the increment and decrement with one variable without the equal
sign is something. And using them with an equal
sign is another thing. So the increment
means to blast signs. The decrement means
two minus signs. Now this means add
one to the variable. This means subtract
one from the variable. Now, in this case, we call this a postfix and
we call this a prefix. This is pre, because
we are adding the two plus signs
before the variable. This is a postfix because we are adding the two plus sign
after the variable. Now, if we executed both of
these sentences taking into consideration that the
variable x equals ten, this, and this will add
one to the variable. So x will equal 11. And then here, x will also equal 11 without its
post-fix or prefix. Now, when it comes
to the decrement, it's basically the same thing. We have a postfix and prefix. This is the postfix and
this is the prefix. When we are calling it, knowing that the x
value equals ten, it will result in this
x will equal nine because the decrement will subtract one from the variable. And in this case, it will also equal nine
without is postfix or prefix. Now up to this point, there are no problems. Everything is clear,
everything is easy. This will increase the
variable value by one, and this will decrease the
variable value by one. Now, the problem comes when
we have an equal sign. This means that we
need to execute this store the result
inside the variable. And in this case, the postfix and prefix that we mentioned up here
has a different meaning. So let's start with
the first one. This one is our postfix. This is basically a sentence
in which we say that the compiler or luxury tell the compiler to assign X to Y. So it will first
store x inside y, then it will increment x. And order of operations
is very important here. The postfix is basically doing the assigning
using the equal sign. X will be assigned to y. Then it do or does the
increment that we did here. So the final result will
be x will equal 11, it will be increased by one. But the value of
y will equal ten. Because when we
first assign x to y, x value was ten, as
you can see here. So ten will be stored inside Y, and this is the first part. The second part is
basically incrementing x, so x plus blast. So x will become 11. Now, this is in the postfix. The prefix, however, will
do something different. It will first increment x. As you can see, this
is the first step. The second step, it will store that result from incrementing
x and assign it to y. So incrementing x
will result in 11. So x will equal 11. Then these 11, or the result as 11 will
be stored inside y. So y will also equal 11. And you can see the difference between these results
and these results. So what I need you
to understand is in case you saw postfix, this means that
you need to assign the variables first,
then increment. But if you saw a prefix, it means that you first
need to increment, then you need to
assign the value. Same thing goes here. Here we have a postfix. So it will assign
the value of x to y. So y will equal ten. Then it will decrement x by one. So x will equal nine. Same thing here. It
will first decrement. So x will equal nine. Then it will solve
that result inside y. So y would equal nine. You can see the difference
between these four lines and these four lines
this forward, very easy. It's simply incrementing or decrementing without its
spot suffix or prefix. While in this case
it's different. Now if it is postfix to
last sign, then increment. If it is prefixed, it will increment then assign. You need to keep that in
mind and take a note about. Now as usual to make
things more clear, let's take a practical example. Now here you need to actually
include that library. Then align the main function. Change the language to see. You all good to
go. Let's call it. Let's call it unit. Now in here. So let's define x as
give it a value of ten. And let's define y without
giving it a value. Now, x plus, plus, plus, plus x, x minus
minus minus minus x. These are the first four
that we will test out. Now what I need to
mention here is to make sure that we will get
the right results. We need to assign x as ten after each operation to make sure that we are changing the
value of x when it is ten. Because otherwise it will
keep changing the value of X with the new value. So here, x will be 11, and this will be 12, this will be 11,
this will be ten. We need to do these
operations on the original value
of X as an example. Now, let's print I will
write the same thing, x plus last value
equal percentage d. And let's add x. Now let's add a slash n here. As a new line. Let's copy this. Paste it here, here, and here. Now we will change
this x minus minus, this will be minus minus x. This will be plus subplots of x. Now, let's talk about what will happen when
we run this code. First. In this case, it will increase
x by one, x plus, plus. So it will print here 11. Now x will go back to ten and will also
increase it by one. So it will print 111 more time. X will go back to ten. It will decrease it by one. So x here will equal nine. It will go back to ten. It will increase it by one, then x will be nine as always. So 1111, line nine. Now let's try running this code. Gcc six arithmetic. You need to see, as you
can see, x2 plus plus, plus plus x have
the same result, 1111 minus minus, minus minus x, the same result which is line. Now, let's add a multi-line
comment to this area. And do the other operations. We mentioned that
we have y equals plus, plus, plus, plus. First. We need to print it. Now, we need to plan
the values of x and y. So x value equals this. And after the slash n, y value equals this. So x and y. Let's add
slash and here as well. And see where things will go. And another slash and slash. Now, this will print this, then it will go to a new line. We'll bring this to a new
line with the value of x. And it will go. My new line to print this
with a value of phi. That's it. Now we need to make
sure that x value equals ten each time
we run this command. Now, let's copy this base
step as before, four times. Now this will be plus, plus x, which is the prefix. This is the postfix. This will be x minus minus, and this will be minus minus x. Let's call the base
is three years. Copy this here, and
copy this listed here. These are just the same
unit operators that we explained earlier and we
are just listing them out. Now if you want to split them, you can add this slash n to make sure that they are separated
when they are printed. Now this will make the value of x equals
ten each time before running the command
to make sure that we are starting with the
same initial value. Going back here. Let's combine the code being the screen, and run the code. As you can see, y
equals x plus plus. Now, we don't know that
the value of x is ten. And the first thing that this
dead is to assign y to x. So y value will equal to,
as you can see in here. The second step is
incrementing x value will equal 11. Let's go here. This is a prefix, so it will first increment x. So x value will be 11. Then it will add the 11 to y. So y value will be 11. Just as we explained. Now, when it comes to
these two x minus minus, this is a postfix. First, assign the
value of x to y. So y will equal ten. Then it will decrement x by one. So x will equal
nine. Going here. This is a prefix, so it will first decrement
x. X value will equal nine, then we'll store
the nine inside y. So y will equal nine. As you can see, this is
a live demonstration of the equations that we
introduced in this lesson. Now if you have any
issue with any of these, I'll be more than happy
to explain them in more details to help
you understand. These are very
important. Potatoes. They are widely used and you
need to understand them all. If you are using the calculator without assigning
using an equal sign, the postfix will be treated
just like the prefix. It will be, there will
be no difference. But if you are dealing with
them with the equal sign, the postfix is different
than that prefix. Postfix will first
assigned operators, then increment, while the
prefix will first increment, then assign the operators. That's it for this lesson. If you have any questions please ask and if
you're on the board, I'll be more than happy to help.
15. Bitwise Operators in C: Hello and welcome to this new lesson in which we're going to talk about the
bitwise operators. Now, the bitwise
operators are used to manipulate bits
inside C language. Now, let's start with the AND, or, OR and NOT. As you can see
from these tables, when you see the unseen. This means that
whenever there is 0, the result will be 0. So if we have two
operands, a and b, a and b equals y 00, y equals 0. Since here we have a 0,
it will equal 0 as well. And since here we have
a 0 equals 0 as well, the only time that
the result will be one is when both of
the operands is one. So 11 equals one when it
comes to the Order table. Now, let's talk about the order. Now it comes to the old. You'll usually see this sign
between the two operands a. Now for the end, you will see the unsigned, so it will be a and b. In case of all, whenever you see one, the final result will be one. So here we have a one and
times that will be one. Here we have one and the
final result will be one. Here we have two ones. This one and this one. And the final result
will be one as well. Now the only case that
the result will be 0. There are no one's at all, neither a or B R1. So the final result will be 0, which is basically the opposite of that and almost oxygen, okay? Now for the XOR, as you can see in here, the x or is basically
this sine a x or b. In this case, if the two
are brands are similar, the result will be 0,
as you can see in here. And then if they are different, the result will be one. Now, the last one, which is the easiest one, it's done at sine. Sine is a union operator. These are by operator because
they take two operands. As we already mentioned, this is an integrator, So we will see not a nut
means the inverse of a. If it is 0, it will become one. If it is one, it will become 0. That's it. Now, let's see them in action. Let's say that we
have two values, x and y. X here equals 1010. Y is 0101, and these
are binary values. And we can talk about binaries
at the end of this course. Now, these two are the two
values that we will do these operators and
or not and XOR. Now when you are doing
the end x and y, the final result
will look like this. We already mentioned that the and sign means
if there is 0, then the final result will be 0. So when we are talking
about the end 01, we can do this to see the value of 01 on
equal 010 equals 001 will equal 010 equals here. So the final result
here will be 0. As we saw in the throat there. Now, regarding the dimension
that this is the r sine. Now if we there is one, then the final
result will be one. So 01110, only 01110
and also equal one. So it will be either a
binary value, 14 ones. All we can convert it to decimal value
using the calculator. Now, you can change the
calculator to poor grammar. And you can enter the
binary value 1111. And as you can see that
decimal value is 50. So this equals 50. Now they're not sign
is straightforward. Not x means this is the sign. Not, NOT x means
the inverse of x. So it will be 1010 since. Equals 0101. And we can use the calculator to
convert the value. We can write 1010, which is the original value. It equals ten in decimal. So let's try the new
value correctly. Now let's add 0101. As you can see, it equals five. So this will be five in this. I need you to memorize these
values because we will do this in a practical
way and few seconds. And we need to know that. Now x, XOR y, we already mentioned that
if they are both identical, then the value will be 0. If they are different,
the value will be one. Since all of these are
different, 01100110, then the final result
will be the same as here, 1111, which equals
15 in decimal. Now we can do our last one
about lumbering systems. But this is like a
prerequisite for this course. That's it for the
bitwise operators. Now, to make things more clear, we need to take a quick example. So let's move on. Now. Let's start by writing our include voids as use one main void. The language to see. In here, we knew we need
to define the values. So x will equal the
binary value of 1010. If you wrote it like this, it will treat it as
a decimal value. So to me, the compiler understand that it
is a binary value. You need to write 0 b. 0 b means binary. This is the prefix
for the binary. Now we need to add Y as well. So 001, 01. Now, once you are done, we need to start defining
new variables, ends. And operator equals x and y. And OR operator equals X or Y. And X OR operator equals x. The power of sine y. And NOT operator
equals naught sine x. Now let's blend this out
using a print f function. So I will write them
equals percentage d. And that's the result here. Slash n here. Colby this line based the tears, tears and paste it here. This was this NOT x OR operator, and the equation change the
Autopilot of the equation. Now, let's save it. Bitwise operators. Let's try to run this. Now. Let's swollen gcc c e x. Now as you can see here, we have 01515, which is
something we expected. And here we have minus 11. Now let's talk
about the minus 11. Now, at this level, we won't go into depth
of the minus values. But let me show you on the
calculator what will happen. Now the value is 1010. Once you choose the
bitwise NOT operator, you will see that the decimal
result will be minus 11. And this is because the 1010
or not treated on their own. The binary operation not is done on all the
bytes that are left. So yes, you did write 1010, but the rest of the bits
were written as zeros. And in this case they're not. We'll reverse them to one. Now in the coming lessons, we will cover the negative
values and how to print them, and how to interpret them
inside your console window. But at this point, what you need to understand is that when we did
the NOT function, we did it only on four bits. We didn't take into
consideration how many bits or bytes the compiler will
assign for the NOT operator. So if it has assigned one byte, then the final result
will be 10101111. Because one byte means eight bits and original
value is 0101. These are only four bits. Bits. There is also four
zeros that are hidden here. So we convert them to one. Now, other compilers assigned
four bytes for an integer. And then this case we will
have three more bytes of ones. That's why this is an advanced topic and we will cover it in
the coming lessons. Now, if you reach this point and you are able to apply the and OR XOR and learned how to
apply the NOT operator. Then you are good to go. Thanks for watching this lesson. Next, we will talk about
more bitwise operators that are advanced and used
in bit manipulation. But that's it for this lesson.
16. Bitwise Shift Operators in C : Hello and welcome to this new lesson in new trial going to talk about two more
bitwise operators, the right shift and
left shift operators. Now, the right shift operator is basically these two signs. And the left shift operator
is these two sides. Now, x, right shift by one, or y left shift by one. Now when we say alright
shift or left shift, we mean this in a
bit wise level. So let's say that we have this. And let's assume that the
width is only one byte, which equals eight bits. Eight bits, sorry. Now, these are our eight
bits in one week, 12341234. This assumed that this is x, and let's make x equal ten. Now, ten in binary will
be stored inside this. This is an eight bits variable that will
take the value ten. Now ten is in decimal,
not in binary. Ten equals 100101, and the rest will be zeros. This is the number in binary and associated
number is ten in decimal. So let's store the
value of 01010000. Now when we say x, right, shift by one, it means that
we need to shift all of these eight bits to
the right by one. So this will be moved here, and this will be
moved in this place. This would be often
this place, and so on. Now, we will have
an empty bit here and we'll take a
default value of 0. So the final result
will be 10100000. This is the right shift. We did shift all of these
bits to the right by one. So we lost this 0. This 0 was written here. And basically there is nothing
here and it will be gone. So the first bit here will be the one because we
shifted it by one. And the third bit will
be the second one, the fault will be the
third, and so on. Now if we executed
right shift by two, means that we are
shifting each of these bits two
digits to the right. So this 0 would be moved twice. This one would be moved twice. So if we move this
0 twice to be out, if we move this 0 to one, this is one twice. It will move to here. This is one and then two here. This is the other one. So this one will go away and this one
would go away as well. So start with 01, and the rest of these
bits will be zeros. This is when we
write shift by two. This is when we
right shift by one. Now what about left shifting? Now when we do the left shift, It's basically the
same principle. Let's copy this area and let's paste it. We're live here. Let's draw this most time
with the same value. Let's add 01010000. Now what we need to
do is to left shift. Let's say that
this is called Why We need to left shift y by one. So if we left
shifted this by one, this will be moved here, this will be moved
here, and so on. And we will end up with this
place as an empty place. So let's add 0 and empty place. Once we are done, we will lift, shift all of these so
it will be 0101000. So we lost this 0. Now, let's talk about moving
it twice to the left. Now here we moved it once
and we have this block, loved ones to the left. So that's, that's
why we have it here. Now if we want to move it
twice, we will add 00. Then we will add
this block, 010100. Does you can see this is the
log that we are focusing on. Now this value will
be moved twice, so it will be here. This value will be moved
twice from here to here, and from here to here. And this is why you
can find it here. So this is the meaning of
fried shift and left shift. And this is how it's
done in beds level. Now we will do it in C
using decimal values. Now to do it first, we need to understand how to treat decimal values and
how to deal with them. And we will have a
separate lesson for that, as we already mentioned. But for now we will
use the calculator. Now, let's take
the same example. Include a CDI or the texts. And then here void main. Void. Change the language to see inside here,
let's two variables. Now, let's define x as an
eight bit binary number. So at 0 be 00001010. Let's define y as well as the
prefix of a binary number. If you wrote the number
without the 0 b, it will be considered
as a decimal, which is the default number. Numbering system inside z. Z will be 00000101. This is y. Now let's use the right shift. Right shape. So integers, right? Shift equals the value of x, right shifted by 14. Lift shift the value of
y left shifted by one. Now, let's blend
the values. Then. Let's add the equation
equals person d slash n. And here,
add right shift. Now copy the same
line, paste it here, and change the equation with
y lift shift and left shift. Now, let's call
this bitwise shift. Now, let's explain
what will happen here. Now this will be
right shifted by one. So the final result will be, will look something like this. 0 will be 000101. There will be a plus 0 here. So we will have five
zeros and ones, one because we
write shifted this, so this will move to the right
one and it will disappear. This will be the first bit, as we already explained. And all of these bits will
be right shifted by one. So we will have this
as an empty place and the eight bit value and
will be replaced with 0. Now this one will
be left shifted. So the one will move
to the left by one. So we'll have an
empty place here. So in this case we will add 0, B 000010100 will be added here because all
of these bits will be left shifted by one. So the final value will be this. Now let's convert it to
decimal using that calculator. Here it is. Let's add
that decimal value, or the binary value of 0 is 0, 00101101 here equals five
in the salon. So five. Now let's clean. Let's add
1010 equals ten in decimal. So this will be ten. Now, this is after doing the right shift and
doing the left shift. Now let's save their work. And let's open up cmd to see if this
matches our calculations. So write GCC. See that, see, now it shows us that
there's a problem here. We forgot to add the hash sign. So CLRS now edit. As you can see, x shifted
by one equals five, which is this value, y left shifted by one equals
ten, which is this value. And this is how you deal with the right shift and
left shift operators. Again, you can write
shifts or left shift by 12345 or any number of you want. The number here
indicates the number of shifts to the
right or to the left. And the signs show
you that I election, this is derived,
this is to the left. Now if you added
one, only one sign, it won't be a shift or
a bitwise operator. It will be a greater than
or less than operator. So make sure that you
don't misuse this. Again, it can be executed easily using this
command, this command. And the other thing that I
wanted to mention is that NC, when you are initializing a
variable, if you add a 0, B means that you have to
add eight bits after that, indicate that this
is a binary numbers. 0, B is for binary. Now we also have 0
x four hexadecimal, but this is something
we will talk about and the numbering
systems lesson. And if you lift it
to the default, she will initialize the
variable equals 55 will be the decimal value because
it doesn't have any prefix, which is the default
value and see, and the most used value. And see. Now I just introduced the binary values
because we need them for that bitwise operator, right shift and left shift.
17. Assignment Operators: Hello and welcome
to this new lesson. No child going to discuss
assignment operators. Assignment operators
are used to assign different values using
different operators. Some of the operators we already talked in previous
types operators. The first one is the
assignment operator. It's a very straightforward one. It's basically the equal sign. And here we are assigning the value of 20 to
the variable x. Here we have an assign
the plus equals sign. This can be interpreted
to x equals x plus three. What this will do
is add three to X value and assign the
final result to x. So in this case,
if X equals ten, which is the
previous value of x, then plus three equals 1313 will be stored inside
X as the new value. We usually use the blast
equal three to short this equation into the
US onto this shape. Now, same thing goes for
all of these equations. X minus equal four is basically
x equals x minus four. If x is ten, then ten
minus four is six. So the value of six, which is the result of this, will be stored inside X. So X value will be
updated and become six. Now, going here, x equals
x multiplied by five. X equal the original value of x is 1010 multiplied
by five equals 50. So x will equal 50. This will be the new value of x. If we went down here
to the last one, AX equals X divided by two. So ten divided by two is 55
will be stored inside X. So the new value
of X will be five. This is the short form
of this equation. This is the short form
of this equation, and this is a short
form of this equation. Now, we also have the same thing for the bitwise
operators and or not. And we have it for that mode. Or the person to sign operator. Now, x equal one, it means X equals x and one. And since we are dealing with
when you are using AND or, OR XOR, we are dealing
with x as a binary number. It will equal 0101. And we want to add
one with this. It will be one here. So the final result
will be 1101. So three plus ten
equals thirsty. So the final result will be 13. Now, the same thing
goes for the r and x, or x equals x, or x equals x. The x or sine two. You need to convert 15 to the binary form,
010115 equals 1111. This is x value, which is ten, and this is 15. The final value. You have to offer them. 01 equals 1111. So the final value
will be 15 as one. So x one equal 15. Already mentioned how to do this and bitwise operators lesson. Now, the XSOAR is the same case. You need to add the value of x, which is the original
value equals ten. Two is 0100. Now we already mentioned
that if they are identical, the result will be 0. And here the result will be 0. Here the result will be
0. Here it will be one. So the final result
for x will be eight. So x equals eight
as a final result. This is the result of
the XOR operation. You can use the calculator
to make sure of the numbers. Or you can watch the lesson that we will add to the
end of this course. That is called numbering system. In case if you are
interested to learn how to convert binary into decimal
without using a calculator. Now, the last equation here is x equals x mod or percentage. For. Now we already know
that x value is ten. And we mentioned that you
need to divide it by four. So ten divided by
four equals two. Now, two multiplied
by four equals eight. Then minus eight, which is the original
value, minus eight, which is the result of
the multiplication, will equal the
remainder, which is two. So the answer here will be two. X will equal to. And I should ask, showed
you how to calculate it. First, you need to turn this
into our normal division, ten divided by four. It will equal to,
without fractions, you want using division, there are no fractions. Now, to note that in mind
that we need to multiply the result without fraction
by the division factor, which is for the
result will be eight. And then we subtract
the original value, which is 104 x minus eight, which is this value. That isn't would
be the reminder, which is two, x equals two. Now, the last two
assignment operators are the shift right?
And shift lift. Now when we write it like
that x shift right by one, we are applying,
right shift to x by one and assigning
the value to x. So it's like writing x equals x, right? Shift by one. Same thing here, x equals x left shift by one without
using the equal sign. And this is a very
important thing to know. As you can see in this lesson, we learned the assign, the ad and assign
plus, equals, minus, equals, multiply, divide
and OR XOR and the modulus. We also then the right
shift and left shift. Now let's do a quick
practical implementation for these values to
see the results. Now, first we need to include
the library as usual. Then void main. Void. Change the language to see. And let's define x equals ten. Now let's call this
assignment operators. Now, we already mentioned
that we need to try all of the assignments
that we just mentioned. The first one is x equals ten. We just tried it, the sign. Then we have x plus five. X minus equal three. X multiplication equal five. X division equal to. And we have the x and
equal, let's say one, x equal one, x, x equal one. And we also have a
right shift by one, x left shift by one. And we have the
modes sign equal to. Now, what I mentioned
that we must reinitialize x after each
of these operations. This is something you
must do to make sure that you are getting
the right results. So let's do this here as well. Well done. Now this one here. And blend the
values. So print f. Let's add person
through D and X. Now here I will
add the equation. But let's first copy this. I'm just doing this so that
we can see the final results. For each of these
operation, that's it. Now we just need to copy
these equations inside. The double quotations. Done. That's it. We just need this
equation here as well. Plus equal. Now, let's
try running this program. Now, GCC, let's zoom in nine, assign operator or the C, E. And here we have the results. Now ten plus equal
five equals 15, minus equal three equals seven, then multiplied by
five equals 50, then divided by two equals five. X equals two. I think this is the
percentage sign. Now to get to try
to add a dimension that you need to try to twice. Let's clear the screen, compile again and run. Now x mod two, mod two equals 0. Since it's ten divided by two. The reminder, now, x
equal one equals 0, x or equal one equals 11. You can check it out yourself. X, XOR one equals 11, x, right? Shifted by one equals ten, and x left shifted
by one equals ten. Now, all of the
results are correct, except for the last two, the right shift and
left shift operators. Now in some compilers, x shift left by one and
right shift by one or not recognize unless you add
the assignment operator, which is the equal sign. So to fix this, you need to go back to
your code and write x equal. X equal. Now, this will solve the
issue and the GCC compiler, other compilers will execute it as it is and show you
the right result. Now, let's compile again. See you again now as
you can see in here, x shift left to the right
plus ten in both cases, which is the wrong value. Now here, x shifted, left shifted here equals
five here equals 20, which is the right value. Since we added the
assignment operator. Now, these are all of the assignment operators
that you need to know. If you have any questions
about any of them or you fail to
implement any of them. We are here to help. Thanks
for watching this lesson. This is Ashraf from
educational engineering team.
18. Logical and Relational Operators: Hello and welcome
to this new lesson in which you are going to talk about rational operators
and true and false and z. Now, in C, any number that is not equal to 0 is
considered a true value. Five is true, minus 12 is true. False is true. As long as the value is not 0, then it will be treated as true. Regarding false. If the number equivalent to 0, then it will be
treated as false. But you need to know
that if there is a statement in C
that returns true, the compiler is free to
choose the value of the true. What's the role that must
be any number except 0? This is a good piece
of information that you must keep in mind when dealing with relational
operators and logical operators. Now, relational operators
are basically operators that check equality or equality, or more than or equal, less than or equal. And these things
that are used in if statements or
conditional statements that we will cover later. So relational operators checks
the relationship between two values by determining
whether it's true or false. Now, if x bar, if x equals ten, and y equals five. Like in this statement. This is a question. Checks if x equals y, this statement will return
either true or false. Now, since x and
y are not equal, it will return false. And we already mentioned
that falls equals 0. Now, if you want to not equal, this is a sign. Now, this will
check not equality. X is not equal to y. It will return true. And since x is ten, y is five, then this
will return true. The third one is statement
that will check a more than. So it will check if
X is more than y. And x here is ten, it's more than y, which is five. So this will return
true as well. This will check
more than or equal. So if X is more than or equal, it will return true
and it is more than Y. So this will also
checks as true. Here. It will check
if x is less than y, which is not true, so it will be false. This will check if x is
less than or equal y. And this is not true. So it will check as false. You need to take an OT. If you want to test and print a false value, it will print 0. However, if you taste
to print true value, the value is chosen
by the compiler. Frequently it is
either one or 255. We will check this in
a minute once we reach the practical section
of this lesson. Now, let's move on to
that logical operators. The ones that we
covered here are called relational operators or
relational operators. Here we have the
logical operators. The logical operators
are used in applying logical
operations between two values in which each
are considered either true, which is not 0, or
false which is 0. So we have the logical
and as you can see here, then as you can and
is written by writing to the logical OR is written
by writing to or signs. Now, now, the logical NOT. Now the logical not in here. X equals not fall. Since four is true because
it doesn't equal 0, then not true equals false, then x equals 0. Now, to test this out, Let's explain the logical AND, and OR before moving to
the practical section. Now here we have
three which is true. We have 0 which is false. We know that if that is true, which is one and
false which is 0. And the and operator, then the result will be 0 from the truth table
that we discussed earlier. Now here, 0 means
0, which is false. Three means through
which is 110. When it comes to
the ord function, it will give us one. So it will be either one or 255, which interprets as true. In this case, four is
true and not true, equals false, which equals 0. These are the logical
operators and the ones before them were the
relational operators. Now let's see how we can implement them
inside our compiler. Let's first include in the
library and write void main. Void. Now in here, change the language to see. And this are writing the lines. I will print them directly
inside the print f function. So here we will deal
with each of them. So let's say that we want
to start with the equal x. Equal equal one is defined x
and y as the two variables. Now, x equal equal y, n will equal percentage d, and we will add the
results in here. So we'll add x equal equal one. Now you can either
do this or you can simply add this
new variable here. Let's call it m and the
value inside M, Then trend. But to reduce the number
of lines inside our code, I will use this and instead, and save one line. That's it. Now Save, let's call it rational. Now, let's copy this line. Basic tiers. We need six lines or six
relational operators. And we need three lines
for the logical operators. So this will be x and, and, and, and as well. This will be all. This will be not x. So it will be the
same here, not x. Now here it will
be equal, equal. Not equal will be greater
than or equal, less than. Less than or equal.
Greater than. I'm writing the
same things that we just saw on the table. Just to see how the
results will print slash and made sure that each of these lines will be printed separately
in a new line. Now that's it. Let's see if
it will print out or not. And we can an issue
when we now G, C, N logic brush. And let's see, now a e x
e. As you can see here, we have the true and false. Now let's shake them out. X equal equal y. So they are not equal. It would be false, which is 0, x naught equal y, it will
be true, which is one. X is greater than one through
ten is greater than five, so it will be one is
greater or equal than y. That is also true. It will equal one.
X is less than y. It's false. So 0, x is less than or equal. Why it's false? So 0 again, x and y. Now we already mentioned
that and, and why? We'll ask if x is
true and y is true. Now, x equals ten,
which is true, y equals five, which is true, that none of them equals 0. So true and true equals true. Now regarding the
whole operation, x equals ten and y equals five. Now we have an error here. It's printing 0 because we
haven't changed this in here. So we need to change it, save the code again. Go back, clear. Sorting. By then. And here it is. True or true equals true. Now, not x, since x equals ten, which is true, not
true equals false. And these first six here, logical, sorry, the
relational operators. And the last three are
the logical operators. That's it. We're done with the operators in C. If you have any question
about any of these operators, I'll be more than happy to help answer all of your
concerns and questions. Just ask. Thanks for watching this lesson. This is Ashraf from
education engineering team.
19. Conditional Statement if in C: Hello and welcome this
new lesson in which are going to talk about
conditional statements in C. Conditional
statements executes operations under
specific conditions. There are two main types of
conditional statements in C. That IF statement,
the switch statement. The one that we'll
start with today, is the if statements. Now, if statement is
basically a type of conditional statements
that you can use to check if a condition
is true or not. And depending on whether this
condition is true or not, you can execute a
specific piece of code. Now, let's talk
about the syntax. Always make sure that you
have the syntax file, then you start
recording process. We will talk in this lesson
about the first case, which is only if condition
without else, if or else. Now, the syntax is as follow. You will write f and you
will write two indices. Now between the two
parentheses you write your condition like x is
greater than y, for example. Then you would add
two curly braces. Between these two curly braces, you write your code or the action that you
want to execute. If the condition is true, then execute this action. Otherwise this action
won't be executed. Now, to make things more clear, Let's see this in action. Now, let's include
our main file. I'll use one. And let's
add the main function. Now in here, you can
easily write anything. Let's initialize a new variable and give it an
initial value of 50, change the language to C. Now
we will use f statements. This is the syntax. If two parentheses
and two curly braces. Now press Enter here as usual, and between the
two curly braces. Now this is where you will write your codes or the action. And here you will
write the condition. Now we will write a logical
or rational condition if x greater than or equal 50. So this is the
condition and we know that it is true
because x equals 50. Now we will print statements. X is greater than or equal 50. That's it. If this
condition is true, it will execute this line. Otherwise want executed. Now, let's save the codes. Let's compile it. Now this is one, n is gcc c. Now, a lot explore. As you can see, the action was executed because
the condition is true. Now, if we change the
condition to make it false, Let's make it takes
less than 50. Now, you can go back here. You can compile the code
again and run the EXE. As you can see, when
you run edit EXE, nothing is printed because
this condition is false. So the code inside the two curly braces
will not be executed. And this is the whole point
from using f statement. So if the condition is true, execute the code between
these two curly braces. Otherwise don't execute it. That's it for the if statement. This is the first case. In the next lesson,
we will cover the second third cases
for if statements. If you have any questions,
feel free to ask. This is Ashraf from
educational engineering team.
20. Conditional Statement else if and else in C: Hello and welcome to this
new lesson in which we're going to talk about that second, third case of if statements. Now, in this case we have
something called else-if. Let's check it out. This is something we
are already familiar with from the first case. If condition then
execute the action. Now, if this condition is false and you want to check another condition, in this case, you need to add else
and leave a space, then add f. You will use
the very same syntax. As you can see in this area. If that condition is true, then execute this action. The only thing that's new
here is the L statement. Now, else here means that if
this condition is not true, then go and check
the other condition. But if this condition is true, then you shouldn't go and
check other conditions. You need to make sure that you understand this point well. If you added else, it means that the first
condition that will be true will mean that the
program will execute the associated action and will not check other conditions. Otherwise, you can
use only f and you can get rid of the else if you want to check
all of these conditions. So in this case, if condition is true, condition one true, it will not go and
check condition too. But if condition one is false, it will go and check
condition two if there is an else statement
before the F. Now, let's see this in action. Now let's use the same
code from before. This is f, x is less than 50. Then go right else if two parenthesis
and two curly braces. Now here we will check if
X is above or equal 50. Then in this case, we will print this so
x is less than 50. Or else, if this
condition is false, then go and check
this condition. If greater than or equal to 50, then print this line. This is the use case of elseif. It will check this condition. If it is true, it will
not check this condition. It will just execute
this line and this will go out of the
if else statements. But if this condition is false, it will check the
second condition. If we have other conditions, and this condition is false, it will check the next
condition, and so on. But you need to add the else
statement before the F. Now, if you need the program to
check all of these conditions, even if this is true and you didn't
check this one as well, you will need to remove
the else statement. And it will check
the first condition. Then it will go and check
the second condition. And to go and check
the third condition and whatever f statements
that you added. But we usually use the else statement to
reduce less CPU load and to reduce the
amount of lines that we need to check and execute. So if this is true, there is no need to check this. Now let's try and execute
this code in here by GCC 11. Condition if else state dot c. Right. Now as you can see it printed, x is greater than or equal
50, which is this line. What happened here is that the program went
through this one. It checked if x is less
than 50, and this is false. So what happened here is that it didn't execute the print f function because this is false. It went to the second condition. Else, if x is greater
than or equal 50, and this was true. So it executed this line, and this is what you see here. Now that leads us
to the third case. The third case is
simply the else case. In this case, this is as
a line of code that you write to make sure that if none of the
conditions is true, it will do something else. So if condition one is false, else if condition two is false, else if condition
three is false, then go and execute whatever is inside
the else statement. You just write else and
add two curly braces. Now to apply this to our code, you can simply go to the code. And in here, you can write
else and add two curly braces. Now between these
two curly braces, you can write a
print f function. None of the condition is true. Now let's make all
of them false. So let's make it x is
greater than or equal to 60. So this one is false. Else if this one is also false, so it will go and execute whatever we wrote inside
the else statement. Save your code, will go
to the console window. Now let's compile the
code needed a screen, hey, the EXP. And here it is. None of the conditions is true because the first
condition is false. It went to the second condition. Else, if 50 is greater
than or equal, 60 is also false. So at least it lifts that second condition
and went directly to the else statement and
executed the code inside this else statement is
like a default case. If none of the
condition is true, you need to write
something inside here to show to the user, like if he wrote
our own password for a specific number of times or the wrong password. I have checked using
if and else if statements for different
passwords that you have set, then you can write none
of the passwords is true. And this is one of the
use cases, L statement. It can also be used
without the elsif. So you can simply remove these two lines and leave
it just if and else. And it will work
without any problems. That's it for this lesson. Now, to summarize things, F is used to check a condition. If you have more
than one condition related to the same variable, you can use if and
else if statements. If you have an action
that you need to execute, if none of the
conditions is true, then you can write it inside the else statement between
the two curly braces. And it will be executed if none of the conditions is true. One last note is F. F statement is true. It will not go through the
else if and else statements. It will just execute this
action and get out of the if, else, if and else block. This is a good way
to reduce the amount of time spent on
checking the conditions. So if this condition is true, it will not check
this condition and will not go to the
else statement. We mentioned that if you want it to check all of the conditions, you need to remove the
else statement from here. And it will be just a
normal if statement that checks the condition. If the condition is true, it will execute the action. That's it for this lesson. If you have any
questions regarding the if and else if statements, I'll be more than happy to help. Thanks for watching
this is Ashraf from education engineering team.
21. Local Variables and Global Variables: Hello and welcome to this new lesson in
which are going to talk about local variables
and global variable. This is a very important concept when programming
in any language, not just in C. Now, let's talk about local variable. A local variable is
the variable that is defined inside any function. This variable can be accessed only on the function
that defines it. So as you can see here, we have a local variable. It's integer x equals ten. So let's type is integer, the variable name is x
and the value is ten. Now this function
can only be accessed unseen inside the funk. See, this is the only function
that sees this variable. This is why we call
it local variable. Now, the local variable
can be seen only between the two curly braces on
which it was defined. So as you can see
from here to here, That's why it's called local. Now, the global variable, on the other hand, is the variable that is
defined outside a function. This variable can be accessed in any
function in the code. So let's take a look
to the right here. We have integer x, and it's defined outside the main function and
outside function one. Since it's defined
outside of them, it can be accessed by the mean and can be accessed by func one. And this is our grid. Global variables are, they can be used anywhere
inside your code. That's why they
are called global. So whenever you want
to create a variable and use it inside more
than one function, you need to create it outside the functions at the
beginning of your codes, usually after the hash include. But if you want a variable to be accessed only inside
the specific function, then create inside
the function itself. And in this case, it will be a local variable. Now to make things more clear, let's take a practical example. Let's create a new
project, actual fluid. Now, void, main void. Go to the language C. C. Let's save that. Let's call it locally. Now, let's create a
variable here, x equal ten. Let's now I planted the regular way x. This is the file name. Now if you tried to
execute this code, it will execute without
any problem with this is a local variable and can only be accessed in between
these two curly braces. Now if we were to create
another function, this is the function
prototype and x void. Now let's make the
implementation here. The same line. Print f x. As you can see, this will try to
print the variable x, even though it wasn't
defined in this group. And even though it
is a local variable. And we already mentioned
that this function will have no access to this variable
since it is a local variable. So let's call the
function when x. So this is the prototype,
as we mentioned, this is the implementation and
this is the function form. Now save the codes. Go to File Cmd, G, C, C. Okay, now let's look at
this error. Function. Print x. X is undeclared. And this is because we are trying to access a
local variable that was created inside the
main function, inside another function. And this is not possible because this is a local variable. Now, let's create
a global variable. Here, int y equal 15. And let's add y here. So this will print
the local variable, and this is our global variable. Now going back to the C code, it will compile without any
problem, as you can see. Now, how about E? And it will print x and y. Now y can be asked in the main function and
in print x function. Because it's a global variable. While x can be on or can only be accessed inside
the main function. And this is the main concept of local variables
and global variables. Fast rushing this last one, this is Ashraf from
educational engineering team.
22. General Rules Regarding if statement: Hello and welcome
to this new lesson in which are going to talk about some general rules when
dealing with if statements. Now, the first rule is that else if and
else are optional. Now you can use if statements without
having to write else, if else, if you have only
one condition to check. These are optional
add-ons that you can use depending on the use
case or the end goal. Nested IF is allowed. This the diff is
basically writing an if statement
inside another FCC. And I'll show you
that in a minute. It is not allowed to write
code between f and f or else. Because if you did write code, you will be separating the F
from the else if statement. And it will show
you a syntax error. Use round brackets to an if statement with several conditions to
avoid technical errors. Some people use the
f statements without fighting the curly braces
or round brackets, which goes a lot of syntax
and technical errors. And I'll talk about
that in a minute. In case you only need one action in a multiple
condition if statement, you may opt to not
use a bracket, although it is still
highly recommended. Now, let's see these
nodes in action. Now let's fight and new code. Let's add the main function. And let's choose the C language
as our default styles. Now, int x equals 50. We need to check
if x equals one. Inside here we would write if x is greater than or equal 50. Now between the
two curly braces, we will use print f function. And in here we will
lie x, The equals 50. Now, what we meant with the
fact that else and else, if our optional is that you can choose not to use them
and there will be no errors, no syntax errors, and nothing. What we meant with nested
F is that you can write another if statement inside this if statement between
these two curly braces. So you can go after
this print f line and check using another
if condition if x. Let's say that here we
are checking if x equals 50 greater than, let's say 50. Let's make it 60. And inside it, we need
to check if equal, if x equals 50. Let's say this will be our code. So we will print the same line. X is greater than 50, and here x equals 50. Now this f statement,
as you can see, these are the two
curly braces and read for the first if statement. Now inside it, we wrote
another if statement, and these are the curly braces for the second if statement. And this is something
we call nested IF, which is writing an if statement inside another if statement. Now, this is allowed
and you can do it, and most of us do it to
check multiple conditions. So when the first
condition is true, we need to check if the
second condition is true inside the first condition. Now, this is the
print F function that will be executed if
x is greater than 50, which is the first condition. Now inside the second condition we add another print f function. So if x equals 50
or let's say 55, in this case, it will
print the second function, which is the print f function. Now, let's try and execute this. Let's call this F fools. Now open containing folder and run it with C and D.
Now here we will add g, C, C, E, E. As you can see, x is greater than 50 because x equals 6060. But since x is not, o does not equal 55, it didn't grant the
second if statement. Let's change this with 16. What will happen?
Executed and run? As you can see here, the first condition is true, x is greater than 50. So it printed this line
inside the if statement. And inside we have another if statement inside the first if statement,
which is stdev. This if statement will
check if x equals, equals 60, and that is true. It will execute the
second print f statement and it printed x equals 55. This is how to use
nested F. Again, this is an if statement
inside the if statement. As you can see, This is
the first if statement, and this is the second
if statement inside it. It's not like the else-if case. Now. It is not allowed
to write a code between the f x equals ten. And let's add
anything inside here. It's not allowed to
write codes between this curly braces and the
else if statement like this. If you try to write, let's say a print f
statement outside that first if statement and
before the else if. Let's try to execute our code. As you can see,
we have an error. We have ends without a previous f. So it's indicating
the else-if statement, which is this statement. This is because we added
a line between them first if statement and
the else-if statements. So this new line breaks the block that if
and else-if block. So this if statement
is one block, this else-if statement
is another block. And you cannot use else if or else without an
original IF statement. That's why there is an error that else without a previous f is detected and it showed
you act syntax error. To fix it, you need to remove this and make them
stick together. And this will be
treated as a block. Now, another thing is that you can use this else if statement without
these two curly braces. You can remove them. And you can just drive
this f print f line. Now, I don't usually
recommend this because it will give you a lot of technical errors
and logical errors. Sometimes the code might work, but it will behave
in our own way. We usually prefer to add the two curly braces
after the if statement. But let's say some rules. If you want to use the F
without the curly braces. The first law rule is that if you wrote the f
without the curly braces, you are only allowed to
add one line of code. To check this, Let's
comment all of these lines. So I will add a
comment here and here. And let's use this statement. Now. If x equals, equals ten, here you can write
it x equals ten, and it will be executed. Now if you want, try
and after line inside the if statement, x and deeds. If one step. Now, since you haven't
added the two curly braces, that beginning at the
beginning and the end of that action code. It will only execute this code. Now, let's check this
inside the console window. Okay, now let's save the code. I haven't saved it. Now, it's saying that we have implicit declaration
of cooking rent. We need to add F here. It's a spelling mistake. Now, let's execute this. As you can see, x equal, equal to n, and this is true. It executes the first line. The second line
is not related to the if statement and it
was executed anyway. Now let's make the
condition wrong. So if x does not equal ten, now, let's compile and run. As you can see, even though
x does not equal ten, it did executed the second line, which is the print f statement. X indeed equals ten. This is because if we
haven't added curly braces, if will only take one line after the if statement and treat it as if it follows
the if statements. So if x does not equal ten, that is not true. So this line will
not be executed. This is the end boundary
of the if statement. Since we haven't added
the curly braces, this line is assembly line of code outside of
the if statement. It's like you have
written this line of code away from the
IF statements. Because IF statement without the curly braces can only
execute one line of code. Now if we wanted to execute
both of these lines, we need to add the curly braces. Now, when we execute
the code one more time, it will print nothing because these two lines are related
to this if statement. Let's try this out. As you can see. It didn't print anything. As you can see, both of
these lines following the if statement and it is false or no lines were printed. But in the case of
no curly braces, it did print the second line. Because we already mentioned that without the curly braces, if will only take one line after the if statement and
treat it as if it is the line of code or the action that it needs
to execute or not execute, depending on the condition. Now, again, we don't recommend at all not using curly braces. Using curly braces is a
good programming practice. Without them, you might end up having a lot of
logical errors, but you need to know the
specific inflammation because sometimes it comes in a multiple choice question or a quiz or exam or an interview. They will bite if
statement without the curly braces and tell you what is the output
of this program. Now you need to
understand that the F is only related to
this line of code. This line of code is not
relevant to the epstein. That's it. That's watching this lesson. If you have any
questions for him. This is Ashraf from education
and engineering team.
23. Switch Statement in C: Lambda will come this
new lesson in which we're going to talk
about switch case. Now, switch
statements is like if statements are used to check if a condition
is true or not, then it will execute a command. Let's switch statement
permits users to choose only one choice
from a set of choices. The variable values are compared with the values and
different cases. And then it matches
the case value. In case there is no match, the default block is
automatically executed. Now to make things more clear, let's take a quick
look at this diagram. Here is our expression, which is basically the things that the variable that
we want to check. Now, if this variable equals that first
constant, let's say one. It will execute code block one. If it is not equal to one, it will go to check
the second constant. Let's say that it is two. Now, if the variable
value equals two, it will execute this block. Otherwise, it will go
to the third constant. And it can go on and on up
to n number of constants. Now, if none of these constants
or conditions is true, it will go and execute
the default code, like the L statement and
the if else statement. So if none of the
conditions is true, go and execute the default code. That's it. Now, to make things more clear, Let's see the syntax. First. Let's add that
include statement. And that main function. Now, change the language to see save the code as 13 switch. Now, let's define a
variable and x equal five. Okay, let's make it equal three. Now. Which case syntax? First, you need to
add that switch and two parentheses
and two curly braces. Then start striking the syntax for writing anything inside
the two parentheses. Here you write case. And these two dots. Then you need to add break. The syntax is a
little bit complex, but you need to memorize
it as it is. Now. You need to add another case, depending on how
many cases you need to add, another break, another case, and
a little break. And then finally, you need to
add the default and break. It's now, let's start by adding here the
variable that we want to switch over, which is x. Now, this is the first case. So bite anything inside here, What makes sure that
it is a constant? So let's live one. So if x equals one, then write your code here. Actually one. This is condition one. Let's say that we want
to use print f to print function, to
print something. Let's say x equals one. Now, copy the same thing. Here we have the
seconds, which case? Make it to say x equals two. Here we have the third one. Make it three, and
write x equals three. Now if none of them, Let's name this default action. And let's say that x
value is negative. If none of these is true, then it will execute this line. Now, let's save this code. Let me explain the code
blocks then we can talk about each of these parts. Now as you can see here, we have the switch statement. Now what I need to understand is that between these
two curly braces, we must add a variable
that we want to check. Now, there are two curly braces. Everything else is
placed inside of them. Now the syntax is clear. We have a case and
we must break it. Now, the case ends
with two dots, while the break ends with
a semicolon means one. Now, the case, there must
be a space after the case, then you can add the value
that you want to switch over. So this will check
if x equals one. And if this is true, it will execute this action between the case and the brake. Now, if this is false, it will go to the next case. Now, if this is true, it will execute this and get
out of the switch statement, just like the if,
else if statement. Now, if this is false, false. Then it will go and
check the second case. If x equals two. If x equals, equals two, it will execute this action. Then it will break
from this case, and it will get out of
the switch statement. Again, as you can see
here, we have two dots. Here. We have a semicolon. Here we have a space between the case and the value
that we want to check. Same thing for the third case. And you can add a fourth, fifth, and any number of cases. The last case will be
the default case here. Now the default case here is basically the else statement
in the if-else statement. Now, if none of these
conditions is true, then it will go to
the default case. Here we have two dots. And to execute the action or anything written between
the default and the break, which is this line of code. It looks like an if, else, if and else statements, but using a switch case. And it's a very familiar
or a very famous way to switch over a variable to check if it is the
right variable or not. And they usually use it
when you're creating a menu for the user to
check an item form. Now going back to our code, let's save the code
and try to compile it. Now here we have GCC
13 switch state. See a dot EXE. And as you can see, that
this line was printed. And it was printed
because x equals three. So once we enter the
switch statement, it shaped if x equals
one, this is false. So it went to the second case, if x equals two, which is o. So it went and checked if x
equals two, which is false. And it went from this case to the next case to check
if x equals three, which is true because
x equals three. So it printed this line between
the case and the break. And if we're not any
other line inside here, it will be printed and executed. Once this or one of
the cases is true, it will execute the code inside the case and it will get out of
the switch statement. So it will not check the default and it will
not execute the default. That's why we have
x equals three. That's it. This is the switch statement
and this is how to use it. If you have any questions regarding anything
unhealthful ****. This is Ashraf from
educational engineering team.
24. Switch Statement General Rules in C: Hello and welcome to this
new lesson in which we are going to talk about
switch statements. Rules. These are rules that
you must take into consideration when you are dealing with switch statements. Now, we'll talk about that constant for
each of these cases. Number 123. This
constant must be unique, so you cannot use
the same constant. Wife's like 11. This is wrong. You cannot add a variable
here like y or x. Or a must be a constant value. Default label is optional. So you can use this
default labeled with a break statement or
you can delete it. The switch case statement
will work without any issues. This is, I would point
to keep in mind. Now, only one
default is allowed. So you cannot add a default
here and add a default here. This will give you a
syntax error because it will disturb that program. It won't know which of these
defaults to be executed. So we have only one default. Now, second thing about
the default is that it can be placed anywhere
inside the switch statement, so you can place it here. All you can place it here. As long as it's
only one default, it is allowed to
place it anywhere. So make sure that you add
it whenever you want. Just make sure that
it's not duplicated. Now, the break statement
ends with a switch. Each of these cases much must have its own
break statement. So in this case is true, will execute this code, then break that switch
statement altogether. So it will get out of
the whole statements. If you forget to add
the break statement, this will cause logical errors. In case I break statement
does not exist. Like if we remove
this break statement, all subsequent codes shall
be executed until the end of switch or until the program
finds a break statement. So it will execute this, then go on to execute this until it reaches
the break statement. Make sure that you don't forget any of the
break statements. Each case must have its
own break statement, and I usually buy the
whole syntax first, then I start to code. Another good thing
that you need to know is that nesting is allowed. And what we mean by nice
thing is that you can write a switch statement
inside of these cases. Let's say that we
are inside case two. We can easily write
switch case 0, a break. And we can add another
variable here, let's say y and switch using
this variable default. And after the default, we have that break. Now, this is allowed
inside any of these cases, and this case is called
nested switch statement. So it's piping switch statement inside that original
switch statement. Now, to summarize, our case, constant must be unique and
it cannot be a variable. A default label is optional. Only one default is allowed. However, it can be placed anywhere in the
switch statement. A break statement
ends the switch. In case I break statement
does not exist, all subsequent codes
shall be executed until the end of switch or
finds a break statement. Nesting is also allowed. Just like we showed
you a few seconds ago. These are the main rules that
you need to keep in mind. Usually most of them come in multiple choice questions and
true and false questions or interview questions
regarding programming in C. If you have any questions about any of the things
that we explained, I'll be more than happy to help that's watching
this lesson. This is Ashraf from education
and engineering team.
25. Do while loop in C: Hello and welcome this
new lesson in which are going to discuss
the do-while loop. Now, it is very
similar to why Lu, but since we have ado before it, then there is some changes or some differences, let's say. Now let's look at the syntax. As you can see, it
starts with ado, then two curly braces. Now, the condition is the
last thing written here. As you can see, these are
the two curly braces. Then we have the block of code. After that, we have
that condition, which is why two parentheses. And between the two parentheses, we have the condition. And here we have a semicolon. Now this is the syntax
and you must memorize it. Since we have the dual and we have the curly
braces with a block of code prior to the while
loop, the while condition. This means that the
loop body statement will be executed at least once before checking whether the condition
is true or not. This is because we have the
condition here at the end. And this is the main difference between the while
and do-while loop. So the do-while loop
executes the code block prior to checking whether
the condition is true. It will continue
to repeat the loop for as long as the
condition is true. Just like the while loop
does shrink in this case. Let's see this example. Here we have an
integer x equals one. We have due to statements
and we have this condition. Now the first thing that will happen, it will go to the dual. Then it will execute these two lines before
checking the condition. So it will print and scan, will print, I'm in the loop. Then it will scan something from the user,
Let's say a number. Now while the number that
was count is not equal 0. So what this condition is true? Just like in the while loop. It will go back and execute
the code block again. Once it finishes the execution, it will go to the condition and it will keep
looping like this, then like this, like
this, then like this, and so on, until the
condition is false. Just like what we explained
in the while loop. Except that in this case, if the condition is false
from the beginning, the code block will
be executed at least once because we are
executing the code block before even checking
the condition without the condition
is true or false. And this is because
we are starting with the du, then the y. Unlike the wine,
which is the second, the second time
that we explained. Now, let's take a look
at a practical example. First, let's include
stdio.h edge void mail. Here we need to add voids. And let's choose the
language to be c. And x equals one. Do while the condition
is x not equal 0. This is the syntax. Now press Enter here. And here we need to
add the code block. This add a print f
statements inside the loop. Then scan f, read an
input from the user. And let's say X percentage d. Okay, now let's say
that enter x then slash and without the slash, and we will leave
it like that and the same line. Now
let's save it. Let's call it 16, do while. Now let's go to the
compiling process. Now go to the file open
containing folder CMD, gcc, c of x. We are inside the loop, enter X, so X, let's say 123456. As long as x is not equal to 0. It will keep doing that. Now once x equals 0. Now here's the condition. X naught equals 0. Now, if x is 0, so 0 equals 0, so this condition is false. It will get out of. The do-while loops. So let's write 0 here. As you can see, it's out. Now, let's say that
we want to make the condition false
from the beginning. So let's say that
x not equal to 0. And here we have it. X equal one. Now, as you can
see in this case. Now if we enter 0 here, let's go to execution. Now if we enter 0 here, means that 0 not equal 0. Now we already have
this condition as false because x equal 11, not equal 0 is true. So if we replace this was 0, let's say we save the code. So this, if it is
not a do-while, nothing will be printed. But since we are
using a do-while, and we already mentioned
that the code block will be printed prior to chicken without the condition
is true or not. The condition is false. It's not true because
as you can see here, 0 not equal 0 is false
because 0 equals 0. So the condition is, and
nothing should be executed. But since we are
using a do-while, It will be executed
at least once, even if the condition is false. So let's run it. As you can see, we are
inside the loop, enter x. Even though the
condition is false, we are getting the
code block executed, as you can see in this line. And this is the main difference between the do-while and while. Now if we are using
the same code, like this code, I will
copy it as it is the wine. And here I will add the two
lines, these two lines. Now let's comment this
part that the wild. And let's play the
wireless cell. Now if we save this code, go back to the execution. The screen. Now edit EXE. As you can see,
nothing was printed here because the
condition is false. Since we are checking the
condition in the wild before executing anything,
nothing was printed. But in the do-while, this was printed, even though
the condition is wrong. This is the main difference
between these two. I hope that now you
are getting what I was trying to point out on
Beck's explanation. Now, if you have any issue or any problem
understanding the do-while, I'd be more than
happy to elaborate. Questions on the
assignments will follow to make you
understand things even more. Thanks for watching this lesson.
26. Break and Continue in C: Hello and welcome to this new lesson in
which are going to talk about break and continue. Now, break statement is basically writing break
and adding a semicolon. Vague statement can be used with the loop to exit the
loop immediately and execute the outside code
starting after the loop. While the continue statement, which is basically writing
continue, adding a semicolon. When skip the current iteration and jump to the next iteration. It will skip this code and go directly to
the next iteration. The purpose of that
break statement here is to break out of a loop. For example, if
our code is asking I user or the input to
enter an integral named x. And this condition is true. It will break the while loop. Now to make things more clear, let's take these two examples. This is an infinite loop. An infant who was basically a loop that will
repeats forever. So this code will keep
repeating forever. Because while one,
and we already know that any number that
is not 0 is true. One here means while true. This condition will
be true forever. That's why we call
it infinite loop. This loop will break once
the user enter x as one. So it will weigh the user to
enter something, anything. Now, if the thing that the
user entered equals one, it will break from the loop. Now, that's it. The loop will end without having to make the
condition false. Now for the
continuous statement, let's say that
this is a for loop that loops from one to nine. Since I is less than ten. Now, it will keep executing this code with each iteration, just like how we exit planned in the fourth lesson
FOR loop lesson. Now, this statement,
if I equal equal sex, will check once I or once the
iteration reaches that six, or I equals six, it will kid this hydration. And when we say escape, it will not execute
anything in here. It will just jump to the
action and increase to seven. So this line will not be
printed when i equal equal six. And this is what we
mean by skipping I iteration using the
continue statement. Now to make things more clear, Let's do this using
a practical example. Now let's start by adding
our hash include. As usual. Man, void. Now here between the two
curly braces or curly braces. So we need to add our code. Let's show that anguish to see. Now, we need to create while loop this, the break sentence. Now inside this while loop, we need to make it infinite loop and we need to read
input from the user. We will live person
to D. And Let's say, let's create a variable, call it M, give it an
initial value of 0. Now inside here we need to
check if m equal, equal one. Then I can add it
in the same line. Or if an alkyne, that method, as long as it's only one line, it will be executed
without that curly braces, as we mentioned in
previous lessons. Now, if you try to
test this code, let's first save it. Now save the file. Go to File open
containing folder cmd. Here. Let's zoom in. You'll see the filename. Continue to see. Now. As you can see, it's waiting for us
to enter something. Let's enter five,
then enter 678. It will keep asking
for more to keep scanning the incoming value since we are inside
an infertile. Now once I write down one, it will break from
the while loop. And break statement
basically is used to exit while loop immediately and
execute the code outside, since we don't have any code, will go outside the while
loop and finish the problem. Let's try one. Enter. As you can
see, now it's out. So it went outside the while loop and the
program finished executing. So this is the main purpose
for using the brake line. That's basically
to break out from all the safe infinite
loop condition. Now, this is the first thing. Let's continue statement
using a for loop. So here's our four inside here. And I equals 0. I is less than ten plus a plus. So here we are going to
ask if I equal equal five. Then continue. Now, before moving forward, we need to add a
print statement. Print f. Let's say
percentage d slash n. In here we need to
print the value of all. Now this line does not exist. Let's see the output in both cases without the continue
on with the container. Now this will print
values from 0 to nine. So let's go File Open. See Cmd, G, C, C, 17, brief tenure. Let's see. Maybe ten. You now ID EXE. We'll print values
from 0 to nine. So 0123456789. Now if we enable this, once I equals five, this condition will be true. Continue, we'll skip this
anything inside the loop, and it will go to
the increment or the action block
of the for loop. So it will not execute any
code when it reaches that, I equal equal five. So five will not be printed. So let's test it out here. Let's save that code. Go back, combine it, executed. Now as you can see,
let's convert them. Here you can see 01234, and here you can see them. Now five is here. And the previous execution
without this sentence. While here we don't have five, we have on 6789. So it did skip five. Now we already mentioned
that continue statement. We'll skip the current iteration and jump to the next iteration. And this is what happened here. That's it. This is the break
statement out of a loop. If the condition is true, this is the
continuous statement, which is basically
a statement that is used to skip a specific
iteration inside a loop. That's it for this lesson. If you have any
questions, I'm here to help. Thanks for watching.
27. Functions in C: Hello and welcome
to this new lesson, which is one of the most important lessons
in this course. In this lesson, we will
talk about functions. A function is a block of code which only runs
when it's called. It is used to perform
certain actions. And they are important
for reusing code, like define a code once
and use it many times. To use the function, you need to call it inside your code. When calling a function, you may send some
data or some input, and it calls return
back and out for you. The most common function
is the main function, which is the entry
point that we have been writing since the
beginning of this course. Now, our wave function
is basically this. We write it like that. Void main, two parentheses, and two curly braces. Now, this is the function name. And main is reserved
for that main function, which is the entry point
between the two parentheses, we add the inputs. Now if we don't want
to add any inputs, we add the word void. Now this is the
returning data type. If this function does
not return anything, then we call it void. We add void in the
return data type. And the code that you
want to execute or reuse, you usually write
it inside here. This is the codes. Now, this is one of the
functions that we have been using a lot since the
beginning of our program. To execute the function, you will need to call it. The function provides you
with advantage that it is defined one time and can
be executed many times. So it takes the same
size in the memory, whatever, how many times
it will be called. When you call a function, you can send to it some inputs, as we already mentioned. And the example for a function that we
have been using since the beginning of
our coding process is the print f function. Now print f function is
basically a function that takes inputs between
the two parentheses. And the execution
of this function. Function will print
output on cmd, which is the command
line window. Now the print function
takes a string as an input argument and
print it on the screen. See projects composed of
one function or more, which are basically the
main print and scan. Now, print f, scan f mean. All of these are called C programming functions
or built-in functions. Because we do not create this
function, we only use them. We call them by their name. But if you create a function, let's call it, let's say that you call the function Ashraf. Then this is called a
user defined function. So anything that you create is called user
defined function. I think that you use, and it's already created by C compiler or C
programming language, is called a built-in function or a C
programming function. Now, to make things more clear, we need to talk
about what should you do if you want to
create your own function? Well, if you want to
create your own function, you need to understand that a function consists
of three parts, and you need to
memorize these parts. The first part is the prototype. And this prototype
declares the function. So it tells the
compiler that there is a function with this name
and these specifications. Usually the prototype includes the return type, as
we already mentioned. The function name,
parentheses here and there, and the input between
the two parentheses. Then the line ends
with a semicolon. This is called a prototype. Now, this is the first part. The second part is called
the implementation. And it's basically the place where you write
the function code, defines the function behavior. And it has the very same
shape as the prototype, as you can see, except
for the same column here, we don't have semicolon. Instead we have the
two curly braces. Inside them. We will write the function code. And you can see that here. Here we have the
first curly brace. Here we have the second. And between them. We usually write our
function statement or code. Now, this is the second part, which is basically writing down the function itself with the code that we
need to execute. Now the third bark is
the function call. The function call includes the function name and any impulse between the two
curly, the two parentheses. And if it returns something, we need to store the
data that this function returns inside a variable. Now if it does not
return anything, we don't need to store anything. Let's do a quick revision. A function that you create consists of three things
that you need to write down. The first thing is
that prototype, which is basically a way
to declare the function. It's a line that ends
with a semicolon. It includes the
return type, if any, the function name
that you define, and two parenthesis with
an input between them. If there is funny. Now, if there is no input,
you need to write void, and we will cover this
in a simulated lesson, the meaning of a void datatype. Now, after writing
the prototype, we need the implementation, which is the function body. We use the same line
as in the prototype. But instead of ending the
line with a semicolon, we end it with two curly braces. This one and this one. Between the double curly braces we arrive the function code. The first step is
the function call. Inside our main, we can call
our function with its name, function name, and
we send two inputs. Now I know that things
might not be clear. So let's take an example to
make things even more clear. Now, this is a function
that we defined and we are going to execute
and few seconds. The first part here
is called prototype. This is aligned. This is the return type. This function will
return an integer. This, the subword is
the function name, which is the second
part of the prototype, as we already mentioned. Now, we have two parentheses, as you can see here and here. And these two parentheses, we'll end with a semicolon. Now this function
will take two inputs. Now it can take more
than two inputs depending on our
program and our code. But you need to make
sure that you have a semicolon between
each of these inputs. And you have the type and the name for each
of these inputs. So we have the data type for
the first input integer, and the name is x comma. Then the data type of the
second input is integer. And its name is why?
The second thing? Now, this is the first
thing that we have. It's called the prototype, as we already mentioned. Now, the second thing
is the implementation. This is number two. And as we already mentioned, the first-line is the same. It's a common thing between the prototype and
the limitation, except for the semicolon, we have to remove it. And after that, we
add two curly braces. And between them we
will add our code. Now this is the code
that you can reuse, and this is the function name. This is the return data type, and these are the two inputs. This function will
subtract x minus y. So we'll create a new
variable inside the function. It will equal x minus y, so the result will be inside z. And since this function
returns an integer, we need to return
the summation value. And we end the land
by a semicolon. So this is our code. This block is called
implementation, which is the second
thing that you must do. This is the first thing. Now the third thing
is the function call. Now, as you can see, the function call is simply calling the function
with its name. Here we have the function name, we have the two parentheses
and the semicolon. But then instead of
adding and x and y, we are adding two values to be passed to the value,
to the function, sorry. And you need to add
the same color. Now, can you pass variables? Yes, you can replace
this with x and y, or a and B or whatever you want. But you need to make
sure that there are only two inputs. Sense. The function takes
only two inputs, and you need to make sure that the variables are
integers and have values. Now once you send the
values to this function, you can call it, which is the third thing. We call this call
or function call. Function call. The function call is
basically calling. So it will give 52
instead of x and y. And five will be
here to be here five minus two equals three. So Z will equal three and
it will return three. Now, when you call the function, will return an integer. So we need to create an integer
value of variable, sorry, we call it var to store the value that will be
returned by the function. And in this case would be three. So var would equal three. And when we print this, it will print result is three. This will be the output
in our console window. And here we call the function, as you can see in this line. Inside the main function. You can call it again
and again and again without having to live
the whole function. Each time you call it, you just write it once
and you can call it, let's say ten times, 20 times, whatever you want. Now to make things
even more clear, let's implement this pixel size and our Notepad Plus, Plus. Now, to do this, we will start with
the usual thing. We will include stdio.h. Now the first thing that we
need to do is add a void, main void, which is
our main function. Now, what we will
do next is simple. We will add the
function prototype. Usually the prototype is
everything above the mean. So here, function prototype. So the function will return an integer and it's
name will be solved. It will take two inputs, x and y, and we will end
it with a semicolon. Now, the second step,
this is the first step. The second step is writing the function itself
underneath the main, outside them in and underneath it as you
can see or below it. We are outside the main here, like three, sorry.
Implementation. Now what you need
to do is simply copy the whole line here
from the prototype, paste it here, but
remove the semicolon, as we already mentioned,
add two curly braces. Now we need to make sure that
this is identical to this, except for the semicolon,
you'll have to remove it. Now this function will
subtract x minus y, a simple C code. And whenever a functional
thing or something, it must contain the word return, it will return this value. Now, let's go to the
last step, the call. So let's say we want to call
this function, give it 52. Now, if we did this and we executed the
code, nothing will happen. This function will
subtract five minus three, minus two, so it would equal three and the three value will
be floating in the memory. So in order to control or to save the result
from this function, we need to store it
inside a variable. Let's call it var.
And it must be the same data type as the
return data type here, here, here, and here. So it returns integer. So we need to receive that return value
inside an integer. Now once we have that return
value from the function, this is the function call. I've felt. By its name. We can print it out using a
print f statement results. The one person does D. Here. We need to add above. That's it. Let's save the code. Let's call it functions. Function this. Now,
let's compile it. Gcc, c, e, As you can see, a result equal three. Now, we can play a little
bit with this code. You can pass variables
instead of constants. Let's say that we
want to ask the user to enter two values. So first number, and we will take it with a scan f statement,
percentage d. Let's call it first. Now. Let me define two variables. Integer, first number of
people on 0, integers, second, number equals 0. Now, this is the
second number variable that we want to solve this
kind of value on side. Let's call it seconds. So here's the first
number. The second number. Now we will call the function, and instead of adding
the two constants, we will add the first number and the second number
that the user enters. Then we will print
out the result. Let's add slash, slash
here and another slash n. Now let's call this
again, compile it. As you can see in the first
number, let's say 10. Second number, let's say
for that is alt equals x. As you can see, it was
implemented without any issues. So a function call can take constants and tag can
take variables as long as these variables out of the same type as
the implementation. And the prototype, always make sure that you are using the same type or
the same datatype. Or else you might end up with
syntax or logical errors. This is how easy it is to create a function,
user-defined function. As you can see, this is the first step
function prototype. The return data type, the function name, and the two inputs ended
with a single. Second step is the
implementation, which is the function itself. This is above the mean
or below the main. The same as the prototype, but instead of a semicolon, you need to add two curly braces and add your code inside it. The last step is
the function call, which is basically calling, just like calling someone, calling a function by its name, passing the two
inputs and getting the result that it will return, storing the result
in side the end, which is the variable that we want to store the results in. That's it for the
functions lesson. If you have any questions,
please ask in the Q&A will be more than happy to help.
28. Void Keyword in C: Hello and welcome to this
new lesson in which we are going to talk
about void keyword. We already mentioned void and we talked about it in
more than one lesson. But in this lesson we'll talk
about it in more details. The void keyword is used to any function to give
the meaning of Nothing. For example, a
function that takes void and returns void
is in front of you. As you can see, this is
the return data type, as we mentioned in the previous
lesson, and it's void. Now, it also takes void
between the two parentheses. That only goal from this
function is to print Mike. Now, this can be
changed with anything. But this function will never
return any variable or any value and it will not
take any input values. So for example, if
we need to define a function that
takes no arguments, we would fly between
the two parentheses, the void keyword, just
like in this example, if we need to define a
function that does not return any output,
we would like. Instead of that return
type the keyword void. And this is the
void return type. The function can take
an odd nor three. Turns out, this is basically the whole meaning
of void means nothing. If you saw the word
void anywhere, it means that this place will takes nothing or
we retails nothing. Now, this is another example. This is our prototype for a function that returns
nothing and takes nothing. That's why we have
void here and here. Then this function
is print by name. Now, as you can see, this is the implementation
of this function. We use the same prototype, but we add two curly
braces and the semicolon. And inside it we add
the print state. Now, the last thing is the coal. And calling a void
function is easy. You just call it by
its name without writing anything between
the two parentheses because it doesn't take
any output and without receiving a value from this and storing it into a variable. So to call a function
that takes void, then write its name and nothing
between the parentheses. Just like in this line. She returns void,
like in our example, then don't receive its
output in a variable, just call it as it is. Now, let's do this in
a practical manner. Let's draw our own void
function or list suitably, right, sorry, our
own void function. Now let's add the hash
include that we are used to. Let's zoom in and STD or edge. Let's save the file and call it. Let's void keyword. See. Now inside here, after using the hash include, we need to call the
void main function. Now the main function
and we void owl can be and let's add void. This is the syntax
that we usually use. Now let's try the
prototype here. Void, print name, and void. The filename, as we
mentioned is void keyword. Now let's copy that prototype, based it down here, and remove the semicolon, two curly braces and rent f y. Now to call, this
is the first step, this is the second step. The third step is calling this
function inside the main. So simply call it by its name, print name, and add two parentheses without
anything inside them. And it wants to return, I think you save file open
containing folder CMD. Let's copy the name gcc
and the name of these two. Now aided EXE. And
as you can see, it executed this function. This is a function that takes nothing out of
the tails is nothing. We call it a void function. This is how easy it
is to implement it. So now whenever you see a word
for it, it means nothing. Don't input anything
wanted to tell. I think that's
watching this lesson. If you have any questions about the void function,
we are here to help.
29. How to Create a Library in C: Hello and welcome to this
new lesson in which we are going to talk about libraries in C. So in order to
create a library in C, first, you need to know
what is a library. A library is basically a set of functions that do
specific actions. And we usually create
libraries for sensors for, let's say an LCD display for key bad for reading a
temperature value, or, or simply a calculator. So let's say that
we want to create a new library and call
it a kilometer library. We already mentioned
that the library is a set of functions. Now these functions to be used, there are few things
that we need to write. We know that each function
has three things. That prototype,
implementation and the core. Now, in order to
create a library, we need to know the number of
files that we can include. Basically can include
unlimited number of files. But we will only
concentrate on two types. This is the first file, and let's call it C or main. That C file. Now you
can call it calculator. This is the second file
that we need to call, and let's call it calculator h. This one is called
a header file, and this one is
called source file. Extension for this
file is dot c and for this file is dot H
stands for header. Now, we need to know what are the things that we will write inside each of these fights. The handout file will
include the prototype. Prototype, this thing. So now we are done with this. The C5 will include
the implementation. So we are done
with this as well. Now, the thing that
remains is the coal. The coal is basically
something we will do in our main C5, which is the main program file. Now, to do this or to call these functions
inside our main file, we need to include our library. So we'll write the word include. But instead of fighting
these two signs, we will add the double
quotation sign. And the only thing
that we need to include the header file. So we'll add
calculator, the tailor. This line will allow us
to call any function inside source file phi of that calculator library. So if we did this,
we wrote this line. This means that we
can see any function inside that calculator
dot c file, which is the file
that we created here. So to wrap things up, this is our library. This library has two things. First, a source file, Let's see. Second is file dot h. Now let's call them Can
stands for Calculator. Now, that source file, as we mentioned, will include the function's implementation. While the head-up file will include the
function's prototype. Now to call the functions, we need to add the hash include, which is this line. Inside our main. If we added this line, it means that we can
see and interact with any function inside
the calc dot c, which is the source file, this file or this one. And this is basically how easy it is to create a function in C now or solid to create
a library in C. Now, to make things more clear, we will clear that
very same thing with the very same name
calculator library inside our notepad and see how
things will be executed. So let's do this. Let's go to our C compiler. Now this is our main file, so we need to do what we do use running,
which is include. And in men void. Once you do that,
we need to save it. I'll create a new file. Let's see the numbers to
make sure that we have the right numbering
for all of our files. We have it at 21. So let's call it 21 C libraries. Now inside it, we need to
create domain dot c file. This is our first file. Now we need to create another
file and name it can see. And this is the source file. Source file. Here we will write the
implementation functions. And we need to
create another file, which is the header file. Functions prototype. Just like what we mentioned
in our explanation, makes sure that it has the
same name as the source file, but ends with h of z. And click Save. Now, the implementation
for odd functions, we need to add a function that will sum
two numbers, x and y. And we need more than we need for functions
actually adds multiplication, division, and subtraction for the calculator to
add two numbers, to multiply them, to divide
them, to subtract them. Now, this is the header file where we wrote the
function prototype. Coby these prototypes
to the source file. Best at here, the blessed that
semicolon with goo braces. Now, we need to start
swiping our codes. As usual. This is nothing new. We are writing the
implementation for each of these function. So equals x plus y. Print f. Some results equal percentage d. And we'll add that here. Copy the same blinds
here, here and here. Now instead of sum here we
need to add multiplication. We need to add division. And here we need to
add subtraction. Now we are done with that function's implementation
inside the source file. But in order to use the print f, we need to include
the standard library. This is the standard library. Now, we finished the prototype, we finished the implementation. We need to call them. So we need to add another hash. Include just like what
we mentioned with two double quotation, like calc. Now, this means that our main function now can see the functions created
inside the source file. So let's call them. Let's call the add
function and add 56. Let's add a slash n here to make sure that they will not be
printed on the same line. Okay, go back here. You need to call
the add function, the multiplication function, divisional function, the
subtraction function. Now, we need to say, I think if you want to
include the library that is created by the
compiler creators, you can add it between the larger and smaller
sites like that. But if the library
that you want to include is created by a user, you must add it between
two double quotations, as you can see here
in this library, was created by us. That's why it's added
between double quotations. And this library was created by the compiler,
manufacturer or creators. So it is added to an integrator
rate or less equal signs. So as you can see, this is a good piece of information that you
must keep in mind. Now let's save and let's
try compiling our code. So here we need to
write GCC main.cc. Now, as you can see, here, we have fewer errors. This is because we tried to
compile only that first C5. Now write this as C. C, C, as you can see, it combined without any errors. As we mentioned earlier. Compiled to see files, we need to make sure that
we named the files here. Now let's execute a e x e. Now as you can see, some
results equals 11, okay? We have a problem with
the implementation. So here we'll have plus, we'll have multiplication,
division and subtraction. Let's call it again. As you can see, 11300 minus one. That's it. Now, as you can see, our code works just fine
without any issues. And we just created
our first library and see if you have written everything that I wrote and
you created this library, then this is the time
that you must clap for yourself because you just created your very
first C library. I'm sure that this is a lot
to take in from one goal. So you might need to pause the video and
go back and watch it again to like understand things. And try to repeat
it more than once, then you can go to the implementation and apply
the same thing that I did. Let's summarize what we have
covered in this lesson. In order to create a C library, we mentioned that you
need to create two files. You can create more
than two files. It's basically a
design approach. But what we will create is a source file and
the header file, the source file,
another file must have the same name with a
different extension. The source file is the
header file is dot h. Now, these two files are basically the library and we are
creating a calculator library. The source file will include the implementation or
functions and limitations. While the header file will include the
function's prototype. Now, remaining
thing is the coal. In order to call
these functions, we need to include
the library name. The library header
file is the thing that we must include
in domains sci fi. So I patch include and add the header file
calculator, that edge. Now you must add it between
two double quotations because this is a library
created by a user, not our compiler
related library. Stuff, including
you can easily call the functions
inside your domain, because once you include this, it means that you have the prototype for all
of these functions. Since the header file
includes the prototypes, you can call them easily. And this is what we did here. This is the function's
implementation inside the source file, the header file calculator, the edge includes the prototype and the goal is inside the main. But before calling, we need
to make sure that we are using the hash include
calculator that edge. And this is written between
two double quotation, not between larger
and less than signs. This is a user created library. This is a computer or a
compiler related library. That's it for how to
create a library and see.
30. Arrays in C: Hello and welcome to
this new lesson in which we're going talk
about arrays in C. Now, artery is a group of data that holds fixed
number or value. All of them are the same type. For example, let's
look at this syntax. This is called the
array definition. That data type here is interior. It's like defining any variable, usual and define a variable. We write int x equal 0. But in this case, instead
of fighting this, we are adding these
two brackets. And between them, we
will add the array size. This is the size of our array. So this line will do
this in our memory. It will create five variables. The name will be array, and you can access
them using the index. The number that we write
here is called index. The first thing that
you need to know is that that account starts from 0. So if it has five here, means five elements,
so it's 0 to four. Now, that index
always starts from 0. It means that the
array has elements from element to element. For, as you can see here, length must be constant value
and it cannot be variable. You cannot add
variables to five here. You cannot do this and array and add X between the
two brackets. This is wrong. It will give you
a compiler error. Array. The brackets takes only constant, doesn't
take variable. And this is what happens
when we execute this line. It will create five
variables and we can access them by
simply writing a 0. And we will see this in
practice in a few minutes. Now. This line creates an array
of integer of size five. I love that example
here is that the array can be initialized during
the time of definition. Using this syntax,
you can write array five and the two curly
braces here and there, and start filling them up. Now since it's an
array of integers, we will add integer
values and you must separate them with a coma.
As you can see here. The line must end
with a semicolon. Now, when you do this, it will create this
thing inside our memory. To create array 0, give it
a value of one at anyone, give it a value of two array
to give it a value of three. Array three, give
it a value of four. 3a4, give it a value of five. And this is basically
these values. As we already mentioned, we'll start with 01234. This is how easy it is to initialize another way
during the definition. Now, there are
some special cases when initializing the array with values less than its length. So here we have the length five, and we only added two values. The remaining elements will
be initialized with zeros, as you can see here,
here and here. So it's like you wrote 12 000. These two are the same. As long as you are not adding
the rest of the values, the compiler will
autocomplete them with zeros. Now, if you try initializing the array with
values more than its length, this will lead to a
compilation error. So let's say that this
array has five elements. Let's go back and we added
another comma and six, another comma and seven. This will most likely give you a compilation error
because you are adding seven elements to
an array of five elements. So five elements, this
must be only five. If you add a seven or six, it will give you a
compilation error. These are few of the
things that you must keep in mind when
dealing with an array. Now, let's talk about
accessing array elements. What you need to know
here is that all, all array elements can only be accessed on the same
statement at initialization. Like in this example. You can access all of the items. And here we accessed
all of these items. Now, the point is after the initialization that they can only be accessed
element by element. So once, once initialized and you want to add a new value, you must add it
using this syntax. You will write the array name, two brackets, the
element number, then the equal sign. And then you can add the value. Again, that array
index starts from 0. So you need to take this into consideration when you
are writing this number. You can use a variable to
indicate for the element index. So this can be replaced
with a variable, let's say x or like array I. And we can add it
inside a for loop. This I can be a valuable, but this is during the, let's say, accessing
of array elements, not during the initialization. Now if we tried adding a variable inside the
initialization line, like in here, it will give
us a compilation error. But you can access
array elements using a variable that
won't cause an error. Other ways that you can
access every element is by scanning a variable directly
inside an array element. So the elements are being
treated like normal variables. So everyone is basically
a variable just like x, y, and any other variable. So you can easily
access it by simply writing and the elements. And just in this
case it's an array. You can even use them inside
the print f function. As you can see here, we
are printing element 0 and we add percentage d
just like a normal variable. And then instead
of adding X or Y, we are adding the array
element that we want to print. Now, to make things more clear, we will do this in
a practical manner. So let's get started. Hello, and now let's add hash include and N main void. Now let's save this. Let's call it array. Folks will be 22. If I recall. I always see. Now, that's it. Let's start by
creating a new array. We already mentioned
that you need to write the data type name,
brackets array size. So let's create an array, array and make the
element number five. Now, there's more than one
approach to creating an array. You can twist like that, or you can directly initialize
it with values 12345. Or you can simply initialize some of the
elements, like 12. As we already mentioned, this will add zeros and
the remaining elements. This will add these
elements inside our array, and this will not add any
element inside of our array. The first piece of information that we didn't
mention is that you cannot add a variable
here next to the five. This is forbidden and C. Now if we tried
adding a variable, let's say x equals Pi. Let's slide compile this. Now let's go to raise it. Now, as you can see. Now it did compile correctly, but we have a problem. Now. This problem, Let's
go here and try to print. Let's add some values
here into that array that we just created with
a variable initializer. Let's print the values
inside that array. 0 equal percentage d. And similarly like
array 0, array. And we want to print this value. Now go back, try to compile, and you'll see that
we have an error. Variable sized object may not be initialized, as you can see, and it's indicating this exists element in our
array initializers. As you can see, it's not taking the array elements since
we have a variable here. Now the fixed this place, this would five
common this line. And it should print the
value without any problem. As you can see, our
element 0 equal one. And this is the first thing
that we need to mention, which is do not add a variable inside the
array initialization. Now, the second thing
that we need to mention is that if you try
to initialize an array, let's combine these two
and work on this one. If you try to initialize
an array with more than its capacity. So here we'll have five elements and we added seven elements. Now, this will cause
an error as well. So GCC, as you can see, excess elements in
array initializer, and it's pointing to
an amine stamp 67. So it's indicating that
these two elements are extra elements and your
array size is only five. So to fix this error, you need to remove them and
make sure that you only add elements with
the same number as inside the two brackets. Now, the last thing that
we need to mention is the artery or accessing array elements after
the initialization. To access them, you need to rename two brackets and the element that
you want to access. If you want to access
the first element, that will be elements 0. The second element
will be element one. Because we already
mentioned that the counter inside
the array starts from 0 to less than the maximum
value, which is five. So from 0 to four. If we want to access
element number three, we need to count 012. So here we need to write two. And this will help us
access this element. We can send a new value to
element number to here, which is basically the
three inside of our array. We can replace it with thin. And this is how
you can access and other elements to read the value inside it or
to change the value. Now you can also print a value, element, percentage d. And we can add the array name. As usual, we can
add any numbers. Let's say that we want
to print this value. So 0123, we need
to add three here. Now let's comment this line. We don't need it. Now I want to blend two values. Array two, since we change
the value and array three. Now let's compile and execute. As you can see, the new value here is ten and the
value here is four. We printed values number
23, which is 104. Now if you want to print
all of that array elements, you can add these 01234. Like kind of go back,
compile and execute. And here we have the
one to 10451245. Now, this was a place with 33 words are produced with thin. And this line. Now since we are printing
using the very same line, but only one thing is changed, which is the value
inside the two brackets. We can replace all of
this with a for loop. We can add a for loop. Here we can add n x equals 0. X is less than x plus a plus. Now we can cut this line here. The place this would be x. Now, this is the thing
that most people fall for, which is the condition
inside the if statement. Should they bought it 654
or lists or equal or less. So in my case, the best thing that I prefer to do when
dealing with arrays. Simply adding x is less than the number
inside the brackets. So x is less than five. This will help us print all
the values inside the array, except for element number five, which is an element
that does not exist because we have 01234. So always when dealing with for loops and arrays and lists, and add the number between
the two brackets to make sure that you are printing all that Ombudsman side battery. Let's execute this line. As you can see, 12345. Now you can make things even
more interesting by adding another percentage d here to indicate that element
that you are presenting. So we will add another variable. If we added x here. And trying to combine
the codes that someday it will print this array element 0
is one array element, one is to array
element to this three. Element three is four are
relevant for is five, which is a very interactive way of fronting array elements. Now to make things
more convenient, we need to start the elements
with one instead of 0. So you can easily do
this by adding last 12 between brackets
or parentheses. Now, combine and run, as you can see at element 12345, It's more convenient for
the end-user to see one. There is nothing
called element 0, since he doesn't know that
an array starts with 0. So it's more
user-friendly to start with one user interface. But you must know deep down
that it starts with 0. This is how you can easily
print the array element. Now in the next lesson, we
will take a good example of how we can take elements into the array and print elements on the display just
like this execution. Now the last thing that I
need to mention here is maybe some of you are asking why using arrays to begin with, why don't we use variables? Now? We can use variables. You can go and create, let's say five variables. Give them these values. X1, X2, X3, X4, X5. And you can simply print them using five,
print f statements. Now that it was
basically created, or let's say used in C, to make sure that you
don't waste your time by creating a 100 variable
of the same datatype. Since we are getting
five integer variables, why don't we create the mood single line
instead of five lines? Why don't we print them with
two lines and instead of printing them with five
print f statements. Now this appears more convenient when you are creating an array
of 100 elements. Let's say that we wanted
to spend a 100 element, not just five elements. It won't be convenient
to create a 100 integer variable and
keep naming them X1, X2, X3, X4, X5, X6, up to x 9900, and giving them values. The easiest way would be to
create an array like this of a 100 elements and
to print it using two lines and instead of
a 100 print f statement. And this is why
Hadley was created. Now if we try to
execute this line, you can see that our compiler printed
the first five numbers. And as we mentioned earlier, if you haven't added the
rest of the numbers, it will automatically add zeros. So here we printed
a 100 variable with only two lines
using a for loop. This is something you
can do with arrays, but you cannot do with the regular integers
and regular variables. So you can either go
with this approach, you can simply use one line of code to create
these five variables. And this is the main point of
using to save time to make sure that you get the
best results out of your, let's say, data management. And to easily manipulate print, take n values inside your array. That's it for this lesson.
31. Arrays example in C: Hello and welcome
to this new lesson in which we are going to take a practical example of
how to use an array. So let's get started, right, include a CBIO, delta
H and main void. And let's write, let's
say the code inside here. 23 example. Let's see. As usual, let's add
the filename here. Now let's create
another and marks five. Now this is an array that will take five elements
and we call it marks. Now we will ask the user to enter his mark using a for loop. So integer x, x is
less than five, as we already
mentioned, X plus plus. Now scan f will be
used the same as with variables percentage
d and marks of x. Now to make things
more convenient, let's ask the user to enter
his mouth and your mouth. Okay, now, let's add another percentage d here to indicate which
mark you will be entering. And in here we will
add x plus one. Now, this will ask
the user to enter, say first Mark, second
mark, third Mark. And he will keep
entering values. And the for loop
will make sure that each new value would be stored inside one of
that array elements. We have five elements. Now, the second thing
is printing a marks. So we'll copy the same for loop. We will remove the scan f. We will tell him that first mark equals array here. So first mark x plus one. Now this will make
sure that we are printing the values that
the user gave to us. So he will enter five marks, and we will print
these five marks. The first percentage d
will be replaced with the mark number, which is 12345. And the second percentage d will be replaced with
the market itself. Now, let's compile the code editor. And here you can see it's
asking us to enter first mark. So let's write 98 is 706050. Now as you can see,
second mark is 83rd, mark is 74th mark
is 65 miles is 50. Just like what we
entrance here. Now. It did start with two, not 0. So we need to add
0 here and 0 here. And we need to add
a percentage d here are solid percentage n. So now let's start again. 9080706050. And here there are 9080706050. Now what if you want
to print the average? It will be a very
easy thing to do inside this application
since we are using arrays. So n sum or average and sum. Now in here, inside
the for loop, we will add plus equal, sum plus equal marks of x. So each new value of x
that their users entering, we will take the value
and add to the sum. Once we are done
with this for loop, we will have all the
values inside the sum. We can easily go here and write average equals sum
divided by five. And we can print
the average here. About equal
percentage d average. Now let's try this
out on your screen. Now, let's run line, line, line, line, line. So as you can see, these
are the five marks. And this is the
average which is 90, because as you can see, I wrote all of them as night. This is how easy it is to create a program that takes Mars from the user and prints his average without having to
create five variables, without having to
sum these variables, and without having to use five, scan f statements and
five print f statements. So we only did this with
a few lines of code. This is, and this is
how great arrays are. That's why you should use arrays whenever
you feel that you have same datatype for
a lot of variables. Just like our marks here or
any other type of situation. Again, arrays will make
your life much easier. You can do the
work without them, but you have to keep copying
and pasting lines of codes. Losing memory, and losing time. Thanks for watching this lesson. If you have any questions
about any of that line, of course that we wrote here.