#Programming #Beginners #Learn #Programming #Tutorial #Beginners #Edureka
C programming language is known as mother of all programming languages present in the software industry. And even you go for an interview C programming language is the most basic and very important aspect which the recruiters are looking for. Hello and welcome everyone. I am Ravi from Edureka
And today I welcome you all to this tutorial session. On C programming in this tutorial we shall discuss very important and crucial concepts related to C programming. So without wasting much time. Let us quickly get started with our agenda for today’s discussion. We shall begin with a brief introduction of C programming language
Where we will learn about the history and importance of C programming language. After that. We shall learn about the installation procedures to be followed to install C programming in your computer system. And after that we shall discuss the data types in variables, which We use in C programming language followed by
That we shall discuss the most important concept which is C tokens followed by C tokens. We shall discuss the most important Concept in C programming, which is the preprocessor directives without which no program can be executed in C followed by preprocessor directives. We shall learn to execute a first C program
In C. Editor later. We should discuss the control statements used in C programming along with the loops used and followed by that we shall Look into the point is used in C programming followed by pointers may have the functions in C programming and followed by the functions.
We shall discuss the Escape sequences used in C programming and later which will come into the important concept which are the data structures followed by data structures. We shall discuss strings used in C programming and after that we shall discuss structures and unions. And finally we shall come into memory allocations.
Once after we are done with the memory allocations. We shall discuss some important sort. Algorithms which are designed in C programming now without wasting much time. Let us quickly dive into the history of C programming as we all know that Dentistry she is the founder
Of C programming language prior to C programming language. We had many other programming languages such as be coupole as Gall and many more which were usually the Machine level programming languages and it was very difficult to develop Linux operating system using those Machine level programming languages. This was the reason which Dennis Ritchie.
And it C programming language in the year 1972 at Bell Laboratories. USA see was originally implemented on the first machine, which is in DEC pdp-11. See is well known to be flexible and versatile which allows maximum control with minimal number of commands and it is a very user-friendly programming language
And it is highly readable. Now, we shall get into the next stage where we’ll discuss the features of C programming language. The first feature of C programming language is it is a high-level programming language. Any programming language is considered as a high-level language
If it is written in English or use a readable language now, we write programs in C language using English or use readable language, which is user-friendly and easy to understand followed by the first feature. The next one is structured language C language is considered to be a structured programming language
As it improves the clarity and quality. And it reduces the development time for designing a programming software followed by this. The next feature is Rich Library C language has its own Library which includes most of the earth emetic and logical operations, which are predefined. We just have to include the libraries
Which we need and we can execute the functionality of those libraries without having to code them separately followed by that. The next feature is its extensibility the programs which are written in C language are Extensible followed by this. We have the next feature, which is recursion recursion is
The feature offered by C programming language where we don’t have to write the function for multiple times instead wherever we need the function. We just have to call it this feature reduces the time involved in the development cycle and also improves the code functionality followed by this the next important feature offered by
C programming language are the pointers using the pointers we can directly interact with The physical memory of the computer system. The next one is faster execution using C programming language. The users can execute programs faster than its predecessors. And the final feature is memory management, the C programming language offers many functions
Where we can dynamically and directly interact the memory of the computer system many functions such as malloc calloc relock and free are the dynamic memory or locating functions used in C programming language for By this in the next stage is the installation of see in Windows system since I’m using Windows system,
We can learn the installation of see in Windows now see can be installed in computer system in multiple ways. The most basic way of instilling see is installing turbo C in your system. If you are a fan of Eclipse IDE, then you can directly download GCC compiler and add see nature
To the Eclipse IDE and continue to execute your programs compared to the previous two methods the Last method is completely simpler and easy to use you can directly use an online compiler. We can directly write your code and execute it. Now. Let us try to install
Turbo C first to install turbo C in your system. You just have to download Turbo C++ for 7 8 8 .1 and 10. You can either choose a 32-bit version or a 64-bit version since my system is a 64-bit version. I have chose the 64-bit version
And it is lying in my downloads section. So this Is my Turbo C++ executable file? All I need to do is just install it. Select yes. Here you go. You can see the installation wizard here. Just select next and except the agreements of Turbo C++. Next next and you’re done.
Installation will be completed within a moment. And here you go. You have the Finish button and turbo C is installed in your system. And this is the editor. You’ll find photo bossy and here you can write your programs and execute. So this is
How the turbo C++ editor looks like now the next method which I was talking about is Eclipse to use C programming in Eclipse IDE. You need to download an eclipse version for C and C++ developers. Now, you can see the link for Eclipse IDE installer for C and C++ developers.
All you need to do is download this installer and run it and after running it you will find options for various ideas of eclipses. All you need to do is just select C or C++ developers version and you’ll have the eclipse as you can see I have already downloaded the eclipse installer.
All you need to do is to run this installer now and I will be getting a dialog box where I have to choose Eclipse IDE for C C++ developers once after you install the Eclipse IDE The Next Step you want to do is to install the GCC compiler to install the GCC compiler.
We need to visit an official website, which is MI NG w. Here you can either choose the 32-bit version or you can choose a 64-bit version and insulate in your PC. This is the download link. I have already installed Ming W 64-bit in my system. And once after the installation is finished,
He can directly start your Eclipse. Once after the Eclipse IDE is installed your IDE looks something like this once after you start the Eclipse IDE. The first thing you need to do is to set the C++ and C nature you can select. C++ or C IDE Nature by selecting this icon,
Which is on the right top most corner of Eclipse IDE. After selecting that you can see a pop-up where the first option is C or C++. This perspective is been selected here and just select open and that’s how you start working with Eclipse IDE,
And now comes the easiest way to use C programming language in your computer is by choosing an online compiler. This is the official site for using an online compiler where you can write your programs directly onto the text editor now with this we shall move on to a next stage
Where we should discuss see tokens. The see tokens available in C programming language are keywords constant special symbols strings identifiers and operators. Now we shall discuss about each one of them in detail firstly keywords keywords are defined as those variables which have special meaning and a predefined nclibraries.
So this was a definition for keywords keywords are something which are predefined in C libraries, which you cannot rename or you cannot Program for example, if we using the loops like do while loop while loop for Loop then the methods and the functionality of those particular keywords ice predefined.
You cannot access them or you cannot redefine them or reprogram them. So this is the meaning of keywords some of the basic examples for keywords are for if-else main void Etc. Now, let’s move on to the next stage of keywords where we’ll have the complete description of all the keywords.
In C programming language. So these are the complete description of all the keywords which are used in C programming language, which you cannot either access or you cannot change the functionality or you cannot reprogram them. These are all predefined and loaded into C libraries followed by this. We shall discuss constants.
The definition of constants goes like this constants are literals are like variables, but the difference is that the values of constants are fixed once they click they cannot be changed. Changed now. Let’s understand this in detail variables. Are those once after you declare them. You can either change
Their values or you can also perform operations on them. But if you declare the same variables with the keyword const in front of them, then you cannot change them. You cannot change the value which is stored in the variable declared with const keyword the Syntax for declaring constant type variables are const
Followed by data type and after that the variable name or you can also declare a pointer type. Abel name with using const keyword followed by this. The next one is the types of constants available in C programming. The first one is the integer constants followed by that. We have floating-point constants later.
We have character constants. Then we have string constants and finally octal or hexadecimal constants. Next are strings. The definition of strings is defined as follows strings are defined as a collection of characters defined in form of an array and at the end with an character
Which describes the end of the string to the compiler strings are nothing but character type arrays in strings. We have the alphabetical data, which is stored in the form of arrays alphabetical in the Mana a-to-z, not the numbers or special symbols. Once after the string is declared in C language.
It has to end with a null character which is the only special character used in strings which will indicate the compiler that it is the end of that particular string now, we should learn the syntax. For declaring a string in C language the syntax
Is first you need to begin with the data type which is cat followed by that. We have to specify the string name and at the end you have to specify the length you’re using for your string. This will be fixed. This will not be buried in the runtime followed by strings.
We have special symbols special characters are the symbols or single characters or sequence of characters that have a special built-in meaning in the language and typically cannot be used in In identifiers, let’s understand this in a bit detail special symbols are characters such as dollar am percent brackets
Etc are having a special meaning which is predefined nclibraries while designing the C programming language. This is the reason for which they are used in a particular segment of code. For example, if we consider Ampersand the Ampersand symbol is used in printf and scanf statements only this Ampersand symbol
Is dedicated to locate the ads of the You have declared in your program and when you come into modular symbol, this is especially used along with the data types for specifying the data type. For example, if you are declaring an integer type data type, you’ll be using modular symbol along with d
If you are declaring string type data type then you’ll be using the modulus symbol along with s. So this is about the special symbols and the basic examples for special symbols used in C. Programming language are modulus Ampersand and brackets. The next thing is identifiers.
Fires at defined at the names that we declare in the program in order to name a value variable function array and Etc. Let us understand identifiers in a little bit detail identifiers and nothing but the name you declared for your variables and values functions arrays Etc, which you are using in your program.
It’s like naming a person for example, if you want to talk to a person then you call him by his name. So in the same way if you want to access the particular memory location of value or an array or what? About the function you’re using
In your program you call it with the name, which is its identifier for understanding this let us consider a simple example, which is int X is equals to 10 here in is the data type which are specifying for your identifier and X is the variable name
Which will be storing the value 10 followed by this visual also understand some basic rules for declaring identifiers. Some of the basic and important rules used for declaring identify as our first character should always be an Alphabet or an underscore the succeeding characters can either be digits or letters for example,
Special characters are not allowed except underscores and last note is that identify should not be keywords. Let us see the examples to understand these rules. The first type of examples are valid identifiers, which are declared using the rules declared for identifiers in C programming for example integer a
Which does not have any kind of special symbols or numbers in front of it. And the next one is underscore a b which is valid because underscore is the only special symbol which is used here followed by that. We have alphabet in the starting place and after alphabets we have numbers.
So this is also valid and invalid type of identifiers. Are you cannot specify the number at the starting position of an identifier followed by that? You can also not include space in between the name of an identifier if you want to include space or if You want to specify
Space then you can use underscore followed by that. You cannot specify the keyword as your identifier here long is a keyword which happens to be a data type. Once after you have understood the rules for declaring identifiers in a c program. Let us move on to the next concept which is operators.
We have the following operators in C program, which will understand about each one of them in detail. The first one happens to be a thematic operators followed by that we have in Mint or decrement operators and next we have assignment operators followed by that we have relational operators next logical operators
And last but not the least The bitwise Operators. Let us understand each one of them in detail firstly Earth Matic operators. The automatic operators are used to perform mathematical calculations such as addition subtraction multiplication division and modulus. Let us go through a simple program to understand. Functionality of arithmetic operators this particular
Program features all the automatic type of operators that we use NC program. Now, let us try to compile this program and see how does it work. As you can see the code has been successfully compiled and we have the output firstly we have declared three variables here using the integer data type.
The first variable is a which stores the value 9 in it followed by that. We have the next variable B, which stores the value for in it and the third variable is an integer type variable, which is empty now. We’ll use the third variable to store the results
Of the operations performed on A and B, firstly we are going to perform addition. Operation on the first two variables A and B and the value will be stored inside C. Similarly. The next operation would be subtraction and the suppression result will be stored in the value C followed by that.
We have multiplication division and modulus as you can see the program is been successfully compiled and the results are shown as below firstly the addition 13 the subtraction five multiplication 36 and division is to finally the modulus operator. Operation which resulted in a reminder which is 1 now with this.
I hope you have understood the basic functionality of a thematic operators used in C program. Now, let us discuss the next type of operators which are increment operators and decrement operators increment operators and decrement operators are basically used when Loops are included in program. We shall discuss about loops
In a much detail way in the upcoming concepts for now, let us understand that the control statement will be Executed for multiple times when increment or decrement operators are included in the program. I plus plus defines that it is incrementing and I – – defines that it is decrementing.
You can either use any of the variables from A to Z along with the increment symbol, which happens to be the plus + and decrement symbol which happens to be – – with this let us discuss about the next type of operators which are the assignment operators assignment operators
Are used to assign values to the Abel’s in C programming language, for example, if you are declaring certain value to a variable in C program. Then you must use assignment operator. For example, we have entered a is equals to 10 here in is the data type and a is the variable name
So to assign the value 10 or to save the value 10 in the variable a you need to use the assignment operator, which is equals to and on the same way if you’re using double equal to Symbol it means that you’re comparing the values stored on LHS. And rhs, for example,
If you are using a double equals to B, then the compiler understands that if the value of a is equal to equal to B, then it has to execute certain commands in the upcoming statements. Let’s discuss about that in the upcoming concepts for now.
Let us move on to the next type of operators which are relational operators relational operator is a programming language construct or operator. That test or defines the relation between two entities or two variables examples for relational operators are as follows greater than less than equals to and not equals
To let us understand relational operators in a much better way relational operators are generally used between two or more variables. If you had to compare the greatest among the two values for example consider a is equals to 10 and B is equal to 20 now the user Wants to know
Which is the greatest variable then you will apply an if condition and declare two variables inside it as shown here. If a is equal to or less than b then a particular statement or particular code segment will be executed. This condition will be true only
If a is less than or equals to be followed by the relational operators. We shall discuss about logical operators. The Logical operators used in C programming are logical and logical logical or and logical not The Logical operators are used to perform certain operations on the given Expressions first,
We shall discuss logic and to execute logic and both the sides of the LHS and rhs must be true only then it will result in a True Value. If any one of the other value is false, then it will result in a false value similarly
When we come into logical or the logical or functions with either one of the values of the to Agents should be true. For example, if you’re comparing A or B, then either HP true or B should be true. Then the resultant will be automatically to if both the values are false.
Then the resultant will be false. Similarly. When you come into logical not the major functionality of logical not is to convert the True Value into false. If you’re using the not operator in front of a True Value, then the value will be converted into false followed by The Logical operators.
We have the bitwise. Ders The Operators. I used to perform bit operations decimal values are converted into binary values which are the sequence of bits and bitwise operators work on this bits. We have six types of bitwise operators firstly bitwise and bitwise or bitwise not Za left shift
And right shift followed by this. We shall enter into the most important concept which is data types and variables used in C language. The data types used in C language are categorized into four varieties. Peace firstly the basic data types. Next we have the derived data types followed by that.
We have enumeration data types. And lastly. We have void data types, which will discuss about each one of them in detail firstly the basic data types. The basic data types described in C language are NT can float and double and followed by that the derived data types are the array pointer structure
And Union the enumeration data type is none other than the data type, which is Declared using the keyword enum in front of it and lastly we have the data type void. Now we shall discuss the variables used in C programming variable is defined as a reserve memory space
Which stores the value of a definite data type. The value of variable is not constant instead. It allows changes to understand variables in detail variables are something similar to identifiers variables actually store the value which you assign to them. For example, if you declare a variable A with certain data types a float
And you assign some value into it say a hundred then it will store that particular hundred value in a memory location with flow data type. That is the value of hundred will be stored as hundred-pound 0-0 in a certain memory location in the memory allocated to it. Now, let us understand the types
Of variables available in C programming language. The mainly Fighters are variables supported in C programming language. They are local variable. Able variable static variable automatic variable and external variable now, let’s discuss about local variables first any variable that is declared inside a code block or a function
That has scope confined to that particular block of code. A function is called as a local variable. Let us understand this in a detailed way. For example, you are defining a function inside a main program. So inside the function, you will be declaring some kind of variables like A or entropy
So that particular variable is eligible to be used inside only that particular function but not inside the entire code. So this is why it is called as a local variable as its scope is been confined to one particular main program or one particular function not the entire program. For example here.
I have declared a function by named Eddie Rekha and inside I have declared a local variable by the name local Durable, and I have stored certain value that is 10 into it. So I will be able to access this local variable only inside the function at Eureka
But not outside the function at Eureka now, let us discuss about global variables. You can see that the global variable is directly the opposite of local variable. The global variables are actually declared outside the main function itself before even you start coding will be declaring first
The global variables the Reality of global variables is that you can access the global variables in the main program as well as the function or in other way. I can say that the global variables can be accessed anywhere in the program followed by the global variables. We have the static variables.
The static variables are also called as variables, but the only difference is that static variables will never allow you to change the value which they have stored. For example, you can run this code here. I have declared a local variable. Variable 10 and followed by that have also declared
Another static variable with the name static variable and I have assigned the value 10 into it. Now. I can perform some operations such as adding one value to the value stored in local variable the local variable will accept it and execute the code
And the value of the local variable will turn into Levin but when you come into static variable and if you want to try this operation on that particular variable, then the compiler will throw an error saying that the Of the static variable cannot be changed followed by this. We have the automatic variable
The automatic variables can be declared by using the keyword or two by default all the variables which you use in C programming language are automatic variables. For example, you can see this I have a main function inside which I have declared two different variables.
One of them is the integer data type with local variable name which stores the value 10 into it and the other one is also an integer type Variable Auto which stores the value 20 I can either use this auto key word or I can just simply declare a variable with integer data type
And I can type the variable name and declare the value into it. Both are considered to be automatic variables followed by this. We have external variable external variables are declared by using the keyword extern similar to the other one, which is auto variable here will declare the external variables
With the keyword X term we can share a variable in multiple see sauce. Files by using an external variable. For example, I have declared an external variable here of integer data type and the name of the external variable is external and it is storing the value 10 inside it.
Don’t forget to see the keyword which I have used which is external to declare the external type of variable now similar to the rules of declaring identifiers. We have also the same rules for declaring variables. The rules are a variable can have alphabets digits and underscore but not special symbols followed by that.
We have a variable name can start with alphabet and underscore only it cannot start with a digit. The next rule is that there should not be white space in between the variable name as we discussed in the case of identifiers no variable name or no identifier name should include a space
In between them. If you want to specify space then you must use underscore but not the actual space followed by that the last rule is that a variable name must not be Reserve board or keyword example integer of flow which will see some examples for valid type of variables
That we can declare in C language firstly in a where the variable name did not start with any of the special characters or space. It just simply started with an alphabet followed by that. We have another way of declaring variables with underscore, which is the only symbol or the special symbol
Which is eligible to be present in a variable name followed by the I that we have the last type of valid variables name which started with an alphabet and it ended with a digit which is acceptable. Now, we shall also discuss some invalid types of variable names where the first one is
Into here the variable name started with an integer value or a number which is invalid followed by that. We have another variable where we have included an actual space between the variable name. That is we have included an actual space between the variable name A and B, which is Invalid
And lastly we have declared a variable name with a keyword, which is long long happens to be a data type and it is a keyword. So this is also not acceptable followed by this. We shall move into the preprocessor directives which happens to be the most important Concept in C programming language.
The preprocessor directives are lines included in a program that begin with a character hashtag which make them different from the typical source code back. They are invoked. By the compiler to process some programs before compilation. It is the macro processor that is used automatically by the C compiler
To transform your program before actual compilation. Let us understand preprocessor directives in a much better way preprocessor directives are anything which you have written in a sea program followed by an ash keyword for example stdio dot h is a preprocessor directive which means standard input/output this particular preprocessor. Activates the input
And output units to perform a c program operation the code for this preprocessor directives is previously written by the C developers and it is stored in the C library. Now, let us understand how the preprocessor directives work firstly the preprocessor directives are declared in a high-level language, which is users language,
For example, hash include stdio.h before compiling the actual program. The compiler will compile the preprocessor. Actors the preprocessor directives will be in a pure high-level language which will be in a relocatable code. So this relocatable code will be converted by the compiler into assembly level code and will be passed to the assembler.
Now the job of the assembler is to understand the courts which are sent to it and to perform the operation which is expected from it. That is to load the particular library now to load the particular Library. The command will be sent to the loader and Linker in a pure Machine level language.
In the library, which the user is expecting will be loaded the C program and that’s how the C program will be executed with the libraries which he needs and with the input and output units, which he requires now, let us discuss some of the preprocessor directives.
First one is Hash include the hash include preprocessor directive is used to paste code of given file into the current file. It is used to include system defined and user-defined header files. There are two variants to use hash includes directive. The first way is to right hash include
And the file name between the greater than or less than symbols at the next way is to right hash include and include the file name between the double quotes followed by that. We have hash Define the hash Define preprocessor directive is used to Define constant or micro substitution.
It can use any basic data type the Syntax for declaring hash Define is as follows has defined and the token value the next type of preprocessor directive is undef. The hashtag undef preprocessor directive is used to undefined the constant of micro defined by the hash Define in a simple way.
He can undo anything which you have done using the preprocessor directive hash Define. The Syntax for undef is hashtag and deaf and the token which you have defined using hashtag defined. The next preprocessor directive is if deaf the FDF preprocessor directive checks, if the macro is defined by using the keyword has defined
If yes it Cutes the code. Otherwise hashtag else code is executed. If the else code is present the syntax is f deaf macro a code segment and if it is not present then end F or an L stack will be executed. If the code segment inside the else tag
Is present the next preprocessor directive is if and deaf the if and if preprocessor directive checks, if the particular macro is not defined by hashtag defined if yes it executes the current Else the S code will be executed if it is present in the macro the Syntax for if and F is hashtag
If and deaf macro and the code present inside the macro if the condition gets failed, then the code inside and F will be executed followed by that. We have hashtag if the hashtag if preprocessor directive evaluates the expression or condition if the condition is true, it executes the code.
Otherwise else F or else. Or end if code will be executed the Syntax for hashtag. F is hashtag if expression for comparing and if the comparison is true, then the code inside it will be executed else the end of code or the else F or else code will be executed.
The next one is hashtag else preprocessor directive the hashtag else preprocessor directive evaluates the expression or condition. If the first if condition goes false if the first if condition goes first then the Movement directly enter into else part and the code which is declared inside.
The else part will be executed followed by this. We have hashtag error. The hashtag are a preprocessor directive indicates any error, which is present inside the preprocessor directive the compiler gives fatal error message if they hashtag error directive is found and skips further compilation process. The next one is Hash pragma
The hash fragment preprocessor directive is used to provide additional information to the Eller the hash fragment directive is used by the compiler to offer machine or operating system feature the Syntax for hash pragma is Hash pragma followed by the token with this. Let us execute a first C program
As you can see this particular example is our first program that we are going to execute in C language. Now, let us try to run it as you can see the code is been getting compiled and the hello world message is printed successfully with this let us dive into the Statements
That are used in C programming language control statements enable us to specify the flow of program control the specify the order in which the instructions in a program must be executed the make it possible to make decisions or to perform tasks repeatedly
Or jump from one code segment to another let us understand this in a bit detail control statements are actually use a defined conditions for the compiler the user specifies a particular condition in front of a code segment. Moment and ask the compiler to execute that particular code segment.
If and only if the condition which he specified to the compiler is true or not. If the condition is true, then the code will be executed. If the condition is false, then the code will be skimmed and the next code segment will be executed. Sometimes this also enables us to make decisions.
And in the other case we can also perform some particular task for a repeated number of times such as Loops now. You can also jump from one code segment to another directly if the condition happens to be false. This can be used in case of switch. Let us execute all the conditional statements
Present in C program and understand each one of them in a much detailed way firstly we shall understand if condition statement or if control statement the if statement in C is defined as a program in conditional statement that if proved true then performs a particular operation
And displays the information in inside the statement block, for example, if you have a condition in front of a code segment that you want to execute but you have a condition to execute it in that particular case the code segment, which you wanted to execute will be executed only if your condition
What your specified will be true else that particular code segment will be eliminated or the control will not enter into that block of statements to understand this which will go through a flow chart here this particular flow chart. Will Define the working of f control statement firstly the control will enter start
And a particular code segment in the starting point of the program will be executed followed by that the control will directly encounter the if condition this is the place where you will provide your condition if that particular condition happens to be true, then the control will pass through the condition
And will execute the statements which are provided inside the condition if the condition is false then the control bit there. Quickly jump to the next set of statements which are after the if condition statements. Let us try to execute a program to understand this in a much better way.
This particular example is based on if condition Here Am specifying a variable by the name number which is integer data type here. I will be asked by the compiler to enter any particular number which is an integer. So the numbers should not be float or any other
So it should be just an integer. Then the compiler will scan the input whatever I provide to the computer then it will run a particular condition specified in the F block which says if the number provided by the user will result
In a reminder is equal to is equal to 0 then the code segment which is present inside the if condition block that is a print of statement should be executed else. It should directly run out of the if block and execute the next particular statement, which is written 0 now.
Let us let’s try to execute this program and see how does it work. As you can see the code has been compiled here. Now. The program is asking me for an input. Now. Let me try to give an even number which is 10
As you can see the modulus of 10 against to will result in a reminder 0 so the condition which I specified here is true and the code segment inside. It is been printing here now, let us try to provide an odd number and see how does it work.
As you can see the code has been compiled now now let me specify 7 which happens to be an odd number? Let us see if this works or not as you can see the program directly finished and the code block which is present inside.
The f-block did not get executed now followed by this. Let us understand if else condition statements the if-else control statement or conditional statement is similar to if condition but the only difference is that you will also have an else block here. Define it if else statement in C language is defined
As a programming conditional statement that has to statement blocks over a condition. If proved true then the block inside the if will be executed and if false then the else block will be executed to understand this in a much better way, let us go through a flow chart in this particular flow chart.
The control is started from executing the stat statements once after the starting statements are executed. It will encounter a condition In this particular condition with the condition is executed on the result is true. Then the control flow will enter inside the if block statements
And the F block statements will be executed and then it will pass into the last set of statements in case if the condition provided inside the condition block happens to be false, then the condition will be directly entering into the else block of statements
Which will be a separate block of statements from the if statements then Else block of statements will be executed and the control signal comes out of the else block of statement into the next set of statements. Now, let us see practically and understand how does it work.
So this particular example is related to else if statements firstly the compiler will compile the code and the computer will ask me to enter a particular number. So here if the particular number happens to be an even number then the first set of statements which are present in the
If block will be executed if the condition fails then the control will directly jump into the else block of statements and will execute the statements present inside the else block which will say that the number you have inputted is not an even number instead. It is an odd number now,
Let us try to execute the statement and see how does it work. Now you can see that the program has been successfully compiled and the computer is asking me for an input. So first let me provide an even number which is for
As you can see the modulus of for is resulted in zero and the if block statements have been successfully executed and it says the number you have inputted is an even number now, let us try to run this code again and let us provide an odd number this time and let us see
If else block is properly working or not. Then let’s provide an odd number, which is 9. As you can see the compiler have understood that the number is nine and else block of statements are been successfully executed. Let us try to understand the next type of control statements,
Which is the SF ladder else a flat iron C programming language is defined as a programming conditional statement that has multiple elsif statement blocks. If any of the condition is true, then the control will exit the else if ladder and execute the next set of statements as you can see,
This is completely similar to the else. Of control statements, but the only difference is that you have multiple else blocks here. For example, if the first if condition fails then the control should enter the next else statements what if the next else statement is also false, then the control will enter
Into the next else block here you have multiple else blocks. So once the compiled of finds the proper and true else block, then that particular else block will be executed. Let us understand this in a much better way. Using a flow chart as you can see the control will start from executing
The initial steps. And after that it will encounter the first condition. If the first condition is true, then the statements inside. The first condition will be executed. If the first condition is false, then the control will directly enter into the second condition. If the second condition
Happens to be true then the set of statements inside the second condition will be executed else the condition will jump into the third condition or the next following condition. And if the condition is true, then the statements inside that particular condition will be executed. If the last condition is also false,
Then the control will directly exit the else if ladder and execute the next set of statements, let us understand Elsa flat iron a much better way by executing a practical example, as you can see this particular example is based on lsf ladder here.
I will be asked by the computer for inputting a number. So if I input the number and if the particular Bob which I have inputted by chance equals to equals to 10 then the first set of f block statements will be executed
If the number is not in instead if it is equals to 50, then the next else if block will be executed if the number is not equal to 10 and not equals to 50 then if it is equal to 200, then the third set of statements will be executed if the number
Which I have inputted will match none of the above statements then a final else block will be executed. Which will say that you are number is not equal to 10 or 50 or a hundred now, let us try to execute this program and see how does it work.
As you can see the program has been compiled here and the computer is asking me for an input. So to execute the first set of statements. My number should be 10 to execute the second set of statements. My number should be equal to 50 or if I choose
To execute the last set of statements then the number which I input should be equals to a hundred if I don’t want to execute all the above three statements, then I can input any of the random number to execute the last set of statements now, let me try to input the number hundred.
As you can see hundred Mass with the third set of statements and the message which is present in the third block is executed with this. Let us move on to the next type of control statements, which is nested F nested if statement in C language is defined as a programming condition statement
That comprises of another if statement inside the previous if statement in a simple way, it’s actually an if statement which includes an if statement inside it to understand this in a much better. Let us consider a flow chart as you can see this particular flow chart describes the functionality
Of nested F here the starting set of statements will be executed and the first condition will be encountered if the condition happens to be false then a particular set of statements will be executed and the loop terminates in case if the condition provided in the f-block happens to be true,
Then the control will enter inside the if block and it will encounter another condition inside the F block which happens to be another. If statement, so inside another if statement, we have a condition if true then a particular set of statements will be executed if false another set of statements will be executed.
And finally the control will exit the if condition let us execute a practical program to understand this in a much better way as you can see this particular example is based on nested. If condition here, we have two variables of integer data type which are V 1 and V 2 followed by
That we have an if condition The entered V1 value is not equal to V2. Then you should enter the first block of statements if they are equal then the control will enter into the else block of statements now, let us try to execute this program and see how does it work.
As you can see the program has been successfully compiled and the computer is asking me for an input. So let me input to same values second value is also same as the first value and inside the first if condition we have another if condition which Compares both the numbers
If V 1 is less than V 2 then the first block of statements will be executed which is this if it is false, then this particular statement will be executed now, who is this? Let us move to the next type of Those statements which are switch control statements. The switch case
In C language is defined as a programming selection statement that checks for a possible match to the provided condition against the available cases. If none of the case matches that the control will execute the default statement block. This is completely similar to the lcf ladder here. You’ll be having multiple cases.
So the control or the compiler will check for the match between all the available cases and then if any case a smashes then the code segment inside that particular case will be executed. If the input does not match any of the provided cases, then there will be a default case
Or default code segment which will be executed by default. Now, let us try to execute a practical example to understand switch control statement in a much better way, but before that let us check out the flow diagram for switch control statement in this particular flow chart, you can see that the control entered
The switch condition statement and in And the switch condition statement it will be received a value which will be compared against the case is provided in the program. As you can see the value is been compared with the first conditional case here if the condition matches
The value then this particular statements will be executed which are located inside the first case as the control will enter into the next followed case and inside that case if the value is matched then the statements of that particular case will be executed similarly
If that case doesn’t match then the Neil Case will be compared if it finds a match then the statements 3 will be executed. If the value does not match any of the provided cases, then the default statement will be executed which will be provided at the end of the switch control statements.
Now, let us try to execute a program to understand switch control statement in a much better way as you can see this particular example is based on switch control statement in this particular program. The compiler will ask me to enter any of the automatic operators. For example People I have selected
Plus minus into and / so once after I enter the operator the program will ask me to select any two operands that means I need to provide two different numbers. So once after it gets the operands and operator, it will perform the operation which I requested to it.
So first if I had provided plus operation, then this particular code segment which happens to be the case plus will be executed if I provide the – operation then this particular segment of code, which works with – will be executed similarly into and / so if I provide the operation
Which does not match any of the above cases, then a default case will be executed now, let us execute this program and see how does it work. As you can see the program has been successfully executed and the compiler is asking me for an input. Now. Let me provide –
Operator the program will ask me for two different operands. Let me provide the first operand as 12 and the next operant as 5 now the case II which happens to be the – is been selected and the code segment inside that will be executed. So according to the result.
We have 12 minus 5 is equal to 7, so that’s how the switch. If statement works now, let us try to understand the next type of control statement, which is the ternary control statement. Their energy operator in C language is defined as a programming conditional statement that is similar to an if statement
But shorter in code length the control checks for a condition and executes either of the two statements. So according to the definition it is actually an if statement but shorter in code length in if condition you actually provide an F. Addition along with a condition specified inside the
If brackets if the condition happens to be true, then you will have to write another code for the particular statements you want to execute that’s the reason the cord length for if block is increased in size. But if you want to coat the same thing using a ternary operator
Your code length will be decreased to one line where you will provide the condition along with the possible outcomes. If the condition is true, then the first outcome will be executed else. The second outcome will be executed for understand ternary control statement in a much better way.
Let us go through this flow chart as you can see the control statement will first encounter the condition if the condition is true, then a particular code segment will be executed else. They are the code segment will be executed to understand this let us execute a program
As you can see this particular example is based on ternary operator. So this particular line is Nick Ranieri operator here. We have Max’s equals to A is greater than b if it is true, then a should be greater else B should be greater. So now let’s execute this program and see the result.
As you can see the program has been compiled successfully and it is asking me for two different numbers one for a and one for B. Now. Let me enter a smaller value for a that is 9 and a bigger value for B, which is 10.
As you can see 10 is the largest number and a is not greater than b so B is been displayed as the greatest number amongst the to wear this let us move on to the next control statement, which is brake control statement. The brake control statement in C language is defined
As a programming conditional statement that is designed to exit the control from current code segment to the next code segment when a specified condition is satisfied. We have checked the brake condition statement in switch control statement each and every case. Switch control statement will include a break which specifies
That if the case has matched then the control flow should exit the switch case and execute the next particular statements with this leaders. Also check the flow chart for brake control statement. You can see that the control has enter the condition if the condition is true, then the control will be break else.
The condition is repeated and executed with this. Let us move on to our next topic which is loops. Loop control statements in C. I used to perform looping operations until a given condition is true control comes out of the loop statements once condition becomes false.
There are three different types of Loops in C language, which our dowhile Loop while loop and for Loop let us understand Loops in a much better way looks are actually similar to conditional statements in C programming language. If you had to execute a particular code segment for a repeated number
Of times say five times then you can actually Specify that to the compiler. You can just write a for Loop or a while loop or a do while loop along with the condition stating that you have to execute this particular code segment four five times. Then the compiler will automatically understand it
And will execute the same code segment four five times. Let us understand each one of these Loops in a much better way along with flowcharts and examples firstly we shall deal with follow follow can be defined as a precise Loop which has initialization condition. Ian and increment or decrement operators,
So firstly initialization initialization process is where you declare an initialization variable and you will assign it to a particular number. Usually the initialization value is 0 followed by that. We have a condition. This is where you specify to the compiler for a particular number of times or iterations according to the previous example,
If we wanted to execute a particular code segment four five times, then you have to specify if initialization variable. Is less than or equal to 5 then execute this particular segment for the next time once after that particular code segment is executed. The very next step. It takes is increment or decrement.
This is where the compiler will check if I have reached a limit or not. If the limit is reached then the control will exit the loop. If not, then it will execute the loop. Now, let us see a flow chart to understand this in a much better way
As you can see the control begins at initialization and later the condition. Is checked if the condition is true, the statement will be executed and after that the increment or decrement process takes place once after the increment or decrement process is done the control will again check the condition.
If the condition is true, then the control will enter the loop again if the condition is false, then it will exit the loop. Now, let us execute a program to understand this in a much better way as you can see this particular example is based on four Loop here.
I am trying to print five different values which One two five. So this particular Loop will be executed five times. So this particular value is the initialization value and this particular code segment is the condition where has specified if the initialization value is less than or equal to 5,
Then the code must be executed. And finally, I have the increment operator which increments. Once the first Loop execution takes place. Once after the first Loop execution finishes, the incrementation value will be increased from 1 to 2. And the condition is again check and according to the condition.
If it is true the loop will be executed. If first the condition will be exited. Once after the limit will be crossed to 5, then the loop will exit else it will continue executing the statements present inside that Loop now, let us try to execute this Loop and see how does it work.
As you can see the code has been successfully compiled and the result has been provided here as you can see the control flow has encountered the furlough first and Initialization value was 1 and the condition was true and the statement is been executed. Once after this Loop finishes the first iteration,
The initialization value will be incremented to 1 as the increment operator here is been executed. Now once after it changes to to the condition is again checked and the condition is true the loop will be executed similarly. It will execute for three and four and finally once
After the increment value reaches to Value 5. The condition is checked again, and the condition is true. Ooh, so the final number will be displayed here. And once after the increment goes to 6, then the condition fails then the control will exit the loop and the next set of statement will be executed.
So that’s how the for Loop works. Now. Let us move on to the next type of for Loop which is Advanced follow-up or nested for loop as you can see. This is also basically a for Loop, but the only difference is that this includes a for Loop inside the external
For Loop to understand this let us go through this flow diagram. As you can see the control flow starts to execute the beginning statements. And finally it encounters the first for Loop condition. If the condition is true, then it will enter the inner for Loop once
After the control enters the inner for Loop the inner for Loop will be executed until the condition of the enough volume is true. Once the condition inside the inner for Loop fails. Then the control will be passed to the increment or decrement variable of the external for Loop here is
Where the counter variable will be. Dated and the condition of the outer for Loop is again check if it is true. Then the inner for Loop will be executed again, the enough all will be executed until the inner condition is true. Once after the inner condition fails again,
It reaches the outer increment decrement operators. So the gone two variables will be updated. This cycle will be executed until the condition of the external for Loop is true. Once after the external follow fails, then the statements outside the external following will be executed. Or the control will exit both the follow-ups.
Now, let us try to execute a program to understand this in a much better way. So this particular example is based on nested for Loop so you can see that there is an external for Loop here. So in this particular for Loop, you have a condition and until this condition is true.
The internal for Loop will be executed now, let us try to execute this program and see how does it work as you can see the program has been successfully compiled and the data has been displayed here. As long as the first for Loop is true. The internal for Loop is executed
And the data is printed now, let us try to understand the next type of loop which is the while loop the while loop can be defined as a loop which executes itself repeatedly until a given Boolean expression or a condition is true. Now, let us understand while loop through a flow chart
As you can see similar to the previous Loop, even the while loop has a decision box inside this the condition will be provided once after the control in count is the And if it finds the condition to be true, then the statements inside the while loop will be executed else.
It will exit the while loop to understand this let us execute a program as you can see this particular example is based on while loop we have the counter variable which is declared to the value 1 and followed by that. We have a while condition Loop here and inside the while loop.
We have a condition which states that the counter variable should be less than or equal to 5 until this condition is true. The loop will be executed. Added and you can find the increment or decrement variable which is present inside the while loop.
So inside the while loop we can see that count plus plus which means increment operator now, let us try to execute this program and see the output as you can see the program has been successfully executed and the while loop ran until the condition was true which states
That the counter variable should be less than or equal to 5, so when the counter variable reach the maximum limit the while loop got terminated and the next set of Trieste It’s very executed. Let us understand the next type of loop which is do while loop do
While loop is completely similar to the while loop. The only difference is the condition will be stated at the end of the loop. You can see this when we execute a practical program, but before that let us check the flow diagram of to while loop in do-while Loop,
The statements will be executed first. Once the statements are executed. Then the condition is checked if the condition is true then the same set of statements. Are executed again else it will exit the loop now, let us try to execute a program to understand.
How does it work as you can see this particular example is based on do-while Loop. You can see that the counter variable J is initialized to zero and once after the initialization process is done, you can see that we have directly entered into the do while loop
And in this do while loop we cannot find any condition instead the control is directly entering into the loop and the first set of statements are executed. Once the statements are executed you can see the increment operator over here which will increment the value of J from 0 to 1.
And once after the increment is done, then the condition is checked the condition states that the initialization variable should be less than or equal to 5 until then this particular code segment can be executed the only difference between while loop and do-while loop is that the while loop will execute once
After the condition is checked, but do while loop is executed without checking the condition. Which means the two wire Loop will be executed at least for once now let us try to execute this program and see how does it work. As you can see the program has been successfully executed
And the output is also generated. The output was generated until the condition was true with this. Let us move on to the next topic which is infinite. Loop infinite Loop is a situation where the condition of the loop fails and the execution process
Until you stop it in manually infinite Loop is not actually a particular type of loop. It is a situation where the condition whatever you have provided to other types of looks like while loop do while loop for Loop or nested Loop. If the condition inside that Loop is failed,
Then the compiler starts to execute the code segment present inside the loop for infinite number of times until you stop it. Manually. This is the risk which you might face when you fail to provide the proper condition to your loop with this.
Let us move on to our next topic which is based on pointers. The pointer in C language is a variable which stores the address of another variable. This variable can be type of end care array function or any other type point. Under the size of pointer depends on the architecture the pointer
In C language can be declared using hashtag symbol. So to understand pointer in a much better way. Let me give you a small example. Let us assume that you have declared a variable by name n which is of integer data type and you have stored some value that is equal to 10. Now.
What is happening here is n is pointing to a value which is 10 and it is storing the value in a particular memory location. So Must have a memory location address similarly as you have your house address in the same way every variable in C language also has an address.
So for example n might be having some address like 1 0 1 1. So if you want to access and directly then you have to access its address. So for that you have to declare a pointer which can access n directly through its address.
So to declare a pointer variable you have to use an asterisk which defines that the variable which you have declared is a pointer variable. So here I have declared a pointer variable p with hashtag symbol that makes it as in Star p is equals to address
Of n so pointer variable P stores the address of n which is directly accessing the value present in N before we execute a program. Let us understand the advantages of pointers. We can return multiple values from a function using pointers followed by that. It makes you Bill to access any memory location
In the computer’s memory we can dynamically allocate memory using malloc and calloc functions using pointer variables pointers in C language are widely used in arrays functions and structures. It reduces the code and improves the performance now, let us try to execute a simple program to understand pointers in a much better way.
So this particular example is based on pointers, as you can see, I have declared an integer type of array with the name first array and the stat is size is 10. So inside the array. I have stored 10 different values starting from 1 to 10.
And after that I have initialized a pointer variable. So the pointer variable is pointing to the memory address of first array. So what I’m trying to do here is to print the address of each and every element present in the first array, let us try to execute this program and see
What are the addresses of all the elements present in first array. As you can see the code has been successfully compiled and the addresses of all the values present in first array are being displayed here. For example, the address
Of value 1 is 0 x 7 f f f 0 56 EA for 60. So this is a hexadecimal value once after you convert it into a decimal value. Then you can have a particular address where the particular value one is been stored. Similarly.
All the other values have also got a different address through which they can be. Just now with this let us move on to our next topic which is escape sequence escape sequence can be defined as a combination of backward slash and a letter or digit. These sequences are non-printable
And I used to communicate with this many device or printer by sending non-graphical control statements to specify the actions like new line or tap space. Let us understand this in a much better way escape sequence is none other than a combination of backslash and a letter or digit.
So we have a particular number of escapes. Once this in C programming which have different meanings. So basically in a c program we use newline and tap space to provide new line for the airport or to provide a tap space between the obtained output if you are printing an array,
So we shall understand what are the meanings of each and every escape sequence which is provided with C programming language. As you can see. These are the few examples of Escape sequences design in C programming language. The first one is alarm which is provided by backslash a and the second One is backspace.
And the third one is form feet and the fourth one, you know this backslash n which means new line. And after that we have had is written and followed by that which is the common one. We usually use while printing mattresses. So that is tab space.
And finally we have vertical tab followed by this. We have a few more Escape sequences, which are double backslash and backslash quote backslash double quote back slash question mark and backslash triple n which It is an octal number and followed by that. We also have back /x H which describes it is
A hexadecimal number and the last one is a null character which we use in strings. So the next topic for our discussion will be functions the most crucial and most important topic in C programming is functions. So what a functions a function can be defined as a subdivided program
Of the main program and close within flower brackets. Functions can be called by the main program to implement its functionality. This procedure provides code reuse ability and modularity few examples for functions are Library functions and user-defined functions. Actually. These are the only two types of functions which are available with C programming now,
Let us understand functions in a much better way, for example, you have a program which has to add two numbers for multiple times in your program. For example consider you had to design a program to add numbers. And inside your program you had to add numbers for ten times.
So to design this program, we had to create an addition operation for 10 times for the first set of two numbers for the second set of two numbers third set of two numbers similarly for the 10th set of two numbers. So here you’re accessing addition function for 10 different times.
So this increases the code length and also increases the compilation time. So the compilation time doesn’t matter for this simple operation, but if you are designing a bigger software, Then this compliation time will matter a lot and the code length also matters a lot instead. You can just write a function
Where you can Define the addition operation and you can just pass the values 10 times less function and the operation will be executed. So this reduces the code length drastically and also the compilation time will be reduced drastically and the code performance will be improved exponentially.
So with this let us try to understand the advantages of functions in C programming advantages of functions are Writing the same code repeatedly in program will be avoided which is the example we discussed about and after that functions can be called any number of times in a program according to the previous example,
We had to call ten times. We can also call multiple times more than 10 also and after that tracking a program easily when it is divided into multiple functions, for example, if you divide your main program into multiple functions, then tracking it would be easy. For example,
If you have designed a program for boredom as function then if you find a problem in Multiplication operation then you can directly enter into multiplication function and check where the error is. But if you did not follow the function type of programming then it might be a difficult task for you to find
Where the error is and coming into the last Advantage reusability is the main achievement of C functions with this let us come into the rules for using functions. The rules to be followed to use functions in C programming are as follows firstly function declaration a function is required to declare Ask Global
And the name of the function parameters of the function and return type of the function are required to be clearly specified. This says that the function should be globally declared this means before you start your main program you need to declare what are the functions you’re going to use
In your program followed by that you have parameters and name of the functions and the return types, which means whatever the name is specified to a function should be clearly specified and it should be readable and also it must include the parameters the values which we you will be passing to the function
And the return type which means if you are getting some value for example as we discussed before if you’re performing an addition operation, then it is sure that you’ll be getting a result out of it. So whatever the result or whatever the written type you’re getting should be specified followed by
That you have the next rule which says function call. Well calling the function from anywhere in the program care should be taken that the data type in the argument list and the number of elements in the argument are Searching for example, you have defined an addition operation. So for a basic addition operation,
You might be having two operands. So in the main function, if you are calling the addition operation, then it must be taken care of that before calling the function. The operands in the main program are also equals to 2. It is not possible to have three variables in calling function
And two variables in called function. This is an error so that case should be Then followed by that, the next rule is function definition after the function is declared. It is important that the function includes parameters declared code segment and the return type value just declaring a function globally doesn’t mean
That your function should work to make your function work. You must also Define it which includes the parameters which are going to use in it and the code segment on the operation that you want to perform should be included and also the return type of value followed by this visual understand
How to use Functions there are four different aspects of using functions in C programming. The first is function without arguments and without written value. In this case. You don’t pass any arguments to the called function and similarly from the called function end. You won’t pass any values to the calling function
In the second aspect function without arguments and with written value in this particular type. The calling function will not provide any arguments the called function, but the called function Will return a value to the calling function. The third type of aspect is function with argument and without written value in this case.
The calling function will provide some arguments to the called function, but the called function will not return any value to the calling function. The last aspect is function with arguments and with written value in this particular case, the calling function will provide some arguments
To the called function as well as the called function after executing the operation will provide results. To the calling function now, let us try to execute some examples to understand this in a much better way functions can be called in two ways either by reference or either by value.
It is execute two types to understand this in a much better way first. Let us execute call by value. This particular example is based on call by value. So here what you are basically doing is you’re actually calling a function by passing values. So let us try to execute this program and see
How does it work as you can see the program is been executed successfully and that output has been displayed here. So the value of x before calling the function is hundred. And once after the function is been called through value. Some operations have taken place in the function
And the result is been given by the called function to the calling function and the value of num is been changed from hundred to two hundred. And finally the value of x is hundred after function is been called now, let us try to execute a program based on And by reference
As you can see this particular example is based on call-by-reference where the function will be called according to the addresses of the variables used in the program. As you can see the outputs have been successfully generated and the values have been changed here since the addresses of the values have been accessed.
So if you access the value of a variable through its address than the values will be changed and this kind of operations is awfully risky. So if you are a fresher, then don’t prefer to use values and Access them through addresses with this let us move on to our next topic
Which is based on data structures data structure can be defined as a collection of data values the relationships among them and the functions that can be applied on to the data. They can be classified as follows primitive data structures of built-in data structures. And the second type is abstract data structures
Or user defined data structures. It is understand the data structures in a much better way and what are the types of data structures present in C programming language and how well supported let us see. They can be classified also based on the characteristics before this may classified them according to the nature.
But here we can characterize them according to their characteristics firstly the linear ones and followed by that. We have the nonlinear data structures. The next are homogeneous followed by that. We have non homogeneous and static and finally the dynamic data structures. It is understand each one
Of them in a much better way and understand what exactly they are firstly let us deal with linear data structures. A linear data structure stores, its elements in an arranged linear sequence nonlinear data structures nonlinear data structures store the elements in a nonlinear fashion, for example tree and graph followed by that.
We have homogeneous data structures a homogeneous data structure stores, the elements that are made up of same data type. For example array the next are non homogeneous data structures. Now, I’m homogeneous data structures toward the element that are of different data types. For example Structures followed by that we have static data structures
At static data structure are those structures whose size and the allocated memory is fixed and cannot be changed during runtime. For example array followed by that. We have Dynamic data structures and the only difference between static data structures and dynamic data structures is that he can change
The memory size of the data structure in runtime. So the most famous example for dynamic data structures is linked list now, let us understand each one of these These data structures in a more detailed and better way as you can see the data structures according to the first description are divided
Into primitive data structures and Abstract data structures. The primitive data structures are character float integer pointer and the abstract data structures or the user defined data structures are high-risk less files and inside less you can find nonlinear less and linear list. If it is a nonlinear list,
You can include stacks and queues and if it is a linear list, then you can include trees and graphs now let us Each one of them in a much better way firstly let us discuss arrays and hiring is defined as a collection of similar type of data stored in contiguous memory locations.
The array is a simplest data structure where each data element can be randomly accessed using its index number. There are three different types of arrays namely one-dimensional two-dimensional and three-dimensional. Now, you must be confused about continuous memory and index. I’ll explain uh and everything in a more detailed way firstly let us understand.
And one dimensional array so one-dimensional array can be defined as an array with single row and multiple columns the elements in one dimensional array can be accessed using index numbers now here will be understanding what is index number. What is continuous memory? What is one dimensional memory through this example,
So here this is how a basic one dimensional array looks like this is how the memory will be arranged in the physical memory of a computer when you have declared an array. So the idea what I Big land is having the size of 5 and the memory addresses
Or the index of the array is known as the address which is specified for each and every memory location. So the first memory location of the array is 0 and this 0 is called as the index number. So the index number followed by 0 is 1 and similarly 2 3 and 4.
So the fifth element is having the index number as for the index numbers for arrays always start from zero now, let us try to add elements into this system. Mmm, the first element is 10 so into the array system the element n will be included similarly.
Let us try to add few more numbers into the iris system. They are 20 30 40 and 50 according to the definition. The elements will be arranged in a continuous order followed by index 0 the next index one will store the next element which is 20 and similarly 30 40 and 50.
So this is how one dimensional arrays work if you want to access any element. This one dimensional array index number is used here. Now, let us discuss two dimensional arrays the two dimensional array can be defined as the array of arrays the two-dimensional arrays are organized as matrices,
Which can be represented as the collection of rows and columns the elements of the array access using intersection of coordinates. Let us understand this in a much better way through the next example here. We have a two dimensional array. So this is how a basic 2 dimensional array looks
Like it has two different rows and five different columns. So to access any particular array you need to select the intersection. For example, if you want to access the element, which is stored in second row First Column, then you have to access the array index array 1 1 which is this one and
If you need to access the elements stored in second row First Column the need to provide the address as 1/4, which is this particular location now, let us try to add elements into this array. The first element would be 10 which will be stored in the first row First Column. Similarly.
Let us add few more numbers which are 20 30 40 50 60 70 80 and 90 and finally hundred now, as you can see the elements will be stored in a continuous format. And this is how the elements look after when they are stored into the array.
Now, let us move on to multi-dimensional array multi-dimensional array is completely similar to the two-dimensional array. Only thing is another dimension is Added to the erase. That is it might be 3D or even multiple dimensions a multi-dimensional array can be defined as an array of arrays.
The 3D array is organized as 3D Matrix, which can be represented as a collection of multiple rows and multiple columns. The elements of the array are accessed using the 3D intersection coordinates for this. Let us move on to our next topic which is lists lists are linear data structures
Similar to that of arrays, but the only difference is that the elements are not safe in a Sequential memory locations. So the types of linked lists are singly linked list doubly linked list and circular linked list to understand linked list. We can assume this what happens in arrays is the elements are stored
In a sequential manner that is a particular brick block of memory will be reserved for the array and that block of memory will be divided into the number of indices. For example, if you decide an array of five elements of integer data type then a bigger block of memory. We will be selected
And that will be divided into five elements each memory block will be having same size integer size which is 2 bytes or four bites. But in case of linked list, if you define a linked list of file lemons, then the memory is not reserved.
What happens is you will be having a memory Heap out of which five blocks will be randomly selected. So each one of the block will be having its own address. For example, you can see the first block is The address of one zero one one followed by
That the next block of memory has 1 0 to 3 and similarly the last has 7 3 to 8. So this is how the fire random integer type of memory blocks are selected. And after that the elements in your list will be added to each one of them.
So the first element will be added to some random block of memory, which is the 10 has been added to the first block so which is 1 0 1 1 and 20 to 1 0 to 3 and 32 3. Zero to six and forty two,
Four five three seven and last one is added to the seven three two, eight one. Did you see the change here? The seven three to eight is changed to null. So this is what describes the end of list. So what exactly happens here is all the elements are given to one
Or the other memory block and all the memory blocks are interconnected with each other as you can see. The first element is 10. The second element is 20. The third element is 30 the fourth element is for And the fifth element is 50. So all of those are allocated to one
Or the other memory and once after all the numbers are added to all the blocks then each and every block is connected in a sequential order in here. The first block is connected to the second block and second connected to 3rd and 3rd to 4th and for 250.
And once after the last block is the reached that block memory address will be changed from the existing memory to null this indicates the end of linked list. So this is how it behaves like an array so it has the starting and ending points and the starting point is called as head
And the ending point is called as tail and all the elements are called as nodes. So this is how a basic lens list exists. So we have three types of linked list in C programming language, which are singly linked list doubly linked list and circular linked list. Don’t worry.
I will explain you each and every type of linked list in a much better way along with practical examples. So firstly singly linked list second is doubly linked list and the third one is circular linked list firstly we have the singly linked list in singly linked list. We have head
And tail the first block will store the head element and the next block of the head will store the address of the next connected node. Similarly. The next node will store the element and the other block will store the address of the upcoming node this continues until the tail node is reached similarly.
We have doubly linked list. The only difference between single English and a doubly linked list is in singly linked list. You can only travel in one direction that is from head to tail. But in doubly linked list, you can Traverse in both the vase
From head to tail and tail to head this is possible because the head stores the element along with it has two more blocks where one of the block will store the null element in the head and the other will store the address of the upcoming node.
And when you come to the second node, you have to address blocks. One address block will store the address of the upcoming note and the other address block will show the element of the previous node. This happens until the tail node is reached similarly. We have circular linked list circular linked list is
Completely similar to the singly linked list, but the only difference is that the tail node will not have null element instead. It will store the address of head block or head node. So that’s how it will be connected Back To The Head and the complete list will become a circular linked.
East now we shall execute the elements based on arrays single one dimensional array two dimensional array and coming into list which will execute singly linked list doubly linked list and circular linked list firstly we will execute one dimensional array example As you can see this particular example is based on one dimensional array
And I have provided the size of array as 3 which means it can store for elements the index of arrays starts from 0 as you can see, I’ve stored the employee IDs of four different employees in one single dimensional array. Now, we shall try to execute an example
For two dimensional array in this particular example is based on two dimensional array, which is having two different for Loops. One is the external for Loop and inside that experiment for Loop. We have the internal for Loop. This is repeated for twice here. Now. Let us try to execute
This program and see how does the two-dimensional array looks like the two-dimensional array must look like a matrix. Now you can see the program has been successfully executed and it is asking the values now, let us include the values first one second, which will include two similarly. Let me follow it.
As you can see the two dimensional elements have been printed here. There is some problem in print statement. I think I have missed the escape sequence, which is backslash now, let us re execute the program again. You can see the elements printed in Matrix format. Now. This is how the output looks like.
Let us execute examples for Linked lists. The first example will be based on singly linked list, as you can see this particular example is based on singly linked list and we can find the head elements as well as detail elements, including all the notes in between.
You can see the program has been successfully executed and it is showing the head element as hundred and the tail element as 600 which is described by the null pointer, which is located at the tail part. Now let us try to execute an example for doubly linked list.
You can see this particular example is based on doubly linked list and in here, I am defining head and the last tail and the current node and this is the structure of node what we have defined here which includes the data and previous and next addresses
Which are the pointer variables and followed by that. We have the link definition here the head link and after that we have the current link and lastly we are trying to insert elements 10 2013. T 40 50 and 60 into the linked list now. Let us try to execute this program.
You can see the program is been successfully executed and the doubly linked list is been described here. You can see the head node, which is 10 and followed by that you have the next node, which is 20 30 40 50 and 60. Each of the other nodes are connected to each other
And finally you have the head node, which is interconnected to it. Now, let us try to execute the third type of linked list, which is circular linked lists. So this particular example is completely similar to the previous one where we have the elements interconnected with each other
And last element will connect with the head. As you can see the elements have been successfully displayed here and it is a singly linked list. As I said before. The only difference is that the last element which is the tail element gets connected with the head which makes it a circular linked list.
Let us move on to our next topic which is stacked. So what is stack stack is a linear data structure which follows a particular order in which the operations are performed. The order might be either lie for which is last in first out or first in last out or called as phyllo.
Back is completely similar to an array but the only difference is if you want to access stack then it should be last in first out our first in last out. Let us see this example to understand. How does it work firstly let me add the first element which is 10 into my stack.
So 10 will be added to the first location my stack the 0 followed by that let us include few more elements which are 20 30 40 and 50. So these are added to the respective locations. So 20 is added. It to 132 index 242 index 3 and 52 index for now
If I want to access the elements in stack. I cannot randomly access it. All I need to do is to access the elements one after the other which is last in first out our first in last out according to the first in last out
Or last in first out we have 50 as the last in element and it should be the first out element also. So first 50 is popped out and a That 40 is popped out and 30 is popped out 20 and 10. So this is
How the elements are pushed and popped in a stack. So when you’re placing an element into the stack, it is called as push and when you’re putting out the elements out of this track, it is called as Pop the elements 10 20 30 40 and 50 were pushed
Into the stack and using the last in first out principle. We have popped out 50 40 30 20 and 10 now let us Try to execute an example to understand this in a much better way as you can see this particular example is based on stack
And here we will be pushing few elements into the stack and later. We will be popping the elements as you can see this particular main program is having a push function where I’ll be pushing eleven twelve and thirty three elements into the stack
And I’ll also be popping out them in the same way using the first and last out principal now, let us execute this program. As you can see the program has been successfully executed and the elements eleven twelve and thirty three are pushed and using the last in first out principle.
We have popped the element 33 from the stack now, let us move on to our next topic which is q q is completely similar to stack but the only difference is it follows fifo, which is first in first out now the definition says that a queue is a linear structure
Which follows a particular order in which the operations We performed the order is first in first out let us understand the terminology of Q similar to stack the elements are pushed into the queue first 10 and followed by that we are going to push the elements 20 30 40
And 50 into the queue and unlike the same wave which we pop the elements in stack. We are going to DQ the elements from Q in a reverse order, which is first in first out so according to this principle then was the element which was first pushed. Into the queue now,
Then must be the element which will be first pushed out or popped out or dq’d from the queue as you can see 10 is been dq’d from Q followed by that 20 will be the next element which will be dq’d from q and next 30 and similarly 40 and 50.
So this is how the queue works first in first out. Let us execute a program to understand cues in a much better way. We will be interviewing the elements 1 2 3 4 5 & 6 and after that we will be displaying the queue and after that will be executing a dequeue function
To DQ the elements inside the queue now, let us run this program and see how does it work. Don’t worry about the code. I’ll be linking that with the description. As you can see the elements which are inserted are 1 2 3 4 5 and once after the queue is full the elements
Have been displayed here and one element was deleted. So the first element 1 is deleted and after that the elements present in the QR 2 3 & 4 & 5 let us move on to our next topic which is graphs graph is defined as a data structure
That is represented in a graphical format using nodes and edges a graph in C language is commonly represented in two formats. Firstly a distance Z Matrix and adjacency list. Now. Let us discuss the first type of graphs you can see this is a graph and here we have the nodes which are 0
1 2 3 and 4 and the lines which are connecting 0 and 1 are called as edges. So we have edges for 0 to 1 0 to 4 and similarly we have adjust from one to four one, two, three and one two two and in this way, all the elements are interconnected
To each other whew. You are connected and you may not now let us describe the graphs using adjacency Matrix as you can see. This is adjacency Matrix and the links between the nodes have been described using adjacency Matrix. For example, 0 to 0 if there is a link existing between 0 to 0,
Then it will be specified as one if there is no link between 0 to 0 then it should be specified as zero so zero to zero we don’t have any Edge so it must be Represented as 0 0 to 1 B do have an edge
So it should be represented as 1 and 0 to 3. You might think there is an edge but there is no direct Edge. So it is 0 and similarly we don’t have a direct connection to 2. So it is also 0 now. Let us see the eye Justin C Matrix
And understand this in a much better way. So 0 to 0 there is no Edge. So it is described as zero and zero to one day was in it so it is described as 1 and 0 to 2 p don’t have an inch direct Edge.
So it is described as 0 similarly 2 3 & 2 0 to 4. We do have an H. So it is described as one. So this is how it is in C Matrix works and similarly. The adjacency list at the sincerest is nothing but a linked list
Which will show the links between the nodes. So 0 to 1 we had a link and 0 to 4. We also had a link but once after 0 is connected to 1 and 4 0 is not considered. To any of the other elements. So the end of adjacency list
Is finished here and followed by that. The next node is 1 so 1 is connected to 0 1 is also connected to for one is connected to 2 1 is connected to 3 but one is not connected to one. So we have an end of the adjacency list here and similarly.
We have two three and four. So this is how adjacency list works next. We have directed graphs. The only difference between a normal undirected graph and directed graph is The direction so here we describe the connection between the nodes based on the direction. So we do have a connection between three to two
But we don’t have a connection from 2 to 3 because the direction is outwards from three to invert to so this is how you need to consider the graph and you have to design The adjacency Matrix and adjacency list. So according to this we don’t have a link between two to three,
But we have a link between three to two. Who is directing two three? No, we have zero, but three is directing to to yes, we have one similarly. We have the adjacency list here 0 is connected to 1 1 is connected to 2 and 2 is connected to 1
And 0 and 3 is connected to 2 4 to 5 and 5 to 4. Now, let us understand weighted directed graphs weighted directed graphs are completely similar to directed graphs. The only difference is that we have certain baits or certain cost. As for the traversal of the notes from one
To another on The Ridges. So according to the adjacency Matrix, there is no change. But according to the adjacency list. We have a little change which includes the cost or the weight of the traversal of notes from one to another followed by this. We have files basic file handling techniques
In C programming provide the basic functionalities that user can perform against files in the systems. Now, let us understand this in a much better way. So the following functionalities at the basic file handling techniques to create a new file F open with attributes a A plus W
Or W plus plus which will understand this in detail in the next upcoming Concepts opening files. We have to use the F open function and reading files we can use the functions F scan F or F get see and to write in a particular file.
We can either use fprintf or F butts and followed by that to moving to a specific location in a file. We can either use FC core. Or rewind and finally to close the file. We have to use the function f clothes. Don’t worry. I’ll explain each one of these functions using
A specific program in short F open is used to open a file fclose is used to close. The file fgets is used to read a file and F print is used to write into a file. Let us execute programs based on all of these three factors. Let us try to create a file
And write some text into that file. So here I am trying to create a file by the name enter a car. Yahtzee and the message I will be writing into that file would be a generic. Happy learning. As you can see the program has been successfully executed
And the file has been created and the message which I wanted to print is been successfully written into the file, which is Erica. Happy learning. Let us try to execute a different program where we have to just read the content which is present in the file.
As you can see this particular program has been designed to read the contents present in that particular file. Let us now try to run this program. As you can see the program has been successfully executed and it is reading the file with some message which says it is missing semicolon.
Okay, let me introduce semicolon here. Let me try to run this program again if the error shows again. Yeah, they might be some problem with the compiler. The compiler is reading the message. Basically if you execute this program in an actual see editor, which is Turbo C++ or C.
It should not throw an error. Yeah, it is reading the file. So now let us move on to our next section which defines the functions of our wa and r+ W + + a + so our search is a file if the file is open successfully using F open function,
Then it loads the particular file and sets a pointer to it and that pointer will be specified to the first character which is starting in the particular file followed by that. We have w w just searches for a file if the file. Then the contents will be overwritten
If the file does not exist. Then a new file will be created and a null value will be returned if it is unable to open that particular file. Next is a a search is for the file with the file is open successfully then F open loads it to the memory
And sets up a pointer and that points the last character of the file if the file doesn’t exist, then a new file is created and if a file doesn’t open then it returns a null value now, let us go into R plus W plus and + r + such as a file
If it is open successfully then F open loads it up into the memory and sets up a pointer which points to the first character in it. If it is not open, then r+ will return null pointer if it is unable to open it followed by that.
We have W plus W plus searches for the file. If the file is existing, then the contents of that particular file are overwritten and if the file doesn’t exist, then it creates a new file and in case if it is not able to open it it will written a null pointer similarly.
A plus a plus 30 is a file if the file is open successfully then it loads into the memory and sets the pointer to the last character of the existing file if it doesn’t exist, then a new file is created. And finally if it is not able to open the file,
Then it returns a null value. Next we have strings. The string is defined as a one-dimensional array of characters terminated by a null character the character array or the string is used to store text such as word or sentences. Each character in the array occupies one byte of memory
And the last character must be 0 so as discussed before strings are completely similar to erase the elements are stored in sequential memory similar to array, but the only difference is that strings include a null character at the end of the array just to convey the compiler
That it is the end of the string. So strings are basically two types which are characterized or string literals now we have some basic operations. Which will be applied on strings the most basic operations, which we apply on strings are string length, which will return the length of the string followed by that.
We have string copy which copies the string from source to destination and string cat which is used to join two or more strings. And after that we have string comparison, which compares the given two strings after that we have string reverse which reverses the given string followed by that.
We have string lawyer and string up which converts the case of the string for example, if we use Ring lower then the upper case of the string will be converted into the lower case and similarly string upper will convert the lower case characters into the upper case characters. Now, let us use Eclipse.
So this particular example is based on string length operation where I have provided the string at Eureka and I am trying to find out the length of the string now, let’s run this program. As you can see the length of the string at Eureka is found to be 7 similarly.
Let us also execute other programs based on other string functions. The next string function is string copy where I’ll be trying to copy the string at Eureka from string 1 2 string to and see if the data is been printed into the string to or not. Let us try to execute this program.
As you can see the data from string one is copied into the string to and the data inside string to is been successfully displayed here followed by this which will also execute an example for string comparison in this particular program. I’ll be comparing the string one
With string to let us execute this program as you can see the program is been successfully executed and now let me type in the string one, which is Eddie Rekha similarly let Open the string to in the same way how the string one is been declared, which is at Eureka now,
Let us compare a string 1 is equal to string two or not. As you can see the strings are declared to be equal. Now, let us try to execute a program based on string concatenation in this particular program. I’ll be concatenating two different strings.
As you can see I have provided string one as hello and string to as world. So according to my function I should we concatenate in both the strings which is hello and world as you can see the program is been successfully executed and both the strings are concatenated into one single word,
Which is string hello world followed by this. We shall also execute a program for string reverse function in this particular example, I’ll be writing the text happy learning into the string and I will be trying to reverse this particular string. Let us execute this program.
As you can see the string before reversing is Happy learning and sharing after reversing is this let us try to execute an example based on string lawyer where a particular string in uppercase will be converted into lowercase. Now, let me write a string in uppercase. I have written a to Rekha in uppercase.
Now. Let us convert it into lower case. So as you can see the data has been displayed as 80 Rekha in lowercase. Similarly. Let us try to execute a program based on strength upper case. As you can see the program has been successfully compiled. Now. Let me write some text in lower case.
It is Rekha is been written in lower case and according to the output at Eureka text is been converted into upper case. Now with this let us get into our next topic which is about structures and unions firstly let us deal with structures a structure is
A user-defined data type available in C language that allows to combine the data items of different data types together structures are used to represent. Record struck is the key word that is used to declare structures basically structure is a data type. For example, if you consider array or lets you can only store
A specific data type of elements because I will not allow you to store different elements of different data types, but what if you had to store a database of a school which has different data types for example roll number will be integer marks will be float
And name will be string in that case. You cannot use arrays or you cannot even use Is list the only available option is structure or either a union what structure basically does is it is capable enough to store the values of different data types. For example here. I am considering school.
I have defined structure using the keyword struct and the name of my structure is School inside, which I have declared ID of a student using integer data type and name of the student using string data type and percentage of the student using float data type now to understand structures in a much better.
Let us execute a practical program now, let us use an online compiler to compile our code. So in this particular example, I am writing the roll number name and marks of a particular student. So let us execute this code as you can see the code has been successfully compiled
And it is asking me for input the name roll number and percentage of the student. Let me write a random name to the student. Raj and his role number will be 1 2 3 followed by that. Let us include his percentage which will be 75 point for
As you can see the data has been successfully imported for the first student. Now as we have defined the for Loop which will run for three times. So let us also include another student’s details here named as from and his role number will be 1 to 4 percentage will be around 60 now.
Let us enter the value. third student as you can see the details of all the three students have been displayed here. So with this let us move on to our next topic, which is Union a union is a special data type available in C language that allows store
In different data types in same memory location Union is the keyword used to declare Union. So you might be confused about structure and Union here, why are two different data structures defined for performing one single operation. Don’t worry. There are few difference between union and Shell which we will discuss after this topic.
So let us execute an example to understand Union in a much better way. We shall consider the example of employees. As you can see the code has been successfully compiled and the date of the employee has been displayed here. The idea of employee is
One eight five one eight seven seven four five zero and the name is John see with this. Let us move on to the differences between structure and Union the key differences between structure and Union our keyword memory change of value access to data and initialization. The keyword to declare structure is struct
And the keyword to declare Union is Union. After that, the memory in structure each member has unique memory space. For example ID has a unique memory space name has its own unique space and followed by that even the percentage has its own unique space
When it comes to Union a whole complete block of memory will be declared for Union and all them data members will be sharing that allocated memory when it comes to the next parameter. It is change of value changing the value of one member in structure will not the others
But in case of unions changing the value of one member will definitely affect the others because they’re sharing one common memory after that access to data all members can be accessed at one single time if you are using a structure, but when it comes to Union,
You have to access one member at one single time. You cannot access all the members at one single time. And the last parameter is initialization. All the members of the structure can be initialized at once but when it comes to Union The only first member of the Union can be initialized.
So these were the few major differences between structure and Union. Let us move into the next topic which is data segments. So what a data segments data segments are divided into four different types. They are data area code area Heap area and stack area.
What are these let us discuss each one of them in detail firstly data area data area is permanent memory area all static and variables are stored in this data area all variables which are stored in data area exist until the program exists followed by that.
We have code memory area code memory area is only area which can be accessed by function pointers. The size of code area is completely fixed followed by that. We have the Heap area. The hip area is used to store the data structures, which are created by using dynamic memory allocation.
We shall discuss about dynamic memory allocation and the functions available in dynamic memory allocation. After this concept, so the size of hip area is variable and depends upon the free space in the memory followed by that. We have the Stacked area stack area is divided into two parts naming initialize and non initializing
Initialize variables are given priority than on initialize variables. So with this we shall move on to our next important topic which is dynamic memory allocation. So what is dynamic memory allocation? Dynamic memory allocation is defined as a procedure in which the size of the data structure. Ah is change during the runtime.
There are four Library functions provided by sea in standard Library dot h header and this facilitates the dynamic memory allocation. Now, let us discuss the dynamic memory allocation functions. Those are malloc calloc realloc and free firstly Malik Malik or memory allocation method is used to dynamically allocate
A single large block of memory with the specified size. It returns a pointer type void, which can be cast into a Tower of any form followed by that we have catalog catalog or continuous memory allocation method is used to dynamically allocate a specific number of blocks of memory of specified types.
It includes each block with a default value of zero, the only difference between malloc and calloc is Malik after locating a block of memory will not allow Kate with a default value of zero instead a junk value is stored there but in catalog it will allocate each and every memory block.
With a default value as 0 followed by that we have real look real luck or relocation is a method used to dynamically change the memory of previously allocated memory. If the memory is previously allocated with help of malachor Kellogg and in case if it is insufficient, then you can reuse relock
And increase the memory size relock is used to dynamically reallocate memory for it by that. We have free method free method is most important method don’t ever forget to use free. When you’re allocating memory using dynamic memory allocation because free method D allocates the memory the memory allocated using the functions malloc
And calloc are not dear located on their own. Hence free method is used whenever the dynamic memory allocation takes place. It helps to reduce the way stage of memory by freeing it. Now. Let us execute programs based on Malik Al of relock and free now,
Let us execute this program based on my log. So this particular Particular example is based on Malik. We are using malloc function here and the data type we have chosen is integer. Now, let us execute this program. As you can see the number of elements we have chosen a five
And the memory has been allocated using malloc and the elements of the array are 1 2 3 4 and 5. Similarly. Let us execute a program based on catalog. As you can see the memory has been successfully allocated using catalog function and the elements are 1 2 3 4 & 5.
Let us execute real talk. Now you can see the memory is been reallocated using real usually using malloc and calloc. We have created elements only four five elements, which were one-two-three-and-four-and-five we needed extra space for six seven, eight and nine and ten. So we have created that extra space using realloc here.
Now, let us use free method to free all the memory which we have created using catalog Malik and real log functions. And you can see the memory will be freed using free method as you can see the output says that the number of elements were five memory successfully allocated using malloc
And Malik memory is been successfully freed. Similarly catalog is also been freed. Now that we have completed dynamic memory allocation Concepts. Let us now move on to sorting algorithms sorting algorithms are used to arrange the elements of an array in a particular order the order might be either ascending order or descending order.
Now C programming language has n number of sorting algorithms out of which we have some primary important basic algorithms, which we will be discussing now. Basically, what is a sorting algorithm a sorting algorithm is used to rearrange a given array Or List elements according to the comparison operator on the elements.
The comparison operator is used to decide the New Order of elements in respect to the data structure as we have discussed earlier. The Sorting algorithms are used to arrange the elements of array or List It in a particular order either ascending or descending it is based on a condition
Which you specify now with this let us move on to the Sorting algorithms that we will be discussing today firstly bubbles on and followed by that. We will discuss about insertion sort and finally selection sort. So first the bubble sort bubble sort is the most basic and brute
Force method of arranging the elements of an array. Now what happens in bubble sort in bubble sort. We consider the first element of the array to be the smallest of the other elements and this small element will become paid by the next following element if the following element is smaller
Than the first element they will be swap followed by that the second element of the array will be compared with the 3rd and 3rd with forth and so on Let Us Now understand this through this simple example, as you can see in this particular example, we have an array of five elements
In the first iteration the first element which is minus 2 will be compared with the second element 45 if the second element is smaller than the first element then they will be swapped in this particular algorithm. We are going to arrange the elements in ascending order. So the smaller element will be located
In the first place firstly you can see it’s minus 2 is smaller than 45 So In the comparison – 2 will be left in the first position and in the second iteration, you can see 45 will be compared with the third element, which is zero.
So here 0 happens to be a smaller element than 45. They both will be swept here followed by that in the third iteration. We will be comparing 11 with the element 45:11 happens to be the small element than 45 so they will be swapped again.
And in the last iteration 45 will be compared with -9 + -9 happens to be the smaller element than 45 so they will be spared. So this was the first cycle. So in this cycle, the elements are arranged to a particular extent but not completely so we will follow the next cycle
Where the elements will be compared again from the starting in the starting iteration – to will be compared with 0 as you can see – to happens to be smaller than zero. So no changes in the positions of the elements followed by that 0 will be compared with leaven
And Levin happens to be bigger than 0 so no change in that as well in the next iteration 11 will be compared with -9 + -9 happens to be a small element compared to 11 so they both will be swapped and in the last iteration Levin will be compared with 45 well,
When will be the smallest so no change in their positions and now we will see the third cycle in the third cycle minus 2 will be compared with 0 in the first iteration followed by that 0 will be again compared with – Nine – nine happens
To be smaller than zero so they both will be scrapped. And in the third iteration 0 will be compared with leaven and the positions will not be changed because 0 is less than 11 and in the fourth iteration 11 will be compared with 45 where 45 happens to be the biggest number
Than even so no change in the position. So in the last cycle – to will be compared with -9 + -9 happens to be a smaller element compared to minus 2, so they both will be scrapped. And in the last iteration you can see that minus 9 is in the correct position.
That is the smallest position happening to be the first of the array and followed by that. We have the second position with -2 and third position. We have 0 for 11 and finally fortified hence. The array is been sorted here. So this is how bubble sort algorithm works now.
Let us see a practical example so that we can understand this algorithm in a much better way. So this particular example is based on bubble sort. We are going to arrange each and every element in a particular position based on ascending order. This is the same array
That we have considered in our previous example, which has the elements – to 40507 and – 9 now let us run this program and see the output as you can see the program got successfully executed and the elements of the array are in ascending order
And the ascending order is been achieved through bubble sort. Algorithm followed by this. Let us move on to our next sorting algorithm which happens to be the insertion sort insertion sort is also completely similar to bubble sort. But the only difference is that we presume
That the first element of the array is already sorted. So in this case, we consider the first element of the array which is 9 is already sorted followed by that we Five so we will take Phi as the key. So here we have considered five as the key.
And now we compare Phi with nine. So in comparison if Phi happens to be the smaller number than the presumed arranged element, which is 9 then they both will be swapped. We can see that Phi is already smaller than 9 so
What we do is we replace 9 with five and five with nine. So in the next iteration we will Consider the first two elements to be sorted and we will consider the third element as the key. Now. We will take one in the position of key
And now we’ll compare the key with the first two elements which are 5 and 9 so we can see that one happens to be smaller than 5 and 9 and inside we can see that Phi happens to be smaller than 9
So we send nine to the position of 1 and Phi 2 the position of 9 and 1/2 the position of 5, so this is how it And firstly We compare one with five and one with nine and we find that one is smaller than 5 and 9 and later.
We compare 9 and 5. So we find that Phi is smaller than 9 the bigger number will be sent to the place of the key and the next following number will be sent to the position of the earlier number and the smallest key will be sent to the first position followed by this.
We have the next iteration. We will consider for as the key. Key, so for will be taken into the position of key now for will be compared with the previous Elements which are 1 5 and 9 so we can see that 4 is greater than the first position 1
So we will not make any changes to the first position followed by that we will compare four with five. So here we can see that 4 is smaller than 5 for it by that will compare for with nine. We can see that 9 is bigger than 4 what we will do is now
We’ll compare the Other two elements in the array which are 5 and 9 fi is smaller than 9 so we will send 9 to the key position which is 4 so followed by that. We will send fi to ninth position as shown in the next iteration. And now the position
Where Phi element was removed will be empty and inside that position we will send for and that’s how the array will be sorted here. You can see 1 4 5 and 9 and in the next cycle we will take the element 3 as the key and we’ll compare with the Other elements
As you can see three is taken into the key position and now three will be combined with all the elements in the array as you can see three is compared with one and you can see one is already arranged and three is compared
With for and you can see 4 is bigger than 3 now. We will compare the other elements with three and three is compared with five and it happens to be 3 is smaller than 5 followed by that 3 is also smaller than 9 now the elements
Inside the arrays are compared now the number nine happens. The biggest one so 9 will be sent to the key position followed by that five will be sent to the ninth position and for will be sent to the fifth position and three will be in the position of 4, which will be vacant
After four is transfer to the fifth position as you can see now the array is completely sorted in the ascending order where one is the smallest number followed by that we have three next is for Nexus 5 and lastly we have 9 now, let us see a practical example to understand insertion sort
In a much better way. Let us consider the same array what we have discussed here. So this particular example is based on insertion sort and we will be considering the same array what we have discussed in our earlier example, which is nine five one and four and three,
Let us execute this program and see if the array will be sorted in ascending order according to insertion sort or not. As you can see the program has been successfully compiled and we obtain the same result what we discussed before which is 1 3 4 5 & 9 Now with this,
Let us move on to our last algorithm, which is selection sort algorithm selection sort happens to be a little different from the other two algorithms here. We actually compare all the elements with each other and we come to an end with a smallest element and we declare that particular smallest element as minimum
And we will replace that element to the first position. And after that we come to the next cycle where we swap the second smallest element in the second position of the array now, Now let us understand this through this particular example where we have considered an array
Which has five elements namely 2012 10 15 and 2 now, we will compare the elements present in the array as you can see the first element happens to be 20. We will assign 20 as minimum in the first cycle. Now, what we do is we’ll compare 20 with 12
And we will find out which is the minimum 12 happens to be the minimum number when compared with 20. So 12 is Now the new minimum element not well will be compared with the next following element which is 10. We can see that 10 is the next minimum element found in the array
And now 10 is the new minimum element declared followed by that. We will compare the next element with the minimum which is 15 15 is greater than 10. So there is no change next we will compare then with the last element which is to now
We find that too is a smallest element compared to 10. So the last minimum Ament finally obtained S2. So what we do here is we will swap to with the first position which is 20 as you can see now the element 20 is been replaced by 2.
So 2 is the first element sorted in selection sort algorithm before moving into the second cycle. Let us have a small acknowledgement of the first cycle. So this is how the comparison happened firstly we can split 20 was the minimum number but well happened to be the minimum number followed by that.
We got our next minimum number which Stan and lastly we got to our minimum number and finally the array got sorted with the first element placing two in the first position followed by that now we shall move to the next cycle in the next cycle. We can see the first position
Of the array to is been successfully sorted now, we shall consider 12 to be the minimum number. So we consider the minimum number to be 10 now 10 will be compared with the next following number which is 15 and we find that 10 is already.
Smaller than 15 so they will be no change in the position followed by that. We have 10 and 20. So 10 happens to be the smaller number when we compare with 20, so there is no change in the position. So now what we do is we shall swap the position
Of 12 with 10 making that the next smallest element in the array with this we finish the second cycle now, we shall move to the third cycle where we will consider the following element after 10, which is 12 to be the minimum. Element and we will compare with the other following elements
Which are 15 and 20 as you can see. We have considered 12 to be the minimum number and we compared with 15 and we obtain 12 is smaller than 15. So there is no change in the position in the next iteration We compare 12 it 20 and we find
That 12 small number compared to 20. So there is no change in the position again. So we find that the array has been already sorted for this cycle and we will move on to the next cycle where we will make. In as our minimum element
And we will compare 15 with the following element which is 20. So here you can see that we have made 15 as our next minimum element and now we are comparing with the element 20 and we find that 15 is already smaller than 20 so there is no change in the positioning
And finally we obtain the sorted array now, let us execute a practical example to understand selection sort algorithm in a much better way we shall consider the same array in that particular example as well. Now this particular example is based on selection sort algorithm and we have taken the same Elements
Which we have discussed in the earlier example, which had 2012 10 15 and 2 if the elements will be arranged in ascending order according to selection sort algorithm or not. As you can see the program got successfully executed and the elements of the array have been sorted
In ascending order based on selection sort algorithm. And there isn’t what we have obtained is completely similar to the results. We have discussed before which is to 10 12 15 and 20 with this we come to an end of this tutorial and I hope this tutorial was useful
If you have any doubts or any queries, and if you need the code for any of the examples that we executed in this particular tutorial, you can write to us in the comment. Below and we will reply you at the earliest till then. Thank you and happy learning.
I hope you have enjoyed listening to this video. Please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist And subscribe to Edureka channel to learn more. Happy learning.
0 Comments