Output for below code

reddensoft

Banned-Spam
Joined
Oct 7, 2022
Messages
84
Reaction score
9
What is the output of the below code?

#include <stdio.h>

int main()

{

int arr[5]={10,20,30,40,50,21};

printf("%d", arr[6]);



return 0;

}
 
There are many compilers where you can test code right in the browser.

However, that code won't compile. The variable is declared to be of size 5 but it has 6 items. Fixing that, arrays are 0-based in C/C++ so the arr[6] references uninitialized memory. So you'll either get a random value or it'll just crash.

If you're working on code, the online compilers are great and you can work on code and run it right in the browser.
 
There are many compilers where you can test code right in the browser.

However, that code won't compile. The variable is declared to be of size 5 but it has 6 items. Fixing that, arrays are 0-based in C/C++ so the arr[6] references uninitialized memory. So you'll either get a random value or it'll just crash.

If you're working on code, the online compilers are great and you can work on code and run it right in the browser.
Even though its array has only 5 elements, it outputs the value of the 6th element by pointer shifting.
Given the simplicity of the program, it won't crash. Therefore, the output should be the first four bytes of the main function's return address.
 
Back
Top