I tried to run this but it keeps give me zero value. What is wrong in this code?
#include<stdio.h>
main(){
int i,min,max,arr[3]={10,20,40};
int *ptr_arr;
ptr_arr=&arr[3];
for(i=0;i<3;i++){
if(max>*ptr_arr)
max=*ptr_arr;
if(min>*ptr_arr)
min=*ptr_arr;
}
printf("The Maximum Number Is %d\n ",max);
printf("The Minimum Number Is %d ",min);
}
ptr_arr=&arr[3];is accessing an address beyond the end of the array. This results in undefined behaviour and could lead to a seg fault event.forloop is not traversing the array so what ever uninitialized/trash values are at arr[3], min and max on the stack are just compared 3 times. Suggest: eliminate ptr_arr, initialize max to MIN_INT, initialize min to INT_MAX, use arr[i[ for the comparison valueint main( void )andint main( int argc, char *argv[] )The optional declaration isint main()which is the same asint main( void )Note thatchar *argv[]is the same aschar **argvWhen compiling, always enable all warnings, then your compiler would have told you about the problem.