C_47 Arrays in C - Part 2 | Initialization of arrays in C programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the series of learning programming in the previous video we have discussed arrays in c means uh what is needle fairy what is array or definition and then declaration of 1d array only right we are left with many points about array like initialization of array how areas are stored like memory representation of array how to access elements of array types of areas we will discuss all the points one by one in this video i will talk about initialization of array two types compile time and runtime and then i will show you practical also on my laptop practical part of previous lecture and for this lecture also like declaration and initialization both i'll show you on my laptop right so you also get ready with your laptop with some id in store like i have vs code editor in my laptop right now before starting the lecture just want to tell you one thing an academy is going to conduct a combat for gate and esc it's a it's india's largest free scholarship test for gate which is going to be conducted on 11th of july and 20 questions you will get and time limit would be 60 minutes here you will get a chance to go head to head to battle with head you know thousands of learners from all over india and the questions you get are the best questions means these are curated by the most experienced and the top esc and gate educators of unacademy so take it live to compete with the best real time leaderboard would be there after every questions to tell you whether you answered it right or not and where you are standing among all others a personalized ranking and rating will be provided to you just to so you can measure constantly measure your progress and one more important point is what after every contest a detailed video solutions from the academy educators would be given to you so that you can identify your weak areas and you can do what improvement according to that and if you take it live then you will get a chance to win some exciting prizes some gifts also you can see the detail over here so enroll now for this combat and use my code jkl10 to unlock and for the preparation of gate and esc you can select a subscription plan that suits you that is shown over here plus and iconic the features are over here you can see the features and you can select any subscription plan that is best for you and use micro jk 10 to get 10 discount on your uh subscription so better to subscribe now before the price hike all the links i'll give you in the description box of this video you can go and check out so now let us see how areas can be initialized there are two ways at compile time and at runtime both will see first at compile time it means when you declare the array at that time only you initialize the array means what elements what numbers you want in that area at that time only you will provide the data items like you remember i guess how to declare an array first of all the data type then add a name i am taking here only a and size i am taking only 5 right this is what declaration right now at the time of declaration only we initialize the array we give the data items equal to suppose i am initializing it to 0 minus 1 11 10 2 and semicolon this is what initialization of area at compile time right five values i have given fine we will see different different types also now first method is this this one will see all the methods this is correct second method to initialize that compile time is what you can just write data item array name and i am not providing here the size right if you declare something like this means it will give error in the previous video i have told you but if you write something like this i am not giving the size of the array but i am initializing it like this 0 1 2 again 0 minus 1 6 7 that is correct this is second method now here automatically the size would be calculated the size of array would be what how many elements are there one two three four five six seven so its size would be seven now see another variant is what if i write something like this int a and 5 equal to 0 1 minus 1 that's it means size is 5 and i am giving only 3 data items then what will happen the remaining date items like two we have left we are left with more two more data items the space for two more data items so those would be initialized with zero fine like 0 here suppose if i represented something like this then 0 1 minus 1 and the remaining 2 locations would be filled with 0 0 automatically right so okay now that this is fine but if you write something like this next is what if i write something like this in a 5 and that's it means now the array is having what garbage value any garbage will not zero any garbage value the data items here in these five locations any garbage value would be there if you print without initializing then garbage value would be printed i'll show you the output also right and if you uh write something like this 5 equal to and here i am giving 1 2 3 4 5 six seven the size is five but i am providing seven data items so that this will give error right because we have already fixed what size is five we can only store five numbers next what we can ah do is suppose i have written only this int a five that said and here you can write a of zero a of 1 a of 2 and here you can initialize these data items like 1 2 minus 1 a of 3 and a of 4 here 11 10 this is also fine individual items you can initialize something like this but obviously we are not going to prefer this method right this is not the right way but yeah you can initialize i am just telling you the number of ways how to access why i am writing this a then in these brackets 0 means the first index of array would be started from 0 so the first element we access the element of the array something like this how to access that we'll discuss in next video right but for this video just i'm just telling you the method so we are not going to prefer this method right next what you can write if i write something like this in 5 is equal to i am writing here 0 only that is also correct all the memory locations would be filled with 0 1 0 and remaining would be obviously automatically 0 i am providing only 1 0. next if you do something like this in a 5 and blank i am not giving any data here this will give error this is not allowed you have to give at least one data item either you can give 0 or any rate item or minus 1 or anything that is fine but you cannot leave it leave it like blank it will give you error it's not like that you can only write here end i can also write here float that would be a floating type array right all the data items here at that time if you write here float that would be a floating type or you can write character something like this like care array i am taking care error name is b and here i am not declaring the size and here i am just writing how to write down character because it is character array in single quotes suppose it's j e n n and y right so this is what a character area of size one two three four five data items i have given right or here you can declare the size suppose i am declaring size 10 and i am giving the data items only five then the remaining items will be null initialized with null value right now here what we have done we have fixed the size this is what a drawback right one by one will discuss everything about error and then last video i'll i'll tell you all the properties of all the points about area so this is what we have fixed with the size of the array now we cannot change it and also we have fixed what value we want in array at runtime we cannot change these values but if you want that users should enter at the runtime user should enter the element of the array means it will ask enter the number of enter the elements in array and user will enter any data it means that is what time initialization of array this is what compile time we have fixed this data items this is compile time right now these are all variants we will see practical also right now i will show you how to initialize array at runtime at runtime we are going to use some standard functions to initialize that is obviously for loop any loop four while do while and scan a function right so just declare the array in a suppose i am taking size is five i am not initializing here the array now at run time i want so suppose when i run the program then it should print for at starting enter the elements of array so for that printing we will use what function printf right here you can write enter the elements of array in double quotes and whatever you will write here in double quotes that would be exactly printed on the screen now obviously we have to give five numbers right so what we will use loops so i am using a for loop i is equal to i am going to start from 0 i less than 5 and i plus plus now before using i what you have to declare it into i now here we can take input how we will take using scanner function i have already discussed printf and scanner function in my previous videos the functioning of these functions you can check it out then format specifier for integer it is percentage d address of arrays a a of i right now if you want to print then again you have to use again a for loop and you can print this is what at runtime you can initialize the array now this program when this program will be run the compiler will what first of all would compile these lines so in i means here in memory some space would be allocated to this i suppose address is thousand here is any garbage value and for a also array name is a and for five red numbers the space would be allocated suppose the base address of array is two thousand two thousand for this four bytes for this one so this would be 2004 2008 12 and 16 right here also we have some card base value we haven't initialized it it now enter the element so far it would be printed for loop i is equal to zero so i would initialize it with zero i less than zero yes condition tree will enter here scanf means it will take you and from the user percentage d address of a of i i value is 1 0 first of all i value is 0 means address of a of 0 index is 0 here 1 here 2 here 3 here 4 so a of 0 is this one now this address is 2 000 so here whatever number the user will enter that would be store suppose user will is entering 10 so a 10 would be stored right so like this you can see the i guess you all know the working of for loop i have discussed many times then i plus plus i would be one then again condition true again this process would be repeated and this is how the array would be initialized index of array would always be started from zero zero one two three uh here till size minus one size is five five minus one that is four right and if you want to print suppose i have initialized ten 11 minus 1 0 2 if you want to print the data of the array simply again take again for loop i is equal to from 0 to 5 5 plus plus and in that for loop rather than scanf to print for output we take what which function printf function printf right percentage d and simply a of i you can write here and there right i'll show you practical of this thing also now when to use compile time initialization and when to use runtime initialization because we have two ah types of initialization of array see if we have only small sizes very small like five or ten so we can initialize that thing at compile time it's very easy to initialize it in a is equal to 5 and you can write 5 data numbers or you can write 10 that is fine but if the size of array is large like 100 here we have size is 100 in a 100 so obviously it's not possible to write here 100 data items yeah we can write but it's not a proper way it's not a good thing right so there we can do what at runtime initialization and maybe suppose we want like first 50 data items or first 38 items would be initialized with maybe 0 or 1 or remaining data items should be initialized with 2 or 5 for anything so that time it's better to use runtime initialization right ha c now here suppose size i have 100 and i want if i is less than [Music] 30 then a of i would be initialized with 1 right else a of i would be initialized with remaining portion would be initialized with zero so first 30 numbers in array would be ministerized with one and remaining would be initialized with zero right so if you want something like this this situation is there that that is also you know we want that runtime initialization is better so now that's it for this video in the next video i'll discuss how to access elements of array and memory representation of array and i'll see in the next video till then bye
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 43,701
Rating: undefined out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, arrays in c programming, c programming tutorials for beginners, introduction to arrays in c, c jennys lectures, ugc net computer science preparation material, gate cs lectures, how to learn to code, introduction to c programming
Id: kMwYD03kVk0
Channel Id: undefined
Length: 15min 36sec (936 seconds)
Published: Thu Jul 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.