Initializing an Array
ฝัง
- เผยแพร่เมื่อ 28 ก.ย. 2024
- C Programming: Initializing an Array in C Programming.
Topics discussed:
1) Initialization of one-dimensional array.
2) What will happen if the number of elements is less than the length of the array?
3) Different ways to initialize an array with all the elements as zero.
C Programming Lectures: goo.gl/7Eh2SS
Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
Contribute: www.nesoacademy...
Memberships: bit.ly/2U7YSPI
Books: www.nesoacademy...
Website ► www.nesoacademy...
Forum ► forum.nesoacade...
Facebook ► goo.gl/Nt0PmB
Twitter ► / nesoacademy
Music:
Axol x Alex Skrindo - You [NCS Release]
#CProgrammingByNeso #CProgramming #Arrays #ArrayInitialization #ArraysInc
Will cops invade my house if I add more elements than mentioned?
Yes you are sentenced to watch 10 hour skibbidi toilet
Due to your good behaviour you are now released
A small correct at 5:20 The statement `int arr[10] = {};` is legal in C, but its interpretation and behavior might not be immediately clear to all compilers, depending on the C standard they adhere to.
Explanation:
1. **C99 and later**:
- In C99 and later standards, you can initialize an array with `{}`. This is called a "universal zero initializer". It means that all elements of the array will be initialized to zero.
- The statement `int arr[10] = {};` will initialize all elements of `arr` to `0`.
2. **Pre-C99**:
- In C89/90 (the earlier C standard), the empty braces `{}` as an initializer might not be explicitly supported, and this could lead to a compiler error or warning.
- Instead, you would typically initialize an array explicitly or with `{0}`:
```c
int arr[10] = {0}; // All elements initialized to 0.
```
Example in Code:
```c
#include
int main() {
int arr[10] = {}; // Legal in C99 and later
for(int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
Compatibility:
- If you are using a compiler that supports C99 or later, the code should compile and run without issues, initializing all elements to zero.
- If using an older compiler that does not support C99, you may get an error or warning. In such cases, use `int arr[10] = {0};` for compatibility.
### Conclusion:
- `int arr[10] = {};` is legal and initializes all elements to zero in C99 and later.
- For maximum compatibility with older C standards, use `int arr[10] = {0};`.
Programming can't be more easier how you making this!❤️❤️❤️
found this after my mid sem , hopefully ill ace my end sem by learning from this , hats off to u sir, ur infinite times better than my teacher !
How is it going?
you fail!!
Always respect your teacher.
@@andistheinforitbutso7513 shut up
@@subhathishvenkata3716 ok burbook
I m now doing my 1semister of my CSE the best explanation...that I have seen
Sir u are a genius in explaining and this channel is great
Thanku for ur clear voice and explanation sir
Sir at 3:40 my sir told that it store garbage value instead of 0's to save time and working 🙄
Other please correct me🙏all who ever know this
in method 1 and 2 how can you initialize array not at declaration?
Thanks teacher your very helpful ..
Nice video sir.. If possible plz upload video fast
Thanks a lot sir... I barely got any doubt in this topic now
Perfect explaination 👌👌
thank you so much i enjoy programming bc of you!!!
thank-you so much sir you help a lot of people by this video
Sir you are the best with your wonderful explaination and clear👍👍🤗
if you write
int arr[SIZE] = {0};
Only the first element will get the value zero and the rest are filled automatically by the compiler, try changing 0 to 1 and see the difference
Really it helps me a lot
Very well explained.. Thank u
If I declare an array of type float and initialise the first value as 0.0f. Will the rest of the values also be initialised as 0.0f? Example: float array[5] = {0.0f}; The reason I ask is because I'm working with a microcontroller and calculations on a float value will be done with double precision and not single precision unless I specify 0.0f and I don't need the extra accuracy but need to save computation time.
Sir ur much better than my hod
Thank you sir
Nice, keep up the good work.
How can i get numbers in a array string if the numbers are between spaces?
Example:
Array string = { "12 3 20"}
Very Helpfull Lecture
Awesome content an d good explanation too..
How to use pointer aary in function ?please make video on it sir.
hi! just came across this video while roaming around, am very interested in learning programming, and was looking for definition of an array, any advise where can I start ?
maybe you can start with learning c language
1:33 In method 1 and 2, data type for array is not written. But is it possible to declare and initialize arrays separately like that ?
You are good, sir, I am just smiling😁
Thanks a lot sir
Sir it's my heartiest request plz upload regularly my interview are near
Why was rest of the elements are zero.is there any chance the rest of the elements becomes garbage values?
verry verry nice video sir thanks.
How to initialize array of string?
string arr[] = {a, b, c, d};
I do this but it's error
What's index means
arr[10]; will also make all elements zero;
1) int arr[5];
arr[5]={1,2,3,4,5};
Why is this code showing error?
2) int ar[5]={};
Sir, even this code works fine in VS code.....in fact, this is the easiest way to initialize an array with all zero.
We cannot initialize and declare arrays separately like that. U can do only if u are assigning one element at a time.
Ex :
int arr[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
@@meowrbius arr[5] means you are accessing the 5th index of arr array(actually not initialising the array), If you have already declared array then you have to use method-3,4 not 1, to declare, define and intialize you can do int arr[5] = {1 ,2, 3, 4, 5}
@@darshanjaviya4770 yea.
And in the video, he is saying no need to write data type in method 1. That's wrong actually
@@meowrbius yes I also noted that
sir can you kindly add links to the source code
Nice
aap ka video achha hai but thoda smjhane ka style thik nhi hai
10 Q
Great sir. Neso Academy's way of explanation is really exciting. Would love to see you get more views by people such as me❤
thank you so muuuuch from Germany Sir
Videos from Neso Academy are absolute gold. Pure concept.
int a[5]={}; still initializes all array elements to zero why is this illegal?\
i m from nepal & its helping us a lot! thank you very much sir
Also please upload pointers, structures and data structure implementation in c(tree, stack, queue, link list, malloc etc..) Thank you
Sir it's my heartiest request plz upload regularly my interview are near
What are you doing now? Any job aur interview ka kya hua 😮
int arr[10] = {}; is legal when compile by GCC
Great lectures Sir, this is so detailed.
Is it possible to declare a 1D array like so;
int arr[] = {0};
yes of course it is a valid element. 0 is of data type int so it is a valid array arr[]
please make it fast
❤❤❤ from odisha
will you please provide the tutorials of Design analysis and algorithm?
Write a program that initialize array of size 10, and prints all Armstrong and Palindrome numbers in array. Your program should ask user about their choice of printing. If user press ‘A’ or ‘a’ then program should display all Armstrong numbers in array and if user press ‘P’ or ‘p’ then it should display all Palindrome numbers in array.
can u give a example for this int arr[10]={0}; code
flower brackets :'D 5:50
The illegal cases that u explained are not illegal at all. They are working just fine
You are excellent man, keep up the good work 👍👍
Thank you so so much sir😊👍
Thanks for the uploaded this video🙏
Doing great job brother 👍
Very nice sir
How about that, is that legal?
👇
int arr[ ] = {0}
5:00 int arr[10] = {}; works in 2023. The c complier may be updated. I tested this using both gcc and clang. Here is the test code : #include
int main()
{
int a[10] = {};
for ( int i = 0; i < 10; i++ ) printf("a[%d] = %d
", i, a[i]);
return 0;
}
sir that's truth.. sir you are genius on c programming.. sir your explanation is awesome for students.. 😊
please checkout my channel i teach C in hindi with codes i make and i will make them in front of you not any presentation.
Thank You Sir
sir but when input is greater than the length of the array,it's workinf fine It's not showing any error
Thank you
thank you so much sir greetings from usa this is the exact quick answer i was looking for! :D
4:51 its not illegal it works actually it will be filled by value of zero
Please explain the 4method of array please🙏🙏🙏🙏🙏
Sir, first of all, thank you that you make such good videos for us free of cost, but sir, I have a request that along with understanding, please provide us notes to write in the exam, because i dont know how to write in the exam.
How did Rony got placed in xyz company as a software engineer without even knowing that basic thing
waiting for u r next videooo....if u fastly upload u r videos it will helpfull for those people who are now learn c...
Hey, at 3:45 the garbage value at first gives 0 for the length if it exceeds by 1 but if I use more memory for rest of the unspecified bits it gives/allocates some garbage value to the array.
Great sir we are lucky to have a teacher like you ❤
How can we write if we want to initialise all elements of the array with a number other than zero
The automatic inicilization with value = 0, does it work only with global variables o with locals also?
in the topic Initializing Array @5:17 to 5:25. you mentioned that you cant add more elements to an array than the specified size of the array. This happens to not be the case as C actually allows you to add more elements than the size array which of course could lead to a lot of problems but I think its worth noting as it appears to be a weakness with the c programming language
Sir , by keeping array size empty we can store any number of values
what happens when we directly print an array.. please reply
sir in 1st and 2nd method you didnt declare type of an array it is correct or not?
Can you make lectures on python and it’s data structures too
I am beginner. Sadly unable to find series of lectue
Can you explain about global array? please
what if we add more values than the size of the array
In method 2, there should be data type involved...
Sir what means by creating of array
Sahi insaan ko pakda maine ab c sikhne ke liye 🤗thank u soo much 💖
You r just awsome!!🤯
illegal was funny .
Oh Man you are Great
simple explanation👍
Very good dada,,,,,
Very useful for me.
Great sir❤
Gjb sir
Thank u
Thanks sir