Transcript for:
C Programming Basics and First Steps

hey guys my name is father manodar and i am a software developer here at programmies i'm really glad to be an instructor for this video series on c programming and in this series we will learn everything you need to know to get started with c by the end of this series you will have a good understanding of c and programming in general so let's get started so what is c programming c is a general purpose programming language that has wide range of application it was initially developed to rewrite unix operating system apart from that modern web browsers like google chrome firefox database management system like mysql and hundreds of other applications you see c is pretty old language created during 1970s and with the rise of new languages like python and javascript there may arise an important question is he outdated the simple answer to this is no while these new languages have their own benefits c is still a popular language for several reasons c is very fast and efficient so if you are working an application where efficiencies must like compilers or operating systems c is a good choice also c programming allows us to access the hardware of computers on a deeper level that modern languages do not that's why there is still a lot of demand for c programmers according to glassdoor a popular job portal site there are 18 000 active job openings for c programmers in united states in fact many universities teach students c programming as the first language and it was the first programming language for me as well there is a good reason for it learning c helps you understand how the software and hardware interact with each other as c allow us to directly work with computer memory so on the question of should you learn c while c programming may not be the hottest language out there we at programmies believe every programmer should learn c eventually it helps us to understand programming from a deeper level here is a line from joel spalsky the founder of stack overflow if you can't understand why this code copies a string you are a programming based on superstition so getting to the basics and understanding it is the must for every programmer before we learn to write our very first program our computer needs to install a sip compiler and a text editor to make this process easier we have created an online tool that lets you run c on the browser without installing anything to access our online c editor we'll open our browser and go to this url www.programmies.com [Music] slash c dash programming slash online dash compiler [Music] you can also find the link in the video description below we will write our code in this area and when i hit run it will execute the code and so output in this area now let's write our very first c program we'll create a program called hello world if you do not know a hello world is a simple program that displays hello world on the screen since it's a simple program it is often used to introduce a new programming language to beginners lighting even a simple program like this also involves a couple of steps so bear with me first we need to create the main function int mean with parentheses and with curly braces this is the main function it's the starting point of every c program now to mark the end of this program we will need to use return statement like this although this part is not necessary it's considered a good programming practice now to print the text in c programming we first need to import the file named stdio.h for that i'll write here has include stdio.h for now just remember that every c program will have this structure and we will write the code inside this curly braces so to print the hello world i'll write here printf bracket inside quotation hello world and end this statement with a semicolon now let me run this code as you can see in the output we get hello world here are the points to take away from this video every c program start from this main function we will write our code inside these curly braces if you're just watching videos without actually writing code you may soon forget and lose interest so i highly recommend you to practice what you have watched as we move forward in this course in this video we will be learning about variables more specifically we will learn how to create variables and store data in them so that we can use them later in our program a variable is a name given to a memory location inside our computer where we can store data let me give you an example i will go to my compiler here you can see int is here the name of the variable is is and to create this variable we use this keyword ind this end keyword is a data type which indicates that the ace variable can only store integer values in c programming every variable must have a type like this and we will learn about these data types in detail in next video and in this video we will continue using in to create our variables once we create a variable like this int is we can also store data into it like is equals to 25 here we have first created a variable named is and then we have assigned an integer value 25 to this is variable by the way we have to use this semicolon at the end to mark the end of this statement and it is also possible to create variable and assign value in a single line let me show you how so instead of writing these two lines of code we can just merge them and write a single line of code like this so i'll cut this out if you have watched the last video we have used a structure that every c program follows i'll get back to my compiler if you remember this is the basic structure of c program so now let's add a variable inside this main function int is equals to 25 if we run this code a variable is created but will not see any output and to get output in c programming we will use printf function before we print this variable let's first try to print a normal text then in the next step we will learn to print variables now to print a line of text we use the printf function print f drag it inside quotation c programming and we will always end this statement with a semicolon now let me run this code and we can see c programming is printed on the screen by the way our variable is is also created but we haven't seen it because we haven't printed it now let's print our variable i just want to print a variable so i'll cut this part that prints a line of text then i'll write printf bracket inside quotation percent d comma and the name of the variable in this case the name of the variable is is to print a variable in c programming we use something called format specifier here percent d is a format specifier now let's run this code as we can see 25 is printed as an output so what happens here is that here person d is replaced by the value of a is variable if we have to print other data like characters and decimals we use different specifier like percent c and percent f which we will discuss in the next video let me modify this program a bit here i'll write here is colon now let me run this code again now we can see this is column is printed as it is and percent d is replaced with the value of a's now that we know how to store data in variables let's see how we can change their values yes we can change the values of variable that's why it is called variable let me give you an example i'll go back to my previous example currently the is variable stores 25 now i'll assign a new value to this a's variable is equals to 31 and i'll print this new is semicolon percent d and comma and again is and end this with semicolon let's run this program as you can see initially the value of is was 25 then i change the value of is by assigning 31 now let me put this second line of code into a new line for that i'll use backslash n and i'll run this again and you can see the output it is more readable and more clearer so here this backslash n is a new line character simply speaking it is a enter key if you are enjoying this video you will definitely love our interactive c programming course and on programmies pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box so far we saw that we can assign data directly to a variable however we can also assign a value of one variable to another let me show you an example in this example as you can see we have assigned value 33 to this first number variable and we have then printed this first number variable now i'll assign another variable second number and instead of assigning any data i'll assign here first number then i'll print this backslash n so that we can get a readable output second number equals to percent d and name of the variable that is second number now let's see what will happen i'll run this code and we can see first number and second number variable holds the same value 33 let's see how this program works line by line first we have created this variable first number and assigned value 33 to it then we have printed this variable and then we assigned another variable second number and assigned the value of the first number to it then we have printed the second number we can also declare multiple variables in a single line let me give you an example here i have created two variables variable 1 and variable 2 and i have separated them with a comma in a single line let me modify this program a bit i'll assign a value 25 to this variable too here the same two variables are created however the value of variable 2 is now 25 by the way in c programming if we do not assign a value to a variable its value cannot be determined in this program variable 1 will have some random unpredictable value however some compilers may store 0 in them ok guys we need your support to keep this type of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses at this point we have covered all the basics of variable before ending this video let's talk about how to choose a good variable name if you have noticed i have used descriptive variable names like a's and first number we could have given variable names like a instead of a's and fn instead of first number it works just fine however it's hard to understand what a means just by looking at the code so when we use good descriptive variable names it becomes easier to understand the code and to make the variable name descriptive we may need to use names having more than one word in such cases we follow the camel case format while giving the names to the variable in camel case the first word will be of small later and the first letter of the second word will be of capital letter like the example shown on the screen by the way there are some rules you need to know while creating a variable you cannot create variable names with space in between you cannot start variable names with numbers and you cannot use certain words as variable names we cannot use if as a variable name because if is a keyword these keywords are part of c programming syntax and have special meaning in c we will learn about these keywords as we progress through the course it's time for programming squeeze can you guess the output of this program hey guys welcome back to this series on c programming in this video we will be learning about data types in c more specifically we will learn about different types of data available and how to use them data type specify the type of data that can be stored in variable let's look at this example here is a variable and int is the data type of this variable the data type determines two things first the type of variable the n-type means our variable can only store integer values second the size of the variable the size of an in variable is usually four bytes that means an in variable can store 2 to the power 32 distinct values by the way in c programming every variable we create must have a data type assigned to it now let's see different data types available in c and how we can print these variables let me copy a list of data types here in our text editor so that it's bit easier for us to see what they are used for now let's try these data types in our program one by one we have already discussed this in-type variable can only store integer values that means it allow us to store whole numbers the number can be 0 positive number and negative number let's see an example as we have already discussed in our previous video this is the basic outline of every c program so i'll continue on this and i'll assign int is equals to 10 here you can see i have assigned 10 to our in variable is now let's print this using printf function print f bracket inside quotation percent d comma name of the variable that is is and end this with the semicolon let me run the program as we can see 10 is printed on the screen here person d is replaced by the value of a's variable and if we want to make our output much more clear and readable we can add text here like this and let's run this again and as you can see our output is much more readable than before in c programming we use either float or double data types for creating variables that can store decimal and exponential values the only difference between the double and float is the size the size of float is usually 4 bytes and the size of double is usually 8 bytes hence double can be used to store numbers with more precision now let's try to use float and double in our code we'll start with double type double number equals to 12.45 here number is a double type variable and we have also assigned a floating point value to this double variable now i will print this number using printf function as before printf bracket inside quotation percent lf comma name of the variable number for double we use percent lf format specifier so now let's run the program as you can see the output is printed on the screen however instead of 12.45 it shows 12.45 with multiple zeros after that this is because the double variable takes six digits after decimal points so it fills the remaining decimal places with random values if we want to avoid those random values we can print output only up to two decimal point all we have to do is to add point two after the percentage now let me run this code as you can see only two digits after the decimal point is printed and we can also use the float data type in the same way so i'll write float number 1 equals to 10.9 f and i'll print this print f bracket inside quotation percent f comma number one [Music] here i have added a float variable with value 10.9 and to indicate it's a float value rather than a double value i have added f at the end of the number here f is not a text it's just the syntax we use in c for float numbers and percent f is the format specifier we use for the float variables now let me run this code as you can see our output is bit messy so i'll use backslash anyare to make our output much more readable let's run this again and you can see the output here as in double we can also remove the unwanted digits after the decimal in float as well let's try it i'll add here 0.1 after the percentage and i'll run this and as you can see we get the desirable output by the way we can also use double and float to store exponential numbers let me give you an example first i will clean up my editor now let me store an exponential number double number equals to 5.5 exponential 6 here 5.5 e 6 is equals to 5.5 into 10 to the power 6 let me print this number to verify this [Music] now let me run this code as we can see this number is indeed equals to 5.5 into 10 to the power 6. here is a tip for you even though both float and double can be used for floating point numbers we suggest you to use double simply because double can store decimal parts with more precision for larger numbers okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses now let's talk about character type data we use the care keyword to represent character type data and to print them we use person c format specifier let me give you an example your character is a care variable and i have assigned jet to this character variable and notice we have used this single quotes for character data and also notice i have used percent c format specifier for the care variable when i run the code i'll get the character jet as an output technically speaking a character data is internally stored as an integer rather than the character itself so if we use person d to print a care variable we can see its equivalent integer value so i'll go back to my code editor now i'll print its integer value print f bracket inside quotation i'll use person t because we want to print the integer value now i'll run this code and we can see 122 is printed if we use percentage so when working with character we need to remember just three things one we use single quotation for characters second to print a character we use percent c format specifier and third characters are internally stored as integers before we end this video let's talk about the size of operator which can be used to find the size of data types such as int double float and carry using the size of operator let me give you an example here i have used size of operator to find the size of int and double variable it gives the size in bytes let me run this program as you can see the size of ink is 4 bytes and the size of double is 8 bytes by the way as you see we have mentioned it already here the size of int is 4 bytes and the size of double is 8 bytes and the size of float is 4 bytes similarly the size of cad is 1 byte in this video we have covered 4 commonly used data types there are many other data types in c programming which we will cover as we progress through the course in this video we will learn to take input from users and store it in variables in c programming we use the scanf function to take input from the user the scanf function takes input from the user and store it in a variable similar to printf the concept of format specifier is also used here let's see an example as we have already discussed in our previous video this is the basic outline of every c program so i'll continue on this first i'll declare a variable int is now i will use scanf function to take the input from the user [Music] and finally i'll use printf function to print the variable [Music] now let me explain it in much detail here is an in variable to store the input data and notice the scanf function it has two parts first is the format specifier that represents the type of input data in this case it is an integer and second is the variable name notice that we have used the ampersand before the variable name here ampersand represents the memory address and ampersand is represents the memory address of the ace variable don't worry about the memory address for now we will learn about them in upcoming videos now the scanf function will take the integer input from the user and store it in the address of the ace variable to verify that the input value is stored in the variable i am also printing the variable using printf function now let me run this code here you can see the cursor i'll provide value 22 and press enter as you can see the input value is printed here initially i get confused about where to find the cursor to input a value so i'll print a message to ask the user for the input value using printf statement so printf bracket inside quotation i'll write misses enter input value [Music] now let's run this as you can see i get the message enter input value i'll provide 22 and press enter here you can see the value is printed now this message enter input value make it obvious to input value okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses now that we know how the scanf function works let's use it to take the double and character input i'll use the same program from earlier and i'll remove this part now first i'll create two variables double number and character alphabet then i'll use printf function to print the message for the user enter double input and similarly i'll use scanf function to get the input from the user here you can see i have used percent lf to take the double input now i'll use printf function to show the message again enter character input and i'll use scan a function to take the character input from the user and finally i'll print both the values using printf function now before we run this program i'll add here backslash n so that we get a readable output now let's run this program for a double input i'll enter 22.1 and for character input i'll enter jet as you can see the double and the character values are printed as expected if you are enjoying this video you will definitely love our interactive c programming course on programmies pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box in our earlier program we took double and character input from the user for that we have used to scanf function however we can also take multiple inputs together using a single scanner function let me show you i have the same code from earlier i'll first remove this code now i'll change this text from double input to input values now in the scanf function i'll add percent c after percent lf and i'll add here ampersand alphabet after ampersand number with comma in between here in the scan if percent lf is used for double input and percent c is used for character input now let's run this code i'll provide 30.6 double value and then character value c and then press enter as you can see respective values are printed now that i have shown you many examples of taking double and character inputs from the user i want you to write a program to take integer and float input from the user so go ahead and comment your program it's time for programming squeeze what is the correct way to take double input in this video we will learn to write comments in c programming we'll also see why comments are important comments are hints that we add to our program to make our code easier to understand let's see an example i have this program that brings a variable to make this code a bit more readable i can add comment here like this create a variable [Music] and i'll add another comment here print the value data variable here these lines that start with two forward slashes are comments we add them to make our program easier to understand for everyone let's see what happens when we run this program as you can see the program runs normally without any issue this is because c programming completely ignores the line that start with double forward slashes we can also put the comment and our code in the same line for that i'll cut this and put this in the same line with the code i'll do this for this comment as well now i'll run this code and see what happens you can see that the code before two forward slashes are executed and all comments are ignored we can add any number of comments anywhere in our program however if our code is self-explanatory there is no need to use comments this only makes our code very messy so i'll remove these comments from the program we can also use comments to debug our code it is a common practice to comment out code so that it is ignored rather than removing them completely let me give you an example in this example i have created a program to take a's and height from the user suppose in this program i was not required to take the height input from the user so instead of removing the code related to height what i can do is i can simply add a double forward slash at the beginning of this line so that they become comments so to make them comments i'll just add forward slash here so i'm adding forward slash to every code that is related to height now the codes will be there if we need them in future but they are completely ignored by the c compiler now let me run this code as you can see it says enter the is i'll enter 29 and press enter here the output says is equals to 29 but these lines are completely ignored now later on if we need height again all we need to do is to remove these forward slashes and they will now become statement not comment now let me run this code again i'll enter the is 29 and i'll add your height equals to 11 and you can see the output on the screen a little pro tip for you remember the keyword shortcut to apply comments in most text editors it's ctrl forward slash if you are on windows and command forward slash if you are on mac this will be very helpful in debugging our code in c programming there is another type of comment that allow us to comment on multiple lines at once let me give you an example suppose we want to comment these three lines we can do that by typing forward slash and asterisk at the beginning and asterisk and forward slash at the end so let's do that forward slash and asterisk at the beginning and asterisk and forward slash at the end now let's run this code i'll enter 29 as you can see the code runs without any errors and these comments are ignored i can also use keyboard shortcut to use multi-line comments let me show you first i'll remove this forward slash and asterisk at the beginning and i'll also remove this asterisk and forward slash at the end and i'll select these three lines and press ctrl shift forward slash as you can see these lines are now commented if you are on mac you can press command shift forward slash here instead of ctrl shift forward slash if i press ctrl forward slash only these three lines will be commented at once but they will be commented with single line comments the multiline comments are useful if we need to explain the code a bit more and add more text to understand the code now let's take a moment to discuss why comments are important imagine that you are working on a huge project for a client since you were on a tight schedule you decide to ignore comments you successfully delivered it to the client and clients are happy about it fast forward in a couple of months the client want to update some features of project since you were the one who developed the project the client wants you to make the change without comments you will forget the reason why you wrote a particular piece of code a certain way and you will end up spending a lot of time looking at your own code and trying to understand it now if you had commented the code it would have been easier for you comments are even more important if you are working in a group it makes it easier for other developers to understand and use your code that being said comments are not and should not be used as a substitute to explain poorly written code you should always try to write clean understandable code and then use comment as an addition it's time for programming's quiz what is the correct way to comment in c programming in c programming an operator is a special symbol that is used to perform operations on values and variables we have already used one operator many times in our program without even realizing it's the equal to operator this is an assignment operator that assign the value to a variable for example int is equals to 24 here value 24 is assigned to the ace variable there are many other assignment operators but before we learn about them let's first learn about arithmetic operators arithmetic operators are basic operators like plus and minus that allow us to perform arithmetic operations here is the list of all arithmetic operators available in c programming before we start let me first copy all the arithmetic operators in the code editor now we will learn about these operators next let's start with addition operator the addition operator represented by the plus symbol adds two values or variables let's see an example here is a basic outline of a c program now i'll create a variable say x int x equals to 12 then i'll do something like int result equals x plus 8 and i'll print this using printf function print f bracket inside quotation percenty comma result now let me run this code as you can see 20 is printed so what's happening here is value 8 is added to the value of x which is equals to 12 and they are added by the addition operator and the equal to operator is storing the result in the result variable and then when i print the result i get 12 plus 8 which is equals to 20. instead of storing the result in a variable i can simply print the result for this i'll remove the result variable so inside the print statement i'll add here x plus 8 instead of result and i'll run this and as before we can see 20 is printed on the screen but personally i would like to use the variable it makes the code much more elegant and organized so i'll revert back to my previous code and i'll put your result as before we can also use the addition operator to perform addition of two floating point numbers let's see an example i'll change the in variable to double double and end result to double result and i'll assign a floating point number so i'll make this 12 to 12.57 then we will add x variable with another floating point number say 8.67 since i am adding two floating point numbers the result will also be floating point number so i'll change the data type of result to double using percent lf format specifier and i'll put 0.2 before lf so that we can print the double value up to 2 decimal places now let me run this code now as you can see we get double result 21.24 as an output we can also add a double variable and an in variable using addition operator i'll use the same code and create an in variable y into y equals 8 so i have assigned an end variable with a value 8 to it now in the place of 8.67 i'll use y variable now let's run this code as you can see i get an output 20.57 when we add a floating point number with the whole number the whole number is converted into a floating point number hence we get a floating point number as a result similarly we can perform subtraction and multiplication using minus and asterisk operator respectively go ahead and try it by yourself and leave your code in the comment below in computer programming the division operator works differently when used with integers and floating point number so let's first see how it works for integer i'll create an input variable x and assign a value 12 to this and let us create an int result and assign x divided by 8 and i'll print this using printf function inside quotation percent d comma result here you can see i have used forward slash for the division and notice i have used only the integer values for this now let me run this code as you can see i get 1 as an output however 12 divided by 8 should give 1.5 but we got 1 because the division operator when used with integers gives only the quotient value now to get the exact result we use division operator with floating point number let me change this variable to double type so i'll change into x to double x and result to double the result then i'll make this integer value 12 to floating point value and i'll change this 8 to 8.00 here i have changed the data type of x to double and assign the value 12.00 instead of 12 and i change the value of 8 to 8.00 and since the result will be a floating point number i change the data type of result to double for that i'll do percent lf and i'll put here 0.2 so that we'll get the output up to two decimal places now i'll run this code you can see the expected output 1.50 is printed we know that the division operator gives quotients when used with integer data now if we need to find the remainder after division we use the remainder operator it is also called modulus operator let's use the earlier code you i'll replace this division operator with modulus operator and i'll run this you can see i get 4 as an output because when we divide 12 by 8 the remainder is 4. the remainder operator can only be used with integer data if you use it with floating point numbers you will get an error go ahead and try it yourself okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses so far we have covered some of the basic operations now let's learn about the increment and decrement operator the increment is used to increase the value by one similarly decrement operator is used to decrease the value by one we use the plus sign as an increment operator and minus sign as a decrement operator let's see an example here i have a variable with value 12 now inside the print statement i'll bring the variable with increment operator so i'll add here plus plus as you can see i have used plus plus with x now let's run this code as expected the plus plus sign increases the value of x by 1 and we get 13 as an output similarly we can use the decrement operator to decrease the value by one let's see an example in this same code instead of plus plus i'll use minus minus in front of x and i'll run this again as you can see value is decreased to 11. here i have shown you example of prefix increment and prefix decrement of variable however we can also use it after the variable in a postfix form like x plus plus for increment and x minus minus for decrement they work in a similar manner but there is one small difference between them if you want to learn how they work differently you can visit the tutorial in our website programmies.com the link is in the video description below so far we have been using single operator in an expression however there might be a case where we have to use multiple operators in a single expression let me show you an example so in our previous example i'll change this value of x equals to 12 to 4 divided by 2 plus 6 multiplied by 5 and will minus 1 and we'll print this using printf function now let's run this here the output is 31 this is because the division operator is executed first then the multiplication is executed then the addition is performed between the result of division and multiplication and finally minus operator is executed that means 4 divided by 2 equals to 2 6 multiplied by 5 is 30 and 2 plus 30 equals to 32 minus 1 equals to 31 so that is our output now the main question is why division is executed first well this is due to the concept called precedence and associativity operators with higher precedence are executed first and operators with lower precedence are executed last here the division has higher precedence than addition and subtraction so we'll learn about this in later videos before we move ahead one quick tip for you we should build a habit of using parentheses to make it easier to understand the code for example in my previous code i'll add parentheses like here i'll add parentheses between 4 and 2 and i'll add another parenthesis between 6 and 5. now you can see our code is much more organized than before it's time for programming squeeze what is the value of x in the following code in this video we will learn to convert one data type to another in c we will also learn about different types of type conversion in c programming before we talk about type conversion let's quickly see an example here we have two end variable a and b with value 5 and 9 respectively and we have used plus operator to add these two numbers now let's run this code you can see we get 14 as an output which is 5 plus 9. now let me change one of the data to cat type so here i'll change the data type of a to care care a equals to 5 and i'll put here single quotation around five now we are performing addition between integer and character variable now let me run this program as you can see that the code runs without any error and we get 62 as an output let's see what's happening here we are adding character value 5 and an integer value 9 in this case the compiler automatically converts this character value into ascii value so the character 5 is converted into its ascii value which is 53 so the final output is 53 plus 9 equals to 62. now let's try another example this time i'll change this character data to double and make this 5 into a floating point number and i'll add here point 7 now in this case we are adding double and in type variable now i'll run this code here we get 14 as an output it looks like the double value is converted into in value 5 and then added to 9 which gives us 14 but that's not the case here actually the end value 9 is converted into double value 9.00 then it is added to the double value 5.67 making it 14.67 however the sum is stored in the result variable which is of in type so the double type sum 14.67 is now converted to an n-type value 14. hence we get the output 14. we can verify this by changing the data type of result variable let me show you i'll change this result variable to double so double result equals to a plus b similarly i'll use percent lf instead of percent d and now i'll run this this time you will see 14.67 with multiple zeros after that you might be wondering why the end value is converted into double why not the double converted to int well the type conversion occur according to the data type hierarchy here the data type that is lower in this hierarchy is always converted to the data type that comes higher for example between care and int cad data type is converted to int and between int and double the in data type is converted to double we can say that the lower data type is promoted to higher data type however if you have noticed we have assigned double value to an in variable the double value is converted to the end value this is because during the assignment the data type on the right of the assignment operator is always converted to the data type on the left in this case sometimes the higher data type can also be demoted to lower data type let's see an example i'll create an end variable and a equals to 5.67 and print this using printf function percent d comma a now when i run this you can see that we gave the integer value 5 as an output during the conversion from the higher data type like double to lower data type like ind there might be loss of some data in our program the data 5.67 is converted to 5 there is a loss of data 0.67 during this conversion so we should be careful during this type of conversion here all the type conversion are happening automatically this is called implicit type conversion we can also forcefully convert one data type to another this type of conversion is called explicit type conversion if you are enjoying this video you will definitely love our interactive c programming course on programmies pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box before we learn about explicit type conversion let's get back to our earlier code here we are adding double type variable a with an entire variable b normally we know that the entire variable b will be converted to a double type and then the addition is performed what if we want the double value to be converted to int and then add the end value this is where we use explicit type conversion let me show you i'll add int inside this parenthesis in front of a so here the end inside the bracket before a tells the compiler to convert the variable a to in type now let me run this code as you can see we get 14.00 as an output here this end inside the bracket changes the double value 5.67 to ind and then adds this value 9 to it and then the sum is 14 which is assigned to result variable since the result is double variable 14 is converted to double and gives us output zero 14.00 and multiple other zeros after that since the result is also in i can now change the data type of result into end and don't forget to change the format specifier from percent lf to percent d and then i'll run this again this time we'll get 14 as an output okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses similarly we can also convert an in data type to double by using double keyword inside the bracket before the variable name let me show you here i'll remove this code now i'll create two end variable int a and assign value 9 and create another in variable int b equals to 2. now i'll perform the division between a and b and store the result in a double variable so double result equals a divided by b then this print f bracket inside quotation percent lf comma result now let me run this code here we get 4 as an output not 4.5 this is because we perform division between two integer value the result will be quotient so we get 4 as an output which is the quotient value however we want the actual result 4.5 after the division then we can use the explicit type conversion to convert one of the variable to double and perform division so here i'll add double before the variable a now let's run this now we get 4.5 as output here we have only converted the variable a to double type but still we get the output this is because now the variable a is of double type and if we perform operation between double and int the end data type is implicitly converted to the double type hence b here is also converted to double type now the division between 9.0 and 2.0 is equals to 4.5 it's time for programming squeeze what is the value of result in the following statement just like integer and double boolean is a data type that can store only two values either true or false we use the bull keyword to create boolean type variables however to create a boolean type variable we must import the std bool header file first let's see an example here i have a basic c program now i'll first include include o stdol dot h just like stdio.edge now let's create two boolean variables bull value 1 equals true and another bull value will value 2 equals false now we'll print these variables we know that we use format specifiers to print variables in c however there is no format specifier for multivariable so how do you print a boolean variable well we use the percent d just like printing integer values this is because in c programming boolean values are represented by integer data false is represented by 0 and true is represented by 1 or all the values except 0. now let's print these variables now i'll print this using printf statement using percent d format specifier value 1 and i'll put some space here then i'll print another value 2 using printf statement so here we'll use percent d format specifier and value two and we'll we'll put some space here and i'll run this you can see 1 is printed for true and 0 is printed for false one thing you should remember is that c is a case sensitive language and we cannot use true and false with capital t and capital f let me show you i'll replace this small t with capital t and i'll replace this small f with capital f and i'll run this as you can see i get an error this is because true with small t is different from true with capital t now that we know about the booleans let's learn about comparison and logical operators these operators are used to create boolean expressions that return boolean values let's start with comparison operator i'll first copy a list of all comparison operators in our text editor so that it's easier for us to use them now let's go through them one by one i'll start with greater than operator the greater than operator checks if the value is greater than another value let's see an example here i am using greater than operator to check if 12 is greater than 9 and assign the result to the value variable since 12 is greater than 9 the output will be true so let's run this code as you can see i get 1 as output which represents true now let's change the value of 12 to 5 now i'll run this code again this time we get output 0 which represents false it's because 5 is not greater than 9 so the greater than operator results false similarly the less than operator checks if the value is less than another value here i'll simply change the greater than sign to less than sign the value 5 is less than 9 so the output will be true let's run this code as expected we get the output 1 which is true now let me change the value 5 to 9 and i'll run this code again here i'll get 0 as output because 9 is not less than 9. the equal to operator compares two values and return true if the value are equal i'll use the same code and change the less than sign to equal to sign here 9 is equal to 9 so we should get true as output let's run this code as expected we get 1 as output now let me change this 9 to 6 by just looking at this you can tell the output is going to be false let me run this code as expected we get 0 which means false in c programming similarly there is not equal to operator it does exactly the opposite of what equal to operator does it checks if two values are not equal here i'll change this equal to operator to not equal to here we are using not equal to operator between 9 and 6. now let's run this code i get 1 as output because 9 is not equal to 6 so result is true in c programming there are also some compound comparator operators like greater than equal to this operator gives true if a value is either greater than or equal to another value i'll use the same code and i'll change this value to 9 is greater than and equals to 6. now what do you think the output of this code will be i'll run this and yes you guessed it right the output is 1 because 9 is greater than 6 so the result is true similarly there is less than or equal to operator that does the exact opposite of greater than or equal to let's change the operator here now let's change the operator here i will run this code you can see i get 0 because 9 is neither less than or equal to 6. we can also use this comparator operator with floating point number so i'll change the value 9 to 9.34 and change this 6 to 6.87 now we are checking if nine point three four is less than or equal to six point eight seven i'll run this code as you can see i get the output zero that means the condition is false till now we have used comparison operators to compare two values however we can also use them to compare variables let's see an example i'll create two variables int num 1 equals 9 and another variable int num 2 equals 6. now let's compare these two variables to compare that we'll create bull value bool value equals num1 is greater than num2 here i'm checking num1 is greater than norm2 now let's print this using print statement print f bracket inside quotation percent d comma value now i'll run this code we get output 1 because the value of num1 equals to 9 is greater than value of num2 that is equals to 6. we can also use comparison operators with variables and value i'll remove this num2 and i'll remove this num2 here as well and put here value 6 and i'll run this again as you can see code runs perfectly fine and we get the output as before these are the comparison operators that are used in c programming they all return a boolean value and the expression where they are used is called boolean expressions in our previous example the code num1 greater than 6 is a boolean expression boolean expression check particular condition and results either true or false and based on the result we can perform different actions okay guys we need your support to keep these types of content free for all user youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses now let's move to logical operators logical operators are used with boolean expression to perform logical operations c programming has three logical operators and they are and or and not let's copy them in our code editor and learn them one by one we use the two ampersand sign for the and operator let me show you an example i'll create two variables a's and height with value 18 and 6.3 so let's do that int is equals 18 and double height equals 6.3 now i'll create another variable bull result where i'll assign two boolean expression first to check if a is is greater than or equal to 18 so equals to is is greater than or equals to 18 and second to check if height is greater than 6 so height is greater than 6 and now i'll use and operator to join them so let me complete this program here i'll add print statement printf bracket inside quotation percent d comma result here i have used n between two boolean expressions the operator returns true if both expression are true let me run this code here i get 1 as output because a is equals to 18 so this boolean expression returns true and height is also greater than 6 so this boolean expression returns true as well hence we get the final result that is also true now let me change the value of 18 to 16 so this expression is greater than equals to 18 becomes false now let me run this code as you can see we get output 0 this is because the and operator returns true only if both the expression are true however the value of a's is 16 so this expression is false hence we get the final result 0 that is false in c programming now let's learn about logical r operator we use double pipe symbol as the r operator the r operator returns true if one of the boolean expression is true let me show you here i'll replace this and with r here this expression is false however this expression is true so the result will be true let me run this code as expected we get one as output unlike and and or the not operator is used with only one boolean expression we use the exclamation mark as the not operator the not operator simply reverse the result of boolean expression if the expression is true it returns false and if the expression is false it returns true let's see an example i'll use the same code as before here i'll remove this double variable and this expression height is greater than 6 and i'll use not operator before this expression here this expression is false so the not operator reverses the value hence the final results become true let me run this code as expected we get one as output now let's change the greater than operator to less than or equal to operator here the boolean expression is true so the result will be false because of this not operator now let me run this code as you can see we get 0 as output boolean expression are very useful in decision making and loops they are used to check particular condition and based on the condition we can make some decision suppose you want to check if a person is eligible to vote then we can use boolean expression to check if the a's of the person is greater than 18 and depending upon the result of the boolean expression we can decide if the person can vote or not this is how we can create decision making programs using boolean expression it's time for programming squeeze which of the following code returns false in this video we'll learn about if else statement in c programming we'll learn to create decision making programs that perform one set of tags under a certain condition and another set of tags under different condition in c programming we use the if statement to create programs that can make decision let's start by looking at the syntax of the if statement the if statement starts with if keyword and is followed by the test condition inside the parenthesis this test condition is a boolean expression that results in either true or false if the test condition is true the body of if statement is executed otherwise it is skipped from the execution now that you know the syntax of if statement let's try a working example suppose there is an election going on and to cast the vote your a's must be 18 or more if your a's is 18 or more we'll print you are eligible to vote let's see how we can implement this here i have a basic c program now i'll create a variable to store is int is now i'll ask the user to input there is for that i'll use printf statement and print the message like enter here is then i'll use scan if statement to take the input so percent d comma ampersand is then i'll use if condition to check if a's is greater than or equals to 18. if this condition is true i'll print you are eligible to vote now let me run this program here i'll provide 31 as the is and i'll press enter as you can see you are eligible to vote is printed here the value is 31 so our test condition is greater than or equals to 18 is true that is why this print statement is executed now let me run this program again this time instead of 31 i'll enter 15 this time nothing is printed on the screen this is because the age is 15 so the test condition is is greater than or equals to 18 is false so the body of the if statement is skipped our program is working fine but it is not printing anything when the a's is less than 18. we might want to print something like sorry you are not eligible to vote when ace is less than 18. so i'll add another if statement so i'll say if a's is less than 18 i'll print sorry you are not eligible to vote now i'll run this code and i'll enter 15 again here you can see sorry you are not eligible to vote is printed this is because age is 15 so this condition is is greater than or equals to 18 is false therefore this statement is skipped however this condition is is less than 18 is true so this statement is executed if you are enjoying this video you will definitely love our interactive c programming course on programmies pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box i have this code from the last segment i'll remove this code to get the user input and i'll manually assign the value of is equals to 15 so that it's easier to focus on the logic of the if statement in this program i have used two if statement to perform two different tags we know that the person is eligible to vote only if the a's is greater than or equals to 18 and if the condition is not met we know that the person is not eligible to vote in such cases instead of writing the second if statement with the condition we can use else clause let's first look at the syntax of the if statement with the else clause on your screen you can see how it looks so what happens here is if our test condition is true statement inside the body of if statement is executed and if it is false statements inside the body of ills are executed now getting back to our code to check if the person is eligible to vote or not in this program i'll use the else clause instead of this second if statement so i'll remove the second if statement and replace it with else now let me read this code in plain english as is greater than or equals to 18 then print you are eligible to vote else print sorry you are not eligible to vote now when i run this code you can see sorry you are not eligible to vote is printed this is because ace is 15 which is not greater than or equals to 18 so we get the output sorry you are not eligible to vote okay guys we need your support to keep this type of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score higher so that more people can discover and enjoy these courses the if statement with else clause allow us to make a choice from two different options however sometimes we need to make choice from more than two options in those case we can use elsif's clause with test condition let's see the syntax first the if statement checks the first condition inside the if statement if it is true the body of if is executed and the statement 2 and the statement 3 are skipped however if the first test condition is false the control of the program jumps to the second test condition if this condition is true the body of the else if statement is executed and other statement are skipped if both the test condition 1 and the test condition 2 are false then only the else body is executed if necessary we can add as many else if clauses as we want for our program to work and among all those alternatives only a single block of code is executed now we know how the else if clause works let's get back to our code to check whether a person can vote or not here the is variable stores the a's of the person currently the is of oldest person is 120 so i want this program to consider is greater than 120 as invalid similarly the a's cannot be negative so i also want to write less than 0 as invalid now i want to include these two additional condition in our program for that i'll use else if clause now i'll modify this code so if a's is greater than 1 and 20 i'll print invalid is invalid is and as if so if a's is less than 0 that is negative value then will print is invalid is i'll change this if to elsif so as you can see i have included one if condition two else if condition and finally one else condition here i'll change the value from 15 to 130 now i'll run this code here the a's is greater than 120 so we get invalid is as our output now let's change the value of this is from 130 to minus 4 and let's run this again this time we get invalid is again so this is the condition of else if here is is less than 0 so the output we got is invalid is now we can add as many else if condition as we want here we can see these two condition are performing the same tags printing the invalid is in this case we can combine both the condition together using logical operator here the a's is invalid if it is either greater than 120 or less than zero or let me modify this code so i'll cut this i'll use logical r and put here is less than 0. so i'll change this value from -4 to -1 and when i run this code i get invalid is as output now as long as the a's is an integer number our program works correctly and the extra condition make sure the a's is neither greater than 120 or less than 0 and for any other value of a's the program runs as intended if we provide the is equals to 50 so let's do that i'll provide the value of a is equals to 50 i'll run this now this person is eligible to vote before we end this video one quick tip for you if the body if else block has only one statement like this then we can omit the curly braces of the if block like this as you can see the syntax of the if statement is pretty simple the harder part is the logic behind the test conditions so you will get better at creating test condition with practice also be sure to check our video on the comparison and logical operators that are used to create the test condition the link will be in the video description below we have covered a lot in this video it's time for you to practice what we have learned here is one programming challenge for you to solve can you create a program to check whether a number is positive negative or zero to create this program create a variable named number and assign a double value to it based on the user input then using an if statement check if the number variable is positive negative or zero if the number is positive print the number is positive if the number is negative print the number is negative and if the number is 0 print the number is 0. it's time for programming squeeze what is the output of following code in this video we'll learn about the ternary operator which can be used to replace if else statement in certain situation to make our code look cleaner let's start with syntax of turnit operator on the screen you can see the basic structure of ternary operator the ternary operator starts with the test condition like x is less than 0 or name equals to podma or any other boolean expression that evaluates to true or false if this test condition results in true this first expression before the column is executed and if the test condition is false then the second expression after the colon is executed here you can see there are three operands hence the name ternary operator now you know the syntax of ternary operator let's see an example here you can see the basic c program now i'll create an in variable is an assigned value 15 to this then i'll create a ternary operator with the condition is is greater than or equals to 18 and if this condition is true i'll print you can vote so printf inside quotation i'll put you can vote and if that condition is false i'll print you cannot vote so i'll end this statement with the semicolon now let me run this code as you can see you cannot vote is printed here is equals to 15 so this condition becomes 15 is greater than or equals to 18 which is false so this print statement after the column is printed on the screen now let me change this value from 15 to 24 now this condition becomes 24 is greater than or equal to 18 which is true so this print statement before the column should be executed now let's run this code as expected we get you can vote as output here we have used print statement as expression inside the ternary operator however we can also store value from ternary operator to a variable and use the variable later let's see an example i'll remove this line and create a care variable called operator and i'll i'll assign plus to this then i'll create an integer variable num1 and i'll assign a value a to this and i'll create another in variable end num2 and i'll assign value 7 to this now i'll cut this ternary operator and start with a new condition and i'll check if operator is equals to plus and if this condition is true i'll add num1 with num2 and i'll add colon here and if this condition is false i'll subtract num2 from num1 so num1 minus num2 this expression are returning some values so i'll assign this ternary operator to a variable so say int result and i'll print this variable using printf statement printf inside quotation i'll use percent d format specifier and the name of the variable that is result i'll run this code and you can see we get 15 as output which is the sum of 8 and 7. here the operator is equals to plus so this condition is true and addition is performed between num1 and num2 depending upon your requirement you can either print values directly or assign value to a variable from a ternary operator however if you plan to use values later in the program it's a good idea to store value in a variable in some of the cases we can replace the if else statement with a ternary operator this will make our code cleaner and shorter let me show you an example on your screen you can see a program that checks if you can vote or not using the fl statement on the right and turn it operator on the left both the program are doing the same tags and checking if a person can vote or not and you can see the use of ternary operator has made our code cleaner and shorter in such programs where we need to do a single task inside the if else statement we can use the ternary operator in a place of if else however if there are multiple lines of code inside the if else statement we should never replace that with the ternary operator this will make our code more confusing and messy okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses now to revise what we have learned here is one programming task for you to solve can you create a program to check whether a number is odd or even to create this program create a variable name number and assign a value to it then using a ternary operator check if the number variable is odd or even if the number is odd print the number is odd and if the number is even print the number is even it's time for programming squeeze what is the correct ternary equivalent of the following if else statement in this video we'll learn about the switch statement and how to use it to make decision making programs in c programming here we will also create a calculator using switch statement before we learn about switch statement let's consider a scenario we are given a number between 1 to 7 and based on the number we have to print the day of a week for example if the number is 1 then the day is sunday and if the number is 2 then the day is monday and so on now we can use the if else statement for this however for every number there should be a separate condition this will make our program look messy and difficult to understand for such type of problems where we have to choose from multiple options a better approach will be switch statement let's start with the syntax of the switch statement first the switch statement starts with the switch keyword followed by the variable or expression inside the parenthesis inside the switch statement we can create multiple cases and each cases will have its own value now the value of variable or expression is compared with the value of individual cases if the result of the variable or expression is equals to value 1 then the body of case 1 is executed similarly if the result is value 2 then the body of case 2 is executed and so on however if the result does not match any case then the body of default is executed now let's get back to our original problem of finding the days of the week on the screen you can see the basic c program now i'll create a variable number in number then i'll ask the user to enter number between one to seven so i'll use printf statement and i'll print the message like enter the number between 1 to 7 and store the value to the number variable so scanf site quotation percent d comma ampersand name of the variable that is number then i'll create the switch statement that will print the day of the week as we have already discussed we'll use switch keyword and then inside parentheses we'll put variable or expression in this case we have variable number so i'll put number here then a curly braces inside these curly braces we'll add cases so when the case is won i'll print sunday and i'll use break statement after that i'll talk about this break statement later on the video so let's continue when the case is 2 i'll print monday so monday and break statement so what i will do is i'll copy this and paste this five more times and at the end i'll put default and i'll print invalid number invalid number so invalid number now the case 1 and case 2 is already done so i'll edit from case 3 and case 3 is tuesday similarly case 4 is wednesday and case 5 is thursday so we'll write thursday here then case 6 is friday and finally case 7 is saturday so let's make this as capital so here i have included cases from case 1 to k7 and if the user input 1 then this part of code is executed and sunday will be printed on the screen and if the user input 2 then this part of code will be executed and monday will be printed on the screen however if the user input any number except one to seven then the code inside default will be executed and invalid number is printed on the screen now let me run this program i'll enter five now the input value five matched this case case number five and thursday is printed on the screen now let me run this code again this time i'll enter 6 as expected we get friday as output now once again i'll run this code and this time i'll enter the number that does not lies between 1 to 7 so i'll enter 9 and as you can see i got the output invalid number which is present inside the default here the default case is optional we can remove this if we are sure that the input value matches one of the cases go ahead and remove the default value and try the code here if you have noticed i have used break statement inside individual cases here this break statement exit the switch statement once the matching case is executed if we don't use the break statement all the cases after the matching case will be executed now let me show you i'll remove this break statement from the program so let's remove this now i have removed the break statement from each cases now i'll run this code and i'll enter 5 and it matches the case number 5 and thursday is printed at first then all the cases after that is also executed so we get the output thursday friday saturday invalid number this is why it is important to use break after every cases if you are enjoying this video you will definitely love our interactive c programming course on programmies pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box we learned that using break after every case is important however sometimes there might be situations where we want to execute multiple cases together in such situation we can omit break statement let's see an example as you can see i have pasted some new code on the compiler here we want to check if the day is weekday or a weekend so all the cases from 2 to 6 will print weekday and the case 1 and k7 will print weekend and then the default will be a invalid number now let me run this code i'll enter four year as expected we get weekday and i'll run this again this time i'll enter one and you can see weekend is printed on the screen as you can see we can omit the break statement as per our need next we will use the switch case statement to create a simple calculator okay guys we need your support to keep this type of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses now that we know the working of the switch statement let's create a simple calculator using it here i have copy pasted the code from our github repository so that you don't have to see me typing the whole thing now let's see what's happening in this code here we have created a character variable we then ask the user to choose an operator and store the input in the operator variable then we ask the user to provide two input values and store them in num1 and num to variable then the result variable will store the value after calculation you can see we have used operator variable inside the switch statement and each case statement uses an operator as its value and based on this operator the corresponding operation is performed inside the case now let me run this code here i'll enter plus and i'll enter first number 8 and then second number 12 you can see i get 20 as output what happens here is i provide plus as operator and this plus operator matches with this case hence the addition operation inside the case is executed and the final result is 8 plus 12 that is 20. the break statement then terminates the switch statement now let me run this code again this time i'll enter asterisk and the first number 9 and the second number 5. since i entered asterix the operator match with this case so the multiplication is performed between number 9 and 5 so we get 45 as our output in this way we can also perform subtraction and division you can try it yourself now to revise what we have learned here is the programming tax for you use the switch statement to create a program that will find the month based on the number input here take the input from 1 to 12 and print the corresponding month based on the input value if the number is 1 print january if number is 2 print february and number is 3 then print marge and so on it's time for programming squeeze which of the cases is executed in the following code in this video we'll learn about while loop to repeat a block of code for multiple times we'll also learn about do while loop in c loops are used to repeat a block of code a certain number of times suppose we want to show a particular message hundred times instead of writing the same message hundred times we can write the message once and use the loop to display the message hundred times let's start with the syntax of while loop a while loop starts with the while keyword followed by the condition inside the parenthesis here the condition is a boolean expression that returns either true or false if the condition is true then the statement inside the while loop are executed then the condition is evaluated again and if the condition is true again then the statement inside the while loop is executed this process continues and the while loop is executed on and on until the test condition becomes false if the condition is false then the while loop is terminated and the body of the loop is skipped let's see an example i'll remove this old code and by now you must be familiar with the basic structure of c program so i'll write the basic structure of c program here now you can see the basic structure of c program now i'll create the while loop with the condition 1 is less than 5 and inside the while loop i'll print the message while loop in c so here the condition 1 is less than 5 is true so the statement inside the while loop will be executed then the condition 1 is less than 5 is checked again this time as well the condition is true so the statement inside the loop will be executed again here 1 is always less than 5 so the condition is never false hence the while loop will be executed infinitely until the memory runs out this is called an infinite while loop now let's run this code so you can see the statement while loop in c is printed until the memory runs out and to avoid this infinite while loop we need to make sure that our condition becomes false at some point so i'll create a in variable count and assign value 1 to this now in the condition instead of 1 i'll write here count so the condition becomes count is less than 5. and inside the while loop i'll increase the value of count by 1 so count equals to count plus 1 so this value of count will increase in each iteration now this line of code increases the value of count in each iteration and ultimately the value of count becomes 5 so our condition becomes 5 is less than 5 which is false and it will terminate our loop so let's run this code you can see the message while loop in c is printed 4 times let's see what's happening here initially the value of count is 1 so the condition count is less than 5 is true so the text while loop in c is printed on the screen now the value of count becomes 2 and again the condition is evaluated this time as well the condition is true so again while loop in c is printed on on the screen now the value of count is increased to 3 in this way the value of count keeps increasing in each iteration and after the fourth iteration the value of count becomes 5. this time this condition count less than 5 becomes 5 is less than 5 which is false so the loop terminates and hence the text is printed four times we can also print the count variable to see the subsequent increment of count variable let me show you here inside the loop i'll add print statement and i'll print the value of count so count equals percent d comma count and we need to add here backspace n so that it will be printed on the different lines so let's run this code as you can see the value of count is increasing in each iteration now we know how the while loop works let's do something interesting i'll create a multiplication table using the while loop so i'll go to my code editor as usual you can see the basic structure of c program now i'll create a variable number now i'll ask the user to provide the input so let's use printf statement and i'll enter the message enter the number for the users so and then i'll store the input in the number variable so i'll use scanf statement just like before i'll create account variable so count and i will assign value 1 to this i want to create a multiplication table up to 10 so i'll use while loop so while with a condition count is less than or equals to 10 so inside the while loop i'll multiply the number provided by the user so number is multiplied with the count and i'll assign this to a to an in variable let's say product then i'll print this product variable so using printf statement we'll print this and finally i'll increase the value of count by so one equals to count plus 1. so before i run this i'll add here backslash n so that the output will be more clearer so let's run this program and i want the multiplication of 8 so i'll enter it and you can see the multiplication of 8 is printed on the screen now let's see what's happening here inside loop i'm multiplying the number provided by the user to the value of count then we are printing the value of product and you can see the output on the screen and then the value of count is increased in each iteration initially the value of count is equals to 1 when the number 8 is multiplied with the value of count 1 the output is 8 and in the second iteration the value of count is equals to 2 so 8 into 2 is equals to 16. in this way the number is multiplied by the value up to 10 so we get the multiplication up to 10 here i am printing only the product and to be honest it does not look like multiplication table so i want to print the exact multiplication table like 8 multiplied by 1 is equals to 8 and 8 multiplied by 2 is equals to 16 and so on now i'll modify my code so i'll do percent d into percent d equals to percent that is number into count is equals to product here you can see i am printing all these three variables with proper formatting now let me run this code and i'll enter it again as you can see the output looks better now now you can create multiplication table of any number okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses in c programming there is another version of while loop known as do while loop the working of do i loop is similar to that of while loop with one major difference before we learn more let's start with the syntax of do while loop the do while loop starts with the do keyword followed by the body of the loop and the while keyword with the condition inside the parenthesis and do not forget to put this semicolon at the end here the body of the loop is executed first and then the condition is evaluated if the condition is evaluated to true the body of the loop is executed again this process continues until the condition becomes false and if the condition is false then the loop terminates let's see an example so i'll remove this old code and i'll start with the basic structure of c program as you can see this is the basic structure of c program now i'll create a count variable so count variable and assign value 1 to this then i'll write a do block and i'll print the value of count here so percent d and backspace n and count here and increase the value of count by one account equals to count plus one now i'll use the while condition and i'll check if count is less than five now let's run this code as you can see the value of count is increased in each iteration when you compare a do i look with a while loop you can see in the do while loop the code that is to be executed is kept inside the do block and the condition it checked this is why the code in do while loop is executed once even if the condition is false let me show you here i'll change the value of count from 1 to 5 now the condition is 5 less than 5 which is false so let me run this code here you can see we get 5 as output even after the condition is false from the beginning this is because first the do block is executed which will print the value of count so we get 5 as output then the condition is checked which is false so the loop terminates the do while loop is executed once no matter what this is the major difference between the while and do while loop since the working of do while loop is similar to that of while loop we can use it to print a multiplication table go ahead and print the multiplication table like before using a do while loop now to revise what we have learned here is a programming tags for you can you use the while loop to print the multiplication table for the given number but this time you have to print the number from 10 to 1 so the output would be something like this it's time for programming squeeze which of the following causes an infinite loop hey guys this is me padma from programming and welcome back to this series on c programming in this video we'll learn about for loop to repeat a block of code for a specific number of times in the last video we learned about while loop similar to the while loop we can also use for loop to repeat a block of code for multiple times let's start with the syntax of for loop a for loop starts with the four keyword and three expression inside the parentheses the first expression is called initialization expression it declares and initialize a variable for example int i equals to zero the initialization expression is executed only once next the test expression the test expression is a boolean expression that evaluates to true or false for example i is less than 5. if the test expression is true then the code inside the for loop is executed and then the update expression is executed the update expression changes the value of a variable for example i plus plus or i plus 1. here the value of i is increased by 1 in each iteration now again the test expression is executed if the test expression is true again then the code inside the for loop is executed and the value of i is updated again and the test expression is evaluated for the third time this process continues until the test expression i less than 5 becomes false if the test expression is false then the for loop terminates now you know the working of a for loop let's see an example i'll remove this old code and i'll start with the basic structure of c program here you can see the basic structure of c program suppose i want to print the number from 0 to 9 so i'll use for loop with initialization expression into i equals 0 since i want numbers up to 9 so i'll write the condition i less than 10 and i'll increase the value of i by 1 in each iteration and inside for loop i'll print the value of i so printf percent d sorry percent d comma i and to make our code much more cleaner and understandable i'll put the space here so i'll run this code you can see the numbers from 0 to 9 are printed on the screen let's see what's happening here initially the value of i is equals to 0 so the statement inside the for loop is executed and 0 is printed on the screen now the update expression increases the value of i from 0 to 1 and test expression is evaluated again since 1 is still less than 10 then the test expression is true and the statement inside the for loop is executed and 1 is printed on the screen the update expression again increase the value of i from 1 to 2 the process continues for the value of i up to 9 when the value of i becomes 10 the test expression becomes 10 is less than 10 which is false then the loop terminates and in this way we get the output from 0 to 9. we can also use the for loop to print the message multiple times here instead of printing the value of i i'll print the message so i'll cut this part and i'll write the message like emergency condition so let's run this as you can see the message emergency condition is printed 10 times now suppose i want to print the message 20 times so i'll change the value from 10 to 20 and i'll run this again as you can see it is printed 20 times if you are enjoying this video you will definitely love our interactive c programming course on programmies pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box now let's see a practical example of a for loop in c programming suppose we want to find the sum of all numbers from 1 to 100 then instead of adding the number like 1 plus 2 plus 3 plus 4 and so on we can do is use a for loop let me show you i'll use the same code from earlier first i'll cut this part because we won't need this now i'll continue with the same code now since i want the sum from 1 to 100 so i'll change the initial value of i from 0 to 1 and i'll change this test condition from i is less than 20 to i is less than or equals to 100 then this update expression will update the value by 1 in each iteration now our loop runs 100 times from 1 to 100 to store the sum of all numbers i'll first create a variable sum and i'll assign value 0 to this then inside the for loop i'll add some variable with value of i so sum equals to sum plus i so it becomes sum plus 1 in first iteration and sum plus 2 in second iteration and so on and finally i'll print the value of sum outside the loop so printf percent d sum now i'll run this code as you can see we get 5050 as our output which is the sum of all numbers from 1 to 100 let's see how this program runs initially the value of i is equals to 1 so this condition i less than or equals to 100 becomes true and the statement inside the loop is executed here we are adding sum with the value of i and assigning the value back to sum so the new value of sum is 0 plus 1 that is equals to 1. now the update expression will change the value of i to 2 here the test expression is true again and this time sum is equals to sum plus i that is the value of sum is 1 from the first iteration and the value of i is 2 from the update expression so 1 plus 2 is equals to 3 this time the value of sum is equals to 3. in this way in each iteration the value of i is added to the new value of sum and finally when the value of i becomes 101 the test expression becomes false so the loop terminates and now final value of sum is equals to 5050 which is the sum of all the numbers from 1 to 100. you can see the use of for loop has made our tax much easier and faster if you have to add the number manually one by one it will be so difficult and error prone okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses let's see one more example this time i'll find the sum of only even numbers from 1 to 100 for this all i have to do is change the initial value of i from 1 to 2 and i'll change this update expression by 2 instead of 1 so i'll change this so i equals to i equals to i plus 2 so before i explain what's happening here let me run this code as you can see the output 2550 is printed on the screen let's see what's happening here initially the value of i is equals to 2 which is an even number when the loop execute 2 is added to the value of sum which is 0 so the value of sum is equals to 2. now this update expression increases the value of i by 2 so the new value of i becomes 4 which is also an even since the test condition is true then the expression inside the for loop is executed so the value of i which is equals to 4 is added with the value of sum which is equals to 2 from the first iteration so we get the value of sum equals to 2 plus 4 that is equals to 6 which is also an even number in this way the update expression only updates the value of i to an even number and even value is added to the sum and this way we get the sum of even numbers and we get the final output 2550. now to revise what we have learned here is a programming tax for you can you use the for loop to compute the sum of only odd number from 1 to 100 in each iteration you need to add an odd number to the sum it's time for programming squeeze in the for loop below which is the update statement in this video we'll learn about brick and continue statement to alter the normal flow of loops in c programming the break statement immediately terminates the loop when it is encountered before we move forward let's see the working of loop first here i have used for loop the loop starts from i is equals to 1 to i is equals to 5 and it prints the value of i so let me run this code as you can see we get the output from 1 to 5. now let me use break statement inside the loop after the print statement and i'll run this code this time i only get 1 as output let's see what happened here during the first iteration the value of i is equals to 1 so when the loop runs the value 1 is printed now after the print statement the break statement is encountered and this break statement immediately terminates the loop for more clarity let me add another print statement after this break statement so printf and i'll enter the message after the break so let's run this as you can see we get the same output one this is because when the break statement is encountered the loop exit and no other code of the for loop is executed we generally use the break statement with the decision making statement like if else statement this is why we can break a loop under a certain condition let's see an example i'll use the same code from earlier here i'll remove this break statement and the print statement after that now suppose i want to end the loop when the value of i is equals to 3 so i'll add if statement so if statement with the condition i is equals to 3 and inside this if statement i'll use break now i'll run this code as you can see we get the output 1 and 2. here during the first iteration the value of i is equals to 1 so this if statement is false so we'll go to this print statement and 1 is printed on the screen now the value of i is increased to 2 and this statement is again false because the condition does not meet and again this print statement is executed and 2 is printed on the screen when the value of i is equals to 3 the if condition is true and this break statement is executed and the loop terminates hence we get the output 1 and 2. let's now use the break statement with while loop i'll create a program that will ask input values from the user if the user inputs a positive value it will be printed however if the user input a negative value the while loop is dominated i'll use the same code from earlier i'll remove this for loop i'll create a while loop that is always true and i'll use one as a condition of while loop because we know that one represents true in c programming so this while loop is always true now inside while loop i'll declare a variable number so int number and i'll ask the input from the user and i'll ask the user to enter a number and i'll store it using scanf function since i want to break the loop only if the input value is negative i'll add if statement with the condition number is less than 0 then will break the loop if it is not negative will print the value of the variable so i'll use printf statement so percent d and name of the variable that is number now let me run this code and i'll enter 5. you can see 5 is printed and i'll enter 9 and it is also printed so now let's enter some negative value you can see the loop terminated here you can see as long as we entered positive value the while loop executed repeatedly however when the input is negative then the if statement becomes true and the break statement is executed and it terminates the loop now let's move to the continue statement on like break the continue statement skips the current iteration of the loop and starts the loop with the next iteration let's see an example in this code we have used break statement to terminate the loop when the value of i is equals to 3 now let me replace this break statement with continue statement so continue and i'll run this code now if you look into the output you can see the value 3 is not printed let's see why this happens we know that the loop runs for 5 times from i is equals to 1 to i is equals to 5 and during the first and second iteration the value of i are 1 and 2 respectively so if condition is false for both iteration hence the loop runs normally and 1 and 2 are printed during the third iteration the value of i is equals to 3 this time if condition is true and continuous statement is executed now the continue statement skips the current iteration of the loop and start the next iteration so the program jumps to this update expression and increase the value of i to 4 and again for the value 4 and 5 the if condition is false and loop runs normally and 4 and 5 is printed on the screen in this way for value 3 the continuous statement skips the print statement ok guys we need your support to keep this type of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's keep the engagement score high up so that more people can discover and enjoy these courses we now know the working of break and continue let's use them together in a single loop i'll create a program that takes the input values from the user however i want the user to input positive number only so if the user enters 0 or negative number i'll use the break statement to terminate the loop also to make the program more interesting i'll only print the even numbers so if the user inputs an odd number i'll use the continue statement to skip the loop so that odd numbers are not printed let's start now here the break statement is already implemented that ends the loop if the number is a negative number since we want to exit the loop if the number is either negative or 0 i'll change this less than operator to less than or equal to operator now i want to run the continue statement if the number is odd so i'll add if statement with the condition number modulo 2 is not equal to 0. so inside if we'll add continuous statement here the modulo operator returns a remainder so for a number to be odd it shouldn't have a remainder 0 when it is divided by 2. so let's run this code i'll enter 4. you can see 4 is printed because it is an even number now i'll add 7 and i'll enter i'll enter even number 32 and you can see this 32 is also printed now let's enter a negative value so minus 3 you can see the loop is terminated so as you can see when the input value is positive and odd number this condition becomes true so the continuous statement skips the print statement for the odd input value however when the input values are positive even numbers both the condition becomes false so it is printed when it is a even number when the input value is negative this condition number less than or equals to zero becomes true so the break statement terminates the loop now to revise what we have learned it is a programming tax for you can you write a program that takes an input from the user and print it if the value is a negative odd number however if the input value is positive in the loop with the message positive value and if the input value is negative even skip the value with the message negative even so our output will look like this it's time for programming squeeze which of the following keyword is used to skip the current iteration of a loop in this video we'll learn about functions to divide our program into small blocks of code we'll also learn different types of function available in c programming a function is a group of related statement that perform a specific task they help us divide a large program into smaller chunks so that it is easier to understand and modify them suppose you need to create a program that draws a circle and a rectangle of different colors based on user input instead of creating one big chunk of statement to create this program we can create three functions a function to create a circle function to create a rectangle and a function to color the seed doing this help us divide the complexity of the program and help us focus on the specific part of the program at a time now that you know why we need a function let's see how we can create a function in c programming on your screen you can see the syntax of the function here the return type indicates the data type of the value that is returned by this function function name the function name is the name of the function and this dot inside the curly braces indicates the statement inside the function now let's use this syntax and create a simple function that prints good morning here you can see the basic structure of c program now i'll start with the return type first so here i don't want to return any value from the function so i'll use void as the return type remember that if a function does not return any value use the void as a return type now i will write greet as the name of my function and don't forget to use the parentheses these are needed to denote that this is a function now inside the function i'll print good morning so good morning now let me run this code as you can see nothing happened this is because to run a function we need to call it now to call a function we use function name with the parenthesis so we use the name of the function that is grid with the parenthesis and i'll run this code now this time you see the output good morning let's see how this program works like any other c program the execution start from main and when it encounters the function call the control of the program moves to the function definition now the code inside the function is executed and good morning is printed on the screen once the execution of function is completed the program of the control moves back to the next line after the function call now to verify this we can print the statement after the function call so i'll print after function call and before i run this i'll add here backspace n now let's run this as you can see first the code inside the function is executed now the control move back to the function call so the code after the function call is executed so we get the output good morning and after function call and as discussed earlier one of the good thing about the function is code reusability now once we create a function we can reuse it any number of times all we need to do is call it whenever we need now let me show you i'll remove this print statement and i'll add grit two more times so great and i'll run this so this time you can see good morning is printed three times before we move to the next section here is a quick tip for you it is a good practice to use descriptive names for function so that anyone reading the code will know what the function is doing by just reading the function name here we are greeting good morning so we are using greet as its function functioning we can also use wish morning or any other descriptive functioning if you are enjoying this video you will definitely love our interactive c programming course and program is pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box so far we have created function that just prints message however sometimes we might want to create function that accepts some value and perform some action based on that value for example suppose we want to create a function that accept a number and computes the square of the number and print it we can do this in c programming with the help of function parameters a parameter is a value accepted by the function now let me show you how we can create function with a parameter so let me get back to my previous code i'll remove this grid function and i'll also remove this function call now i'll add another function call it calculate square so calculate square and inside parentheses i'll add function parameter int number now inside the function i'll do int square is equals to number multiplied by number then i'll print the number and it's square like printf bracket inside quotation square of percent d is percent d that is after comma number sorry number comma square now here in the main function i'll call the function calculate square now let me run this code here we get an error this is because when we create a function with a parameter we need to pass the value while calling the function so in the function call i'll add 5 inside the parentheses now i'll run this code again this time you'll see the output the square of 5 is 25 now let me tell you what happened here the calculate square function accepts the parameter so we call the function by passing the value 5. now the parameter number is assigned with the value 5 so when we use number inside the function we are using the value 5. a function can also accept multiple parameters let's see an example here i'll change this function name from calculate square to add numbers inside the parenthesis i'll change this number to int number one and i'll add comma and i'll add int number two now i'll remove these two line inside the function and i'll add int some is equals to number 1 plus number 2 and i'll add print statement after that to print the number and sum so i'll do printf bracket inside quotation sum of percent d and percent d is percent d that is number 1 comma number 2 comma sum now i'll remove this earlier function called calculate square and i'll add add numbers with the value 8 and 9 as its arguments so i'll run this as you can see we get the sum of 8 and 9 that is 17 as output here the value 8 and 9 are assigned to the parameter number 1 and number 2 respectively in programming values passed during the function call are also called function arguments 8 and 9 in this case are the function arguments many people use parameters and arguments interchangeably so don't worry about that however one thing you should remember is that the data type of parameter and argument should be the same in our case we have created in-type parameters so we pass into the values 8 and 9 during the function call if we want our function to accept double or care type parameters we should call the function with double or care values respectively go ahead and write a function that accept double parameters and add them till now we have been using void as the return type of our function this is because our functions are not returning any value in c programming we can also create a function that returns a value suppose in our earlier program instead of printing the sum inside the function we want to return it let's see how we can do that here is the code from our earlier program first i'll remove this void and i'll add int i'm using end because i want to return the sum of two numbers and the data type of sum is in now i'll remove this print statement and i'll add return sum here the return statement that is used to return any value from the function since the function is returning some value this return value need to be stored somewhere so during the function call i'll assign the function call to an n variable say result so int result is equals to this function then i'll print the result using print statement so print f printf result is equals to percent d comma result now let me run this code as you can see we get the result 17 which is 8 plus 9. here the function add numbers add 2 number that is 8 and 9 and it returns the sum the return value now replaces this function call so this statement becomes end result is equals to returned value which is 17 hence we print the result and we get 17 as our output the data type of the return value and the data type of the variable where the return value is stored should be the same the return statement is the last statement of a function when a return statement is encountered the function exits we can verify this by adding print statement after the written statement so i'll add a print statement after the return statement and i'll write here after return statement so i'll run this you can see we get the same output the print statement after the return statement is not executed okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses in c programming a function prototype is a declaration of a function it provides information about the function name parameters and return types however it does not include the body let's see an example here i have the code from our earlier program if you have noticed i have put the code of add number function after the main function now let me add the function prototype for this add number function so i'll add int add numbers parentheses int number 1 comma int number two as you can see the function prototype includes the function name and return type and the parameters inside the parenthesis now let me run this code as you can see the program runs without any issue in our program we are calling the function before defining the function in this type of situation the function prototype provides information about the function to the compiler now during the function call the compiler can now match the function call signature with the function prototype signature if we are defining the function before function call a function prototype is not needed that's why we haven't included any function prototype in our earlier program so far we have been creating our own function these types of function we write the function definition ourselves are called user defined function in c there is another type of function called standard library function they are built in function that are already defined in the library files and we can directly use them in our program for example the printf function the printer function is a standard library function that allow us to print the output the printf function is defined inside the stdio.h header file that's why we need to include the file using include stdio.h similarly the sqrt is another standard library function that allow us to compute the square root of a number let me show you an example here you can see the basic c program first we need to include mat dot etch header file because sqrt is defined inside the map dot edge header file so i'll include math dot edge header file now inside main i'll use sqrt function the sqrt function returns the square root of number as a floating point value so i'll write float result is equals to sqrt and inside sqrt i'll write 25 and i'll end this with semicolon now i'll print this square root square root is percent f comma result now i'll run this code as you can see we get 5 as output which is the square root of 25 in all of our program we have used main if we look into the syntax of main it looks similar to a function this is because main is also a function in c programming it is the first function that is executed by the compiler so every c program should include a main function now to revise what we have learned so far here is a programming tax for you can you create a program that takes two numbers perform the multiplication of two numbers returns the result it's time for programming squeeze what is the correct way to call the following function in this video we'll learn about variable scope more specifically we'll learn about local and global variable scope in c programming a scope is a particular reason in a program and a variable scope determines which variable can be accessed from which region of a program in c programming there are two basic types of variable scopes they are local variable scope and global variable scope let's start with the local scope first to understand the local scope let's see an example of a c function in my code editor you can see function add numbers this function takes two parameters and it stores the sum in the result variable and it prints the result inside the function instead of printing the result inside the add number function let's print it after the function call so i'll paste it here so i have moved this line from the add number function to this main function i will run this code you can see we get an error saying result on declared this is saying the result is appeared for the first time in the line 11. you might be wondering how is that possible even though we have declared the result variable here inside the function well this is where the concept of local variables comes in any variable that is declared inside the function is a local to it meaning the result variable is local to the add number function this reason is called local scope and the result variable has no existence outside this group when the function ends the local variable are destroyed this is why we get an error when we try to use the result variable outside the local scope now if you remember we can solve this problem by returning result from the add number function so i'll add return result and i'll change this void to end since the function now returns a value i will now assign this function to a variable sum and then i'll print the sum so i'll run this code you can see the result is equals to 11 which is 5 plus 6. with the return statement we are not using the local variables directly rather we are returning its value to the function call and assigning it to another variable inside the main function this concept of local variable is a good thing because anyone using this add number function does not need to know what's inside it they just need to know what argument to pass and what value it returns and they can use this function without a problem this makes our code reusable to not only us but also to our fellow programmers another way we can solve our earlier program is using global variables global variables are variables that are declared outside of the function let me show you so i'll use this code from earlier as you already know this code will generate an error so now what i will do is i'll declare an invariable result outside the add number function and then i'll remove this in from here now i'll run this code as you can see this code runs without any error so what happened here is the result variable is declared in global scope this is why we are able to access it from both the function this add number function and the main function it's time for programming squeeze in the volume code which variable is a global variable in the last video we'll learn to create a function this function which we define ourselves i called user defined function in c there is another type of function called standard library function the function definition of standard library function is already provided by c and we can directly use them in our program let's see a simple example to print a text you can see this code in my code editor i'll run this program and you can see hello world is printed on the screen here printf is a standard library function that prints the text on the screen we haven't provided any definition for printf instead we are directly using it in our program in c programming a standard library function is a predefined function which is already defined inside a file and we can directly use them in our program in our case the definition of printf file is present inside the stdio.h header file that's why we have included stdio.edgeheader file inside our program and once the file is included we are able to use the printer function inside our program and also the scanf function that we have used to take the input from the user is also a standard library function and it is also defined inside the stdio.h header file there are various header files available in c programming this file includes different standard library function that we can use directly in our program let's learn about some of the c library files and standard library function present in those files we'll start with the math file the math.h file provides us with various built-in function that helps us to perform mathematical operation easily for example the sqrt function computes the square root of a number all these math based standard library functions are defined inside the math.edge header file so to use them we import the math.ad header file in our program let's now use this math based library function in our program i'll start with the square root function on your screen you can see the basic structure of a c program now first i'll import the math.header files so i include math dot edge header file then i'll create an end variable say num and i'll assign value 25 to this i'll print the square root of this number so i'll write printf bracket inside quotation square root percent lf and comma s q rt inside parenthesis name of our variable num here the sqrt function returns the value in double so i have used percent lf to print the output now let me run this code as you can see we get 5 as output which is the square root of 25. here we don't have to worry about the definition of skrt function we can simply pass a number and get the square root of that number similarly we can use the cbit function to compute the cube root of a number let me show you i'll change the value of 25 to 27 and i'll change this function sqrt to cbrt and then i'll change this text from square root to cube root so i'll run this as you can see i get 3 as output which is the cube root of 27 now let me show you one more example this time i'll calculate the power of a number i'll use the same code from earlier and i'll remove these lines and i'll create two variables so end a and i'll assign value 5 to this in variable a and another variable b and i'll assign 2 to this now i'll use pow function so inside the parenthesis i'll use two variables a and b here this pow function is the power function i'll assign this to a double variable result so double result is equals to pow function and finally i'll print this using printf statement so power percent lf comma result and i'll run this code here we get 25 as our output so what happens here is this first argument a of the power function is the base value and the second argument b is a power raised to the base so it gives 5 raised to the power 2 which is equals to 25 now let me change this value of b from 2 to 3 and i'll run this again and this time you can see we get 125 which is 5 raised to 3. there are various functions defined inside the math.header file you can learn about them on programmies.com website i'll put the link in the video description below now let's learn about different library function available inside the c type dot edge header file this file provides function to perform various operations on characters hence it is known as a character type header file let's see some example so you can see this basic structure of c program here first i'll import c type header file in our program so c type dot h header file now inside the main function i'll create a character variable say alpha and i'll assign a character e to this let's first use a function to convert this character to uppercase so i'll use a function to upper and inside the parentheses i'll use alpha and i'll assign this to a care variable upper so care upper is equals to this function then i'll print this using printf statement so percent c comma upper i will run this code and you can see we get capital e as output now let's change this uppercase value to lowercase so after this print statement i'll write care lower and i'll assign function to lower inside parentheses i'll put here upper then i'll print this lower variable as well so percent c comma lower and then i'll run this code as you can see the capital e is now converted to lowercase e in our example to upper and to lower our standard library function that are used to convert character to uppercase and lowercase respectively okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses now that we know how to use library function in our program let's see why standard library function are so helpful as we saw earlier we can directly use library function in our program we don't have to worry about the function definition and we don't have to write the code ourselves this saves significant amount of time and since they are already tested they work completely fine without any error also they are built in inside the c program so they are optimized for better performance and developers are constantly improving to keep them up to date there are so many library function in c programming if you want to learn about them visit our text based tutorial i'll put the link in the video description below now to revise what we have learned in this video here is the programming tags for you create a program that compute the result of a number raised to the power of the square root of a number so first take the input from user compute the square root of number using sqrt function compute the power of the number raised to the power of its square root and then print the result it's time for programming squeeze what is the correct way to include library function in our program in our past videos we'll learn about function now we will learn to create a different type of function that call itself these are called recursive function in computer programming recursion is a process that allow us to create a function that call itself let me show you what does that mean on your screen you can see we have created a function named recurse and you can also see we are calling the recurse function from inside its body here the function is calling itself this is called a recursive function let me tell you how this works initially the recurse function is called from the main function now the control of the program goes inside the body of the recurse function inside the function body we have included the function call to call the same recurse function now again the program's control move back to the recurse in this way the function is called again and again this might result in infinite function execution to prevent this infinite recursive call we need to provide an if else statement let me show you you can see we have put the recursive call inside if else statement now the function call itself only under certain condition this way we can prevent the infinite recursive call if you are enjoying this video you will definitely love our interactive c programming course and program is pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box now that we know how recursion works let's see an example i'll create a program that computes the sum of n natural numbers instead of typing the program line by line i'll just copy the program so that i can focus more on the actual working in our previous video function we have already discussed about this function prototype so if you want to know more on this you can find the link in the video description as well here we have some function that takes in parameter n here n represents the number up to which we have to find the sum for example if n is 4 we have to find the sum of four numbers 1 2 3 and 4. inside the sum function we have an if else statement that checks if n is equals to 0 if false the function makes a recursive call to itself however for each recursive call we are decreasing the value of n and finally when the value of n becomes 0 the else condition returns n and the recursive call is stopped let's run this code and i'll enter 4 you can see we get the sum is equals to 10 let's see how it works so initially we provide 4 as input value which is stored inside the number variable this value is passed to the sum function during the function call from the main function inside the sum function the value of n is now 4 so the condition is not equal to 0 so this condition becomes true and if statement is executed here the return statement inside if returns n which is 4 plus a recursive call to sum which with a new value 3 which is 4 minus 1. now again the value of n is 3 which is not equals to 0 so this condition is true again so the if statement is executed now if statement returns 3 and a function call with a new value 2. this process continues until the value of n becomes 0 when n becomes 0 the else statement is executed which returns 0. finally we have returned value 4 plus 3 plus 2 plus 1 plus 0 hence we get 10 as output also this image will help you visualize the working more clearly okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the videos so leave a comment below press that like button hit subscribe if you haven't already let's keep the engagement score higher up so that more people can discover and enjoy these courses now to revise what we have learned in this video here is the programming tags for you create a program that computes the factorial of a number here take a input number from the user create a function that takes the number as a parameter inside the function check if the number is greater than zero if true return number multiplied by a recursive call to the function with parameter 1 less than number else return 1. it's time for programming squeeze which of the following statement is false in this video we'll learn about arrays in c so far we have been using single variables to store a single data now we will learn to store multiple data together inside a single variable using an array an array is a collection of similar type of data before we learn about array in c let's first know why we need them suppose you need to store the a's of hundred people one way to solve this is to create hundreds of variables to store the a's of each individual however that will be a lengthy and tedious task instead what we can do is create a single array and store the a's of 100 people together you got that right now let's see how we can declare an array in c programming on your screen you can see the syntax for array declaration here data type means the type of data that will be stored inside the array for example if data type is in all the elements will be of in type arrow name is an identifier that will be used to identify the array and array size specifies the number of elements that can be stored inside the array let's see an example i'll type int because i want to store into the data and i'll call my array is and since i want to store the a's of 5 people i have written 5 inside the square bracket in the c programming we cannot change the size and the data type of an array once it is declared so now the data type of a's will be in and it can only store five values now that we know how to create an array let's learn to store data in it i'll go back to my code editor here you can see the code from earlier i don't need the syntax anymore so i'll just remove this so now let's continue with this code similar to assigning values to a variable we use equal to operator to assign data to an array now i'll assign 5 values 21 29 25 32 and 17 these value inside the curly brackets are called array elements we can also omit the size of the array for that i'll remove this in this case the c compiler automatically determines the size of the array by counting the number of elements inside the curly bracket another thing we can do is assign less number of element than the declared length for example here i'll add 5 inside the square bracket and i'll remove these two elements from the curly bracket you can see the length of array is 5 however we are assigning only three values to this here the remaining two positions are filled with the default value in our case the fourth and fifth position will be zero now we know how to assign value to an array let's learn to access them but first let's learn about the array index in c programming each element of array is associated with a number this number specifies the position of an array element inside an array and is known as r index and array index starts with 0 so the first element of the array is present at index 0. similarly the second element is present at 1 and so on we use the array index to access our element let me show you here we have our array from our earlier program now let me complete this program so hash include stdio.h int into mean and inside the curly bracket return 0 this is the basic structure of c program so i'll cut this and put it here now to access an arrow element we use the array name and the array index inside the square bracket so to access the first element of this array we use is square bracket 0 let me use this and print the first element so percent d comma is square bracket with the index 0 and i'll run this as you can see 21 is printed on the screen similarly we can access second third fourth and fifth element by using array indices 1 2 3 and 4 respectively now what i'll do is i'll copy this and i'll paste it here and i'll change the index value from 0 to 1 2 3 and 4 and i'll run this here you can see all elements are accessed earlier we learned to assign multiple values to an array during the declaration we can also assign values individually using the index number let's see how we can do that on your screen you can see the earlier code here we have assigned all five values during the declaration since i want to assign values individually i'll remove this assignment now i can assign values individually using the index number so to assign the first value i can write is square bracket inside the bracket zero index is equals to 21. similarly i can assign a second value at the index one so is inside the bracket one and i'll assign value 29 to this in this way each value can be assigned to a particular index so i'll continue on this i'll run this code as you can see each value is assigned to the array here this acts as a variable so we can take input from the user and assign to it let's see an example i'll remove these assignments now i'll ask the user for five input value so i'll use print statement and i'll ask the user to enter five input values and i'll use scanf to store the input values so scanf bracket inside quotation percent d comma ampersand is square bracket inside the bracket zero index and scanf percent d comma ampersand is square bracket inside the bracket one here you can see i have used print statements to print the values now i'll run this code and i'll enter 21 29 25 32 and 17. as you can see the input values are now stored inside the array we can also change the value of an array element for this we can simply assign a new value to a particular array index let's see an example i'll use the same code from earlier here i'll remove these print statements now suppose i want to change this third value to 26 from 25 here i can simply assign a new value to the position 2 so i will do is square bracket inside the bracket 2 is equals to 26 and i'll print this so percent d comma is square bracket inside the bracket 2 and i'll run this you can see the value at index 2 is 26 okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses in this example we have used five different scanf statement to take five input values of an array also we have used five different print statement to print values of the adding here instead of writing the same code again and again i can simply use a loop i can use one loop to take the input values and second loop to print the values i'll first add a for loop as we know array index starts from 0 so end i is equals to 0 so we have started our loop from i is equals to 0 and since the array can store 5 elements so i is less than 5 and we have this update expression that increases the value of i in each direction by one now inside the loop i'll add scan if statement and i'll use ampersand is with the square bracket inside the bracket we have i here you can see each input values is stored in a sequential index from 0 to 4. that's why inside the for loop we have used index i along with the a's in the scanf statement in each iteration of the loop the value of i is increased by 1 and input value is stored sequentially now i can remove this message and these scanf statements similarly i can use for loop to print the r elements so for int i is equals to 0 i is less than 5 and now inside the for loop i'll use printf statement percent d comma is with the square bracket inside the bracket i and i'll remove these prints statements i'll run this program and i'll enter 21 11 14 16 and 17. here you can see we get the desired output let me show you something interesting in this code here i'll change the condition from i less than 5 to i less than 6 and i'll run this code and i'll enter 12 13 14 15 and 16. you can see a random number so what happens here well we have created the array to store only five elements now we can access array elements from index 0 to index 4 however our for loop runs from i is equals to 0 to 5 so when the value of i becomes 5 the element is square bracket 5 does not exist as a result we get an unexpected value sometimes we can also get an error due to this so we should never try to access element of an array outside of its bound or length now to revise what we have learned in this video here is a programming tags for you create a program that computes the average marks of a student for this create an array that stores the marks of 5 subject compute the total marks by adding all the marks divide the total marks by total number to the subject and print the average marks it's time for programming squeeze which value will we get when we print norm square bracket 4 from the following array in this video we'll be learning about the multi-dimensional array and we will also learn to write the matrix multiplication program in c programming we learned that an array stores multiple elements together similarly a multi-dimensional array also stores multiple elements the only difference is that the each element of multi-dimensional array is also an array this is why a multi-dimensional array is also known as an array of arrays let's start with the syntax of multi-dimensional array here the array is of in type which means it can only store integer data and arr is the name of the array and 2 and 3 represents the size of the adding here 2 means there will be 2 arrows inside this array and 3 means each array will have 3 elements now all together our array can store 2 into 3 6 elements now let's see how we can initialize a two-dimensional array i'll use this same array now i'll first assign values to this array so this array is equals to curly bracket inside this curly bracket another curly bracket with element 1 comma 3 comma 5 and enclosing this bracket and another comma here and curly bracket here here is 2 comma 4 comma 6. these are the elements of first array and these are the elements of secondary you might think that this assignment looks different than the assigning value to a single dimensional array it's because here we are assigning aries not individual elements we have assigned two arrays and these two arrays has three elements each here this is the example of two-dimensional array and we can think of this as a table with two rows and three columns where rows represents arrays and column represents element inside the array if you are enjoying this video you will definitely love our interactive c programming course on programmies pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box now that you know how to store elements to a multi-dimensional array let's learn to access them remember array indexes well the concept of array indexes is also present in multi-dimensional array however here each element is associated with two indexes let me explain this i'll go back to my code editor here we have our earlier code now to access the first element we use the adder name and indexes 0 and 0. so add a name with indexes 0 and 0. here this first 0 indicates the first element of add array which is this array and the second zero indicates the first element of the first inner array which is one similarly to access the third element of second array we write a r r with index 1 and another index 2. here this 1 represents the second array and this 2 represents the third element of the second array now let's complete this program so i'll start from the basic structure of c program i will now cut this part and i'll paste it here and i'll print this both arrays so i'll use printf statement comma i'll cut this and i'll paste it here and i'll do the same for this add as well will now run this code as you can see we get 1 using this arr 00 and we get 6 using this arr 1 2. to get the idea of indexes in multi-dimensional arrays you can refer to this image we can also change the elements of a multi-dimensional array by using array indexes let's see an example here we have the code from our earlier program i'll remove these two printf statements from here now suppose i want to change this value of 5 to 7 and the value of 4 to 8 here the value 5 is a third element of first array so we can use the index 0 and 2 so we'll write a r r with index 0 and 2 and i'll assign value 7 to this similarly 4 is the second element of second array so we can use index 1 and 1 and i'll assign value 8 to this i will now add print statement here and print these both arrays so percent d comma a r r with square bracket 0 and 2 and print f percent d second array and second element i'll run this code as you can see the value at this position is changed from five to seven and the value of this position is changed from four to eight okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses similar to an array we can also access each element of a multi-dimensional array using a for loop let me show you here in our earlier example i'll remove these lines of code from here now i'll write two for loop for bracket and i is equals to 0 and i is less than 2 plus plus i this is the increment expression and inside this for loop there is another for loop int j is equals to 0 and j is less than 2 sorry j is less than 3 and we have increment expression here the for loop iterates 2 time from i is equals to 0 to i is equals to 1 to access the two inner arrays of two-dimensional array and the inner for loop iterates three times from j is equals to zero to j is equals to two to access the three elements of both the inner array now inside the inner for loop i'll now print the element at index i n j so printf bracket inside quotation percent d comma a r r inside the square bracket i and j i will now run this code as you can see the elements of multi-dimensional array are printed here the outer for loop runs two times from i is equals to 0 to i is equals to 1 and in each iteration of outer loop the inner loop runs 3 times from j is equals to 0 to j is equals to 2. initially the value of i and j is equals to 0 so are i j prints the first element that is 1 now in the second iteration of the inner loop the value of j is increased to 1 and the array i j becomes 0 1 and it prints the second element 3 on the screen in this way the inner loop runs three times to access all three elements of first array after that the condition of inner loop becomes false so the inner loop dominates now the program control moves back to the outer for loop and the value of i becomes 1 and again the inner loop restarts from j is equals to 0 to j is equals to 2. this time arr i j becomes arr10 arr 1 1 and arr 1 2 which gives the first second and third element of secondary to help you visualize the output more clearly let me print a new line after the inner loop so print f i'll break the line here i'll run this program and you can see the output is printed in proper format it's time for programming squeeze which value will we get when we print arr 1 4 from the following array in this video we will be learning about strings in c more specifically we will be learning to use textual data in our code let's first see the example of the hello world program here hello world is an example of a string we have used double quotation to represent strings in programming strings are a collection of characters that are used to represent textual data for example hello world is a string containing a sequence of characters h e l l o w o r l d world now that you know what strings are let's see how we can create them in c here we have created a string type variable str with the value program is if you look carefully you can see the syntax of string looks somehow similar to an array this is because the string in c are an array of characters so this code is similar to this so let's complete this program has include stdio.h int main inside the curly braces return zero i'll cut this and i'll put it here then i'll print this using printf statement so percent s comma str here you can see we have used percent s format specifier to print the string now let me run this code you can see we have successfully printed the string by the way every string in c is terminated by a null character represented by slash 0. this null character helps the compiler to identify the end of the string even in our string programming the compiler adds slash 0 at the end due to this the actual size of the string will be 1 greater than the total number of character in the string we can use the same scanf function to take string input from the user let me show you here i have this code from earlier program i'll remove this part now if you remember this is the syntax of declaring an array i'm declaring an array of characters so i have to provide the size of this array as well i'll add here 20. now i'll add scanf statement to take the input from the user you can see i have used str instead of ampersand str this is because sdr is an array and it already points to the address of the first element of stm hence we can omit ampersand and we will discuss this in our later videos now before the scanf let me print the statement and ask the user for their name so printf i'll enter your name here now i'll run this and i'll enter my name you can see input value is stored on str string now let me run this code again this time i'll enter my full name padma manandar and i'll enter however i'm only getting padma instead of my full name padma manandar this is because the scanf method only takes the input until it encounters white space here there is a space after pogma so the scanf method only takes padma and ignores manandar if you want to take the entire line of string as input we can use f gates function let me modify the program i'll remove this scanf function and i'll add here f gets bracket str comma size size of str comma std i n here f gates function only takes three parameters the first parameter represents the name of the string in our case str is the name of the string and the second parameter is size of the string and this third parameter is stdin which means standard input which denotes that we are taking input from the keyword now let me run this program i'll again type my full name and i'll enter this time you can see my full name is printed okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses we know that a string is a area of characters in c so just like an array we can access each character of a string using indexes let's see an example i'll start with the basic structure of c program so hash include s t d i o dot h int mean and inside this curly braces return j row and here i'll create string with value programming so so care str square bracket is equals to programming now to access the characters we can use string name and the index inside the square bracket so let's access the first character and print it using print statement so i'll use print statement print f percent c comma str and index is zero and i'll run this code you can see p is printed on the screen similarly we can access other elements easily using array indices for that i'll copy this so i'll copy this and paste it here and i'll just change the value of index number here from zero to one two and three so now second third and fourth elements can be accessed using array indices one two and three so now let's run this code as you can see we get the desired characters we can also change the character of a string for this we can simply assign a new character to a particular index let's see an example i'll use the same code from earlier program here i'll remove these print statements now suppose i want to change this third character to capital o i can simply assign value to index 2 so str square bracket will point to the index number 2 and will assign its value capital o now let's change this fifth character to capital r so i'll do the same process here again so str with the index number four that means the fifth element is equals to capital r i'll now print the string using printf statements so print f bracket percent s comma str and i'll run this program as you can see the third character it changed to capital o from small o and the fifth character it changed from small r to capital r to revise what we have learned so far here is a programming tax for you create a program that takes your full name as input and prints your name then change the first letter of your name to x now if your name is joan williams it will be x on williams if your name is julie bing it will be julie being with x as your first character it's time for programming squeeze in this video we'll learn about four most important string function that are used to perform different operation on string let's revise the working of string first so i'll go back to my code editor here i have the basic outline of a c program now i'll create a string care language is equals to c programming let me print this string using printf statement printf bracket percent s comma language as discussed in our last video we use percent s to print the string now i'll run this code as you can see we have successfully printed the string this is the basic understanding of how string works in c if you have some doubts about this i highly recommend you to watch our previous video you can see the link on the screen now let's start with the string function the sdi line function or string length function is used to find the length of a string let's see an example here is the code from our earlier program suppose i want to find the length of this string for that i can use sdrlin function but before that we have to import string.h header file so i'll include here include string dot edge header file this is important because strlin function is a standard library function that is defined inside the string dot h header file now let's use the stanley function i'll use printf statement here and backspace n length percent jet u comma strlin function and inside the function language here percent jet u is the format specifier that we use to print the return value of strlin function now let me run this code here you can see 13 as output the strlin function counts the total number of characters in the string if you are enjoying this video you will definitely love our interactive c programming course on programmers pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box the strcpi function or string copy function is used to copy one string to another let's see an example suppose i have a string name food and i'll assign value pizza to this i want to copy this string to another string named best food so let me first declare the best best food string so best food string and inside the bracket i'll put strlin and bracket food here i have used strlin function so that the length of the best foot string is same as the food string now let me use str's cpy function and i'll copy the string from food to best food so best food comma food the strcpi function takes two value source string from where we want to copy and the destination string and i'll print the best food now so using printf statement percent as comma best food now i'll run this code here you can see the string is copied from the food to best food the strcat function or string concatenation function is used to join two string together let's see an example suppose we have two string care text one is equals to hey and care text 2 is equals to it has value how are you now let's use sti cat function to join this string so str cat bracket text 1 comma text 2. i'll print this using printf statement so percent as comma text 1. now i'll run this code here you can see two strings are joined together however the output does not look nice so let me add here comma and a space and i'll run this again now you can see a clear output that are concatenated together okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the videos so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses the strcmp function or string comparison function is used to compare two string the function returns zero if both the string are equal however if the string are not equal it returns a random non-zero value let's see an example suppose we have two string caret text one and i'll assign value abcd and another string that is text 2 and i'll assign here value ef gh now let's compare these two string using strcmp function so i'll use strcmp function bracket here text 1 comma text 2 and since this function returns an integer i'll assign the return value to an integer so int result is equals to strcmp function now i'll print this using printf statement percent d because this time percent d because this time it gives us integer value and here we'll put result the name of the variable now i'll run this code here we get -4 as output which is a non-zero value because these two strings are not equal now let me change the value of this text to from efg to abcd and i'll run this again this time we get 0 as output because here the both string are equal apart from these four there are dozens of string function that can help us to work with string however remembering them is not possible instead what you can do is google and find the function that you need remember googling things is one of the most important skill in programming now to revise what you have learned in this program here is a programming task for you create a program to compare two string and print the larger string here is how the programs should work get two string input from the user using f gets compare the length of both the string using sdrlin and print the larger string it's time for programming squeeze which of the following function is used to join tostring in this video we'll learn about pointers in c more specifically we'll learn to work directly with computer memory address with the help of pointers pointer is one of the main features that make c programming so powerful it allows us to work directly with the computer memory before we learn about pointers let's first learn about addresses in c programming whenever we declare a variable a space will be allocated in the memory for the variable and c allow us to access the address of the variable we use the ampersand symbol with the variable name to access the memory address let's see an example you might be familiar with the basic structure of c program now i'll create an integer variable is so int is and i'll assign value 25 to this now let's use ampersand symbol to access the address where the is variable is stored so printf percent p comma ampersand is here i have used person p format specified to print the address now let me run this code here you can see we get this output which is the memory address where 25 is located when you copy and run the exact same code you may get different output because the output is based on the location where the variable is stored and it varies device to device if you remember we have used ampersand symbol with scanf like this like ampersand is so usually we use this ampersand is with scanf while taking input from the user this is because with scanf we are instructing the compiler to store the input value at the memory address specified by this ampersand is now that you know about memory addresses let's get back to pointers just like a regular variable a pointer is also a variable however a pointer variable stores the memory addresses of the variable not the actual value let's now see how we can create pointers so i'll create pointer int asterix ptr here ptr is a pointer variable let me create a regular variable and compare these two so i'll create int var when comparing these two you can see the only difference between these two is this asterisk symbol now let's see how we can assign value to a pointer variable here is our code from our earlier program now let me create a pointer variable int astrix ptr as we know a pointer variable stores a memory address of a variable and we also know that ampersand is gives the memory address so when we assign ampersand is to the pointer variable the pointer variable should store the memory address of the a's variable so let's do that now i'll assign this pointer to the address of a's variable and i'll print this using printf statement comma ptr now let me run this code here you can see we get the same result for both ampersand is and ptr we can also access the value of a variable using the pointer let's see an example in our code from our earlier example i'll remove this print statement and i'll modify this print statement here address colon express n now let's print the value using ptr pointer i'll use printf statement printf value colon percent d comma astrix ptr here asterix ptr gives the value stored in the ptr address and since the value is in integer i have used percent d format specifier now let me run this code as you can see we have successfully accessed the value using the pointer variable remember ptr is a pointer that stores the memory address and asterisk ptr gives the value stored in the memory address pointed by this ptr in our last example we saw that we can access value of a variable using pointer variable similarly we can also change the value of a variable using pointer variable let's see an example here we have our code from our earlier program here in this program i'll remove these two print statements now let's use pointer variable to change the value from is 25 to 31 so i'll use pointer and assign value 31. since we know asterisks astrix pti gives the value pointed by the ptr address so this code assigns a new value to the memory address pointed by the ptr variable now let's print the value of the ace variable i'll use printf statement percent d comma is and i'll run this here you can see the value of ace variable is changed to 31. this is because the ptr pointer points to the address of is variable and we have changed the value pointed by ptr pointer from 25 to 31. so ultimately we have changed the value of the a's variable okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score higher so that more people can discover and enjoy these courses while working with pointers you might encounter these two syntax of creating pointers while both are the valid ways to create pointers you should try to avoid the first one because this syntax sometimes creates a lot of confusion let's try to understand this with the help of an example here we have created a pointer that stores the memory address of the a's variable in this syntax this asterisk symbol is attached to ptr so many things that this asterisk ptr is the pointer and try to print the address using asterisk ptr which is wrong this is because asterisk ptr gives the value pointed by the pointer not the memory address let me show you i'll use printf statement here percent d comma asterisk ptr and i'll run this as you can see we get the value of the age variable not the memory address so to avoid this confusion i highly recommend you to create pointer like this now you can visualize ptr separately as pointer and asterisk as the part of syntax to create a pointer now let's revise pointers here we have a regular variable number and pointer ptr then ptr is equals to number is wrong this is because ptr is pointer which can only store memory address but number is not a memory address so so this is invalid asterisk ptr equals to ampersand number is also wrong this is because asterix pti gives the value stored in the ptr location however ampersand number gives the memory address so so this is also invalid ptr equals to ampersand number is a valid code because both pti and ampersand number represent the memory address so this is a valid code and asterix ptr is equals to number this is also valid because both asterisk pti and number represent the value stored in the memory location so this is also a valid code now to revise what we have learned in this program here is a programming tax for you create a program to change the value of a variable using a pointer here is how the programs would work get input value for a double variable salary assign the address of salary to a double pointer now use the pointer to print the value of a salary and increase the salary by two times and print the new salary it's time for programming squeeze which of the following is valid for variable a and pointer p in the last video we learned about pointers in cc we learned about working with memory addresses now we will learn to use pointers to work with arrays an array is the collection of similar type of data you can see the example on the screen here numbers is an array that stores five elements and if we want to access these arrow elements we can use the array index that is to access the first element we use number bracket 0 similarly to access the second and third element we use number bracket 1 and number bracket 2. in this way we can access each element of the added now let's try to access these elements using pointers but first let's try to access the memory address where these array elements are stored so here we have our array of numbers and we are printing the array elements using printf statement inside the for loop now let's print the address of each element using the ampersand operator let's modify this print statement i'll add here percent p and here comma ampersand numbers bracket i i have used percent p format specifier to print the memory address and also i have used here ampersand number square bracket i to print the memory address of the i element now let me run this code as you can see we get the corresponding address of each elements now let's see something interesting for that i'll print the address of array without any array index so i'll use printf statement printf here array address percent p comma numbers now let me run this code here if you look into the output you will notice two things first the difference between two memory addresses is four this is because the array is of integer type and size is of int is four byte second the memory address of the first array element and the address of the array is the same this is because the address of array always points to the first element of the array also here we have used only the name of the array without the ampersand sign this is because in most context array names are by default converted to pointers and we can directly use the name of the array so from now on if i use any name remember it's the pointer similarly we can use number plus 1 to print the address of the second array element and we can use number plus 2 to print the address of third array element now let me show you that so what i'll do is i'll copy this and i'll paste it here and i'll modify this print statement to have a clear output so array address of 1 backspace n and i'll change this to numbers then add a address of three here i'll add here backspace n and i'll change this to numbers plus one and i'll change this to array address of 5 and let's put this as it is now let me run this program as you can see this number gives the address of first element and numbers plus one gives the address of second element and numbers plus two gives the address of third element in this way numbers plus i gives the address of ith element so we can implement this exact logic in a loop so i'll modify this code first i'll remove this print statement and in this print statement i'll modify this ampersand numbers bracket i to number plus i so number plus i now let me run this code as you can see we get the desired output in our last example we saw that for an array numbers numbers is a pointer that gives the array of the first element similarly numbers plus i gives the address of the ith element now we can use this pointer to access the array elements to access the first element we can use asterix number similarly to access the ith element we can use asterisks parenthesis numbers plus i let me show you you can see the code from our earlier program here numbers plus i gives the address of the ith element since i want to use the pointer to access the element i'll remove this number bracket i from here instead i'll write astrix parenthesis numbers plus i so this gives us the ith element now let me run this code as you can see number plus i gives the address of the id element and asterix parenthesis number plus i gives the value present at that number plus i address in this way we are able to access the address and value of an array using pointers okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score higher so that more people can discover and enjoy these courses just like accessing the ad elements we can also change ad elements using pointers all we have to do is assign a new value to the address let me show you here i have the code from our earlier program i'll remove this for loop now suppose i want to change the value of first element to 2 so i can simply write here as tricks numbers is equals to 2. here astex number represent the first element and i have assigned a new value to it also let's change the last value to 11 from 9 so that our array includes only prime number so i'll write here astrix parenthesis numbers plus here 0 1 2 3 4. so numbers plus 4 is equals to 11 here astrix parenthesis numbers plus 4 represents the last element and i have assigned a new value to this address now to verify the value i changed or not let me print the first and last value so i'll use print statement here first element is percent d backspace n comma astrix number and another print statement here it prints the last element here last element colon percent d comma asterisk parenthesis numbers plus 4. now let me run this code as you can see the value of first and last elements are changed now to revise what you have learned in this program here is a programming tags for you create a program to find the largest element of an array first create an array with elements 34 12 21 54 and 48 then assign the first element to the array to a largest variable this time you should use a pointer instead run a loop to iterate through each add element in each iteration of the loop compare the largest variable with each element of the array using a pointer and if the largest element is smaller than the array element assign the an element with the largest variable again using pointers finally print the largest variable it's time for programming squeeze in an array int is bracket 5 what does aces represent in the last few videos we have been learning about pointers in c we'll continue that in this video as well today we will learn to use pointers and function together this is going to be an interesting video let's revise the working of the function with the help of an example here i have created a function that calculates the square of a number the function takes single parameter and it then computes the square of the number and then returns the square here we are passing a regular variable to the function in c we can also pass a pointer variable as an argument to a function let's see an example in which i'll create a function that accepts a pointer as an argument and change the value pointed by the pointer so i'll create a function void find value here this function takes pointer as an argument and inside the function astrix num is equals to 39. here the num parameter is a pointer variable and we have changed the value pointed by this pointer now let's create a number variable inside the main so i'll create here int number and i'll assign value 21 to this and i'll call the function by passing the address of the number so i'll call the function find value bracket ampersand number since our function accepts a pointer variable which stores the memory address we are passing the address of the number variable now let me print the value of the number so i'll use print statement print f inside quotation number percent d comma number let's run this code as you can see the value of the number is changed to 39. this happens because when we pass the address of a number variable the pointer variable inside the function is assigned with the address of the number so when we change the value at the memory address the value of the number is also changed now let's see another example in this example we'll find the square of a number like before but this time we'll use a pointer variable instead of a regular variable here i have the code from our earlier program let's remove this function now i'll create a find square function void find square and this function accepts a pointer variable as its parameter so int asterix number inside the function i'll find the square of the number so int square is equals to asterisk number multiplied with asterisk number this syntax might look confusing for you here asterix number is the value pointed by the number pointer and we are multiplying the value with itself now the square variable stores the square of the number now let me assign this to the value pointed by the number pointer so asterix number is equals to square now inside the main function i'll change this find value to find square so find square i'll also modify this print statement i'll write here square is now i'll run this code as you can see we have computed the square of the number if you are enjoying this video you will definitely love our interactive c programming course on programmis pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box just like regular variable we can also return a pointer variable from a function let me show you an example here we have our code from our earlier program now suppose i want to return pointer from the function not just compute the square so first i'll change the return type of the function from void to integer pointer that is int asterisk now inside the function i'll add a return statement that returns a pointer so return number this statement returns the address pointed by number now to store the return value i'll assign this function call to a variable so int asterix result is equals to this function call since the return value is an address we have created a pointer to store the result now let's use this result variable to print the square value i'll modify this print statement here i'll remove this square is and i'll write here result is percent d and i'll remove this number and i'll write here asterix result now let's run this program as you can see the return address stores the square of the number okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's keep the engagement score higher so that more people can discover and enjoy these courses let's see one more example to make it more clear for you so i'll create a function that adds to number and returns the sum let me go back to my code editor i'll remove these all code from here and i'll remove these statements from here so first i'll create a function that accepts three pointer so i'll create a function here that is int asterisk add number int asterisk num1 comma int asterisk num2 comma int astrix sum and inside the function we will compute the sum of the number pointed by num1 and num2 and store the result to the address pointed by the sum so we'll do asterix sum is equals to asterisk num1 plus asterisk num tube then i'll return the sum so return sum now inside the main function i'll create two variables int number 1 and i'll assign value 32 and another variable int number 2 and i'll assign value 8 into this and a sum variable to store the sum let me call the function by passing the addresses of three variables so i'll call the function add numbers parenthesis ampersand number one comma ampersand number two comma ampersand sum since the function returns the address i'll assign this function call to a pointer so int asterisk result is equals to this function finally i'll print this using printf statement so print f bracket inside quotation sum is percent d comma astrix result now let me run this program as you can see we have computed the sum of two numbers using the function and pointers now to revise what you have learned in this program here is a programming task for you create a program to find the multiplication of two numbers using a function and a pointer here is how your programs would work create a function that assets three pointers inside the function multiply values represented by two pointers and assign the result to the address of the third pointer inside the main function create three variables two variables with the value 13 and nine and third variable to store their product call the function with addresses of the three variables as argument store the return value inside a pointer and print the value pointed by the returned address it's time for programming squeeze what type of value does the following function return in this video we will learn about struct in c more specifically we will learn to store related data together under a single name using struct a struct is a collection of variables under a single name the variables can be of different types suppose we want to store the name and a's of a person for this we can create two variables and store name and a's however since both the data are of same person it would be better to create a single struct and put this data inside it remember struct is often termed as structure in c so don't get confused they are mostly used interchangeably let's now start with the syntax of creating a struct a struct in c starts with the struck keyword followed by the name of the struct the curly braces indicates the body of the struct and the variable inside the struct are called members of the struct when we define a struct we are basically creating a data type just like ind and double in this example the data type is struct person so we can create variables of this struck person type just like creating variables of int and double types now let's create a variable of struck person type here if you look into the syntax you can see the syntax looks similar to creating regular variables where struct person represents data type and this person 1 represents the variable name now we can use these struct variables to access the members of struct now i'll add here person 1 dot is is equals to 25 and person 1 dot salary is equals to 4 321 here i have used dot operator to initialize the struck members is with the value 25 and salary with the value 4321 321.78 for the person1 variable now let's print the value i'll use printf statement here is of person one is percent d n comma person 1 dot is and another print statement that prints the salary of person 1 that is in double so point 2 lf comma person 1 dot salary similarly the same dot operator is used to access the value of struct members is and salary for the person one variable now i'll complete this program here i'll add include stdio.h and add here struct person currently presses double sorry double salary and int is now i'll run this code here you can see we have successfully initialized and accessed the stock members for the person 1 variable in this way we can create as many stock variables as we want and all stock members can use the a's and salary members of the struct let me show you inside the main i'll add here struct person person2 then we can initialize and access the struct members for person to variable as well so i'll initialize person2 variable is is equals to 31. person2.salary is equals to 78943.2 and i'll print this so is of person 2 golden percent d comma person 2 dot is and print f salary of person 2 is percent 0.2 lf comma person 2 dot salary so i'll remove this space from here now let me run this code you can see we have successfully used the struct for person 2 as well if you are enjoying this video you will definitely love our interactive c programming course on programmies pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box in this example we are creating the struct variables in one line and assigning member variable in another line instead we can simply create the struct variables and initialize members in the same line let me show you i'll remove this part of code so i'll remove this part as well i'll remove these two lines of code and so i'll simply write here struct person person 1 is equals to curly braces dot is is equals to 25 comma dot salary is equals to 4321.78 and i'll close this curly braces and another struct person person 2 is equals to curly braces dot is is equals to 31 comma dot salary is equals to 78943.2 and let's close this now let me run this code here you can see we get the desired output also we can create stock variables while defining the struct let me show you in this previous code i'll remove this part of code and here i'll add these struct variables person 1 comma person 2. now let me run this code you can see we get the same output while creating the struct variables we have to use both the struct keyword and the name of the struct which is too long instead we can use an alias the typedef keyword allow us to use an alias for struct let's see an example here we have our code from our earlier example in this case instead of struck person we can use an alias with the help of typedef let me modify this code here i'll add here type def in the struct definition and person at the end now this person can be used as an alias to struct so we can now create a struct variable using person instead of stock person so i'll remove this and i'll write here person now let me run this code as you can see we get the desired output okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score higher so that more people can discover and enjoy these courses let's see one practical example of struct we'll create a program to add two complex numbers we know that a complex number has two part real part and imaginary part so while performing addition between two complex numbers we always add the real part of complex number with the real part of another complex number and the imaginary part of complex number to the imaginary part of second complex number now you know the mechanism of the program let's start writing code first i'll create a struct type def struct complex and curly braces and it has members real and imaginary so double real and double imaginary so i'll just write imagine so this represents the real and imaginary part of complex number and we will add complex at the end of the struct so complex here and semicolon now let's create two complex variable whose sum is to be found so i'll write here complex c1 is equals to curly braces dot real is equals to 21.87 and its imaginary value is imagine is equals to 30 and then we'll close this and another variable complex c2 with value real value is equals to 13.34 and imaginary value dot imagine is equals to 112 point uh suppose this is 112.23 and we'll close this remember we are assigning value while creating the complex variables now let's create another complex variable to store the sum of these two variables so i'll create here complex sum as mentioned earlier let's add the real and the imaginary value of these variables independently and assign the results to the real and the complex part of this sum variable so what i'll do is i'll add here sum dot real is equals to c1 dot real plus c2 dot real and then sum dot imagine is equals to c1 dot imagine plus c2 dot imagine now let's print this i'll use printf statement printf bracket inside quotation result is percent point 2 lf as it is double so plus person dot 2 lf and we'll add here i for the complex number and sum that real comma sum dot imagine here i have formatted the output to make our result look better now let me run this code as you can see we get the sum of two complex numbers now to revise what you have learned in this program here is a programming task for you create a program to find the difference between three complex numbers to perform the subtraction between complex numbers you need to subtract the real part of one complex number from the real part of other complex numbers and the imaginary part of one complex number from other complex numbers it's time for programming squeeze what is the name of the variable of the following struct in this video we'll learn about enums in c programming we'll also learn to create variables of enum types and use them in our program and enum is a type that has a fixed set of values let's see an example here we have used the enum keyword to create the enum named size this enum contains four fixed values small medium large and extra large these are called enum constant now let's create enum variables from the above enum so i'll create enum size suit size your suit size is an enum type variable this variable can now store one of the four values present in the enum size so basically the value of suicides can be of either small so i'll assign this here suicides can be small or this can be medium so medium or this can be lars or it can be extra-large now let's complete this example and run the code but before that let's first quickly learn about integral constant in c all enum constant are internally represented as fixed integer values known as integral constant if we consider our earlier enum the constant small is internally represented as 0 and similarly medium large and extra layers are represented as 1 2 and 3 respectively let's verify this i'll go back to my code editor here you can see the code from our earlier program now i'll complete this program so i'll add here has include stdio.h and here i'll add int main and inside the curly braces return zero and i'll cut this and i'll put it here so yeah i'll now print this using printf statement and here person d comma su size as we know enum constant are integer values internally so i have used this for person d format specifier now i'll run this as you can see we get 3 as an output because internally the value of extra large is three now i'll change the value of suicides from extra large to small and and i'll run this again this time we get the output zero as we saw in our earlier example the default integral values of enum constant starts from 0 so we get value 0 1 2 and 3 for enum constant small medium large and extra large however sometimes it might be necessary to print other values for example the value 0 1 2 and 3 for sizes small medium large and extra large does not make sense instead we might want to print 27 for small 31 for medium 35 for large and 40 for extra extra large in such situation we can change the value of integral constant let me show you how i'll go back to my code editor here you can see the code from our earlier example now i'll assign new value to this enum constant so small is equals to 27 and medium is equals to 31 lars is equals to 35 and extra large is equals to 40. now let's create some more variables as well first i'll cut this line of code and now i'll modify this and write here suit size 1 is equals to small and similarly i'll write enum size so size 2 is equals to medium and for lars and extra layers i'll copy this too and i'll paste it here and i'll change this to size 3 and i'll write here lars and so size 4 is extra large so extra large then i'll print this for that i'll use the same code from our earlier program and i'll copy this and i'll paste it three more times and change this from suicide 2 so size 3 and this is suicide 4 now let me run this code as you can see we get 27 31 35 and 40 for the enum constant small medium large and extra large now we are to at the end of this video but before ending the video let me give you a small tip we have been using this format to create enum variable however we can also create enum variables while defining the enum let me show you here suicides is an enum variable and like before it can only store four values now let me run this code now this time we get the expected output okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the videos so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses now to revise what you have learned in this program here is a programming task for you create an enum with enum constant sunday monday tuesday wednesday thursday friday and saturday inside the main function create two enum variables named weekend one and weekend two assigned values sunday saturday to the variables respectively print the values of the week in 1 and weekend 2 it's time for programming squeeze what is the name of the variable in the following enum in this video we will learn about dynamic memory allocation in c programming more specifically we will learn to allocate and destroy memory addresses while running the c program dynamic memory allocation allow us to allocate memory dynamically that is after we run the program before we learn about dynamic memory allocation let's first revise the concept of pointers here we have created a pointer variable ptr this pointer variable stores the memory addresses of the var variable in the print statement we have used the pointer variable to access the value pointed by it when i run this code i'll get the output 32 we have already discussed pointers in our earlier videos if you are confused how this program works i highly recommend you to watch earlier videos you can find the link somewhere on the screen now let's get back to our video c provides three major functions to perform dynamic memory allocation malloc reallock and free we will now learn about them one by one the malloc function stands for memory allocation is used to reserve a block of memory of the specified size for example suppose we need to allocate memory to store n variables of double type variable then we can use the malloc function now let's see how we can use the malloc function i want to allocate memory for n variables so let me create the n variable int and now let's create a pointer variable to store the address of allocated memory int asterix ptr now let's use the malloc function i'll write here ptr equals to parenthesis int asterisk malloc and into size of int here and into size of int is the memory required to store n number of integer values suppose the value of n is 100 so let's change this to n equals 200 now we know the size of int is usually 4 bytes so total space needed to store 100 double values is 100 into 4 that is 400 bytes now malloc function allocates 400 bytes of memory finally we have used the typecasting so that the allocated memory is used to store integer type data here we have assigned the allocated memory to the ptr variable in this case the ptr variable stores the address of the first byte of the allocated memory and we can use the ptr variable to access the remaining bytes as well once the work of allocated memory is completed we use the free function to release the dynamically allocated memory here is the syntax to use the free function here the ptr is the pointer variable that stores the address of the memory allocated using malloc now that we know how malloc and free works let's use them together in a program but before that let's get some engagement in the video so that we can keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score higher so that more people can discover and enjoy these courses here we have our quote from our earlier program now let's complete this program first so i'll add here has include stdio.h and int main inside the curly braces return zero now i'll cut this and put it there and let's clean this up now it's ready okay so we have our complete code now first i'll change the value of n from hundred to four for simplicity now let's provide four input values to store in the allocated memory so i'll use printf so print f and here i'll write enter input values and i'll use for loop so end i is equals to 0 i is less than n plus plus i and inside the curly braces i'll use scanf function and inside quotation percent d comma ptr plus i here we have allocated spaces for four element and ptr stores the address of the first element so we can treat ptr as an array and ptr plus i gives the ith element of the array which we have already discussed in the pointer and array video now let's print the input values i'll use the print statement first to print the message input values and i'll use for loop again and i is equals to 0 i is less than n and plus plus i inside the for loop i'll print percent d and comma asterix ptr plus i so asterix parenthesis ptr plus i gives the value pointed by the ptr plus i pointer while working with malloc there might be situation where the memory allocation fails in such situation the malloc function returns null so let's create a condition to end the main function if the function cannot memory in our code i'll add if condition if pti equals to null then then we'll print memory allo memory cannot be allocated and return zero here if the malloc function returns null the condition becomes true and this line memory cannot be allocated will be printed and finally return 0 terminates the main function now finally let's use the free function to release the allocated memory so i'll add here free function now let's run this code i'll enter here 12 21 13 and 9. as you can see we have allocated the memory using the malloc function if you are enjoying this video you will definitely love our interactive c programming course and program is pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box in our previous example we have allocated 400 bytes of memory to store 4 of values now suppose we need to store two more integer data in this case we use the real look function to change the size of dynamically allocated memory now let's see the syntax to use really function here six into size of int is the new size allocated using the real look function let's see an example now here i have the earlier code for simplicity i'll remove this section to take input values so i'll remove this and here i'll print the memory addresses directly so here i'll use printf inside quotation allocated memory and i'll use for loop for and inside for and i is equals to zero i is less than n and plus plus i inside the curly braces inside the for loop i'll print the memory addresses so percent p comma ptr plus i now suppose i need two more memory spaces so i'll add a new value of n with value six so i'll add here n is equals to 6 then we will use realloc function to allocate two more spaces so here ptr is equals to reallock function and ptr comma and into size of int finally let's print the memory addresses of new allocated spaces so printf and newly allocated sorry allocated memory backslash n and we'll use for loop again and i is equals to 0 i is less than n and here plus plus i inside for loop i'll print the newly allocated memory addresses so printf percent p comma ptr plus i i'll run this code you can see the output here initially four spaces were allocated using the malloc and then we increased the size from four to six using reallock now to revise what you have learned in this program here is a programming task for you create a program that dynamically change the size of an array create a pointer array like this int asterix aces and then allocate four spaces for the array using the malloc function get four input values for the array print the array element using the pointer resize the array to store six element using reallock assign 32 and 59 as the fifth and sixth element you can see the hint on your screen then finally print all six arrays elements it's time for programming squeeze what is the correct way to allocate memory to store 12 double data in c programming there are usually three steps involved while working with files open a file perform file operation close the file let's first learn to open a file we use the file pointer to work with the file don't worry you don't have to understand all about this pointer just remember this syntax for now now once we create a file pointer we then use the f open function to open the file let me show you an example here i have this test.txt file now let's open this file what i'll do is i'll first create the file pointer file pointer so file astrix fptr now i'll use f open function fptr is equals to f open parenthesis inside quotation test dot txt and another attribute that is r that means read basically opening a file is nothing but connecting our c program to the external file in this case we are connecting our main dot c file to this test.txt file now you can see this r this r here indicates that we are opening the file in read mode so we can only perform the read operation on the file one thing you need to remember is that while opening the file in read mode you need to make sure the file is already present in the specified location otherwise the f open function will return null let me show you now let me first complete this program so i'll add here has include stdio dot edge and int main inside curly braces return zero and i'll cut this two lines of code and i'll paste it there so let's make it more clearer okay so now i'll add an fl statement to check if file open is successful or not so here i'll add if fbti is not equals to null then we'll print file open successful file been successful else will print file open unsuccessful and success now let's run this code as you can see file open is successful because test.txt file is present here now let me change the file to something else let's say test 2 dot txt and i'll save this and i'll run this again as you can see this time we get file open unsuccessful because the file does not exist and f open returns null if you are enjoying this video you will definitely love our interactive c programming course and program is pro the course includes easy to follow tutorials along with quizzes and challenges to practice and test your coding knowledge in real time you will also be creating projects to experience how programming works in the real world and in the end you will receive a certificate which will help you land your first job sign up now by using this qr code or you can also use the link in the description box now that we know how to open a file in read mode let's read its content in c we use the af gate function to read the content from the file let me show you an example here you can see the code from our earlier program now i'll change this test to to test.txt now i'll remove this file open successful print statement from if condition now let's use afkit's function to read the content of this file so first we need to create an array to store the file content so i'll add here care content square bracket 1000 now let's read the content from this file to this array so i'll write here f gets bracket content comma 1000 comma fpti and i'll close this then i'll print it using printf statement so percent f comma content i'll save this here f kids read the content of the file and stores it in the content array and this thousand is the size of the file that is to be read since our array is thousand i have kept a thousand and fbtr is the pointer used to read the file now let's run this code you can see we get the output this is the text file here you can see in this file this is the text file is the first sentence we are only able to get the first sentence from the file because here we have used f kits and f kids can only read the first sentence at a time if we need to read all the lines we can use a loop let me show you so first i'll use this line as the condition of while loop so i'll write here while and put this line inside parenthesis here f gets function reads the content of the file if the file read is successful it returns true otherwise it returns false hence our loop runs until there is some content to be read from the file now inside the loop i'll print the file content so i'll cut this and put it here now i'll save this now let's run this code as you can see we are able to read all the content of the file once a read operation is completed it is a good practice to close the file we use the fclose function to close the file here i'll add fclose function at the end fptr now let's learn to write content to a file but first we need to open our file in write mode this means we need to use w as the second parameter instead of r with the f open function let me show you here this test.txt file already exists but if the file did not exist a new file is created let me show you i'll change this name to new file new file dot txt now this new file will be created i'll save this and i'll run this you can see new file.txt file is now created here now let's add content to this file we use f put function to write content to a file let me show you i'll write here f puts parenthesis inside quotation i'll write i love c programming comma fbtr let me add one more message here i'll add f puts inside parentheses i'll write c programming series by programmies is the best so here comma fbtr and don't forget to close the file so f close fptr now let's run this code now when you open this file you can see the file now includes the message however the message looks a bit odd so in this file i'll add here backslash n and i'll save this and let's run this again now you can see the file looks clean now here remember that initially the file did not exist when we run the program for the first time a new file is created we then added the content to the file using f puts and when we run the program for the second time adding backslash n the initial content of the file is erased and new content is added so whenever we open a file in write mode if the file already exists all content will be erased let me show you that again now i'll change this message from i love c programming to to new misses new misses one and i'll change this to new messes two and i'll save this and i'll run this now you can see the old content is now replaced by new content now to revise what you have learned in this program here is a programming task for you create a new file in write mode and add content c is a fun programming language and i love using c language close the file again open the file in read mode and read the content of the file it's time for programming squeeze in this video we'll learn about preprocessors and macros remember this has include stdio.h we have been using it in all our programs today you will know the exact meaning of this line so let's get started in this line has include is preprocessor directive that includes the stdio.h header file in our program basically a preprocessor preprocess our program before it is being compiled for example this line adds the stdio.h file in our program before the execution of the program there are different types of preprocessor and all of them are represented by the hash symbol at the beginning let's start with the most common preprocessor as mentioned earlier the hash include preprocessor is used to include external header file in our program for example has include stdio.h this code includes the stdio.h file in our program similarly has include math.h this code includes the math.h file in our program once we add the header file in our program we can use all the function present in the file remember printf and scanf these functions were defined inside the stdio.h file we are able to use this function because we have added the stdio.h file in our program similarly we can use math function like sqrt and cbrt to compute the square root and cube root respectively once we include the math.header file in our program let's see an example here is the basic c program since we will always be printing some values i have already added the stdio.h file now let's add the math.h file so here i'll add has include math.h hero file now inside main let's create a variable int number so int number and i'll assign a value 36 to this i'll now use sqrt function to compute the square root of this number so i'll use sqrt bracket number this function returns a double value so i'll assign this to a variable so double square root is equals to square t number now let me print the square root variable i'll use printf statement print f bracket inside quotation percent dot 2 lf comma square root now i'll run this code as you can see we get square root of 36 which is 6. now let's find the cube root using cvrt function i'll change this value of number from 36 to 125 and let me change this skrt to cvrt c cbrt and i'll change this variable name from square root to cube root i'll change the variable name in here as well now let's run this code now this time you can see we get the cube root 5 of this number in c the hash defined preprocessor is used to define macros for example has defined pi 3.1415 here pi is a macro a macro is a piece of code which is given a name in our example we have used pi to indicate the value 3.1415 now whenever we need to add the value in our code we use bi let's see an example now let me define pi macro has define p i 3.1415 now i'll print the value of p i inside the main so i'll use printf statement here print f inside quotation percent dot 4 lf comma vi sorry pi and i'll run this code as you can see we get the value 3.1415 when we print pi let's see one more example suppose i want to compute the area of circle i have already defined pi macro now let me create the radius variable first i'll create radius variable here double radius is equals to 12.4 and i'll now compute the area so here double area is equals to p i into radius into radius here i have used pi macro this will be replaced by the value 3.145 now let me print the area i'll modify this print statement so i'll modify this print statement and i'll put here area i'll run this code you can see we have successfully computed the area of the triangle okay guys we need your support to keep these types of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses as mentioned earlier macros are piece of code that is given some name hence we can also define a complete function as a macro let's see an example we have computed the area of circle instead of computing the area like this we can define a function macro that computes the area and use the code in our program let me show you here after the defined pi i'll write here has define circle area parenthesis are another parenthesis i'll write here p i into r into r this is the macro function it takes the single parameter r that is the radius of the circle and computes the area now let's use this in our code to compute the area so i'll remove this pi into radius into radius from the code and i'll add here circle area and here read yes so let me run this as you can see we get the same output now to revise what you have learned in this program here is a programming task for you create a program to find the square root of a number using the sqrt function and macro together here is how your programs would work include the math.header file define a macro function square root bracket and this macro function should use the sqrt function to find the square root of the parameter n inside the main function use the square root macro to compute the square root it's time for programming squeeze what symbol is used to begin a preprocessor directive comment your answer below see you in the next video happy programming [Music]