-5
#include<stdio.h>
#include<conio.h>

void main() 
{
    int a[]={1,2,3,4,5,6};
    int num,i;
    for(i=6;i>0;i--)
    {
        a[i]=a[i-1];
    }
    printf("enter any no, ");
    scanf("%d",&num); // input 12
    a[0]=num;
    for(i=0;i<=6;i++)
    {
        printf("\n\n%d",a[i]);
    }
    getch();
}

the output is 12 1 2 3 4 5 12 why??? there is no array memory problem when we do like this

#include<stdio.h>
#include<conio.h>

void main() 
{
    int a[]={1,2,3,4,5,6};
    int num,i;
    for(i=6;i>0;i--)
    {
        a[i]=a[i-1];
    }
    printf("enter any no, ");
    //scanf("%d",&num); 
    a[0]=12;
    for(i=0;i<=6;i++)
    {
        printf("\n\n%d",a[i]);
    }
    getch();
}

the memory of an array is same but the output is 12 1 2 3 4 5 6 (Now the output is change) so, when we use scanf so why the output is different

there is no mistake of Undefined Behavior and no mistake in length of an array

so plz give me the reason why this happen while using scanf????

  • It's undefined behavior. – BLUEPIXY Sep 27 '17 at 08:23
  • 3
    You have undefined behaviour because the array `arr` doesn't have 7th element at index 6 (index range is 0 to 5). – P.P Sep 27 '17 at 08:23
  • See: https://stackoverflow.com/q/15646973/4389800 and https://stackoverflow.com/q/9137157/4389800 – P.P Sep 27 '17 at 08:24
  • in c there is no matter of index – Ashish Verma Sep 27 '17 at 08:29
  • You have undefined behaviour because you assign and print outside the array bounds. It is likely that `num` is placed just after `a` on the stack. So when you print `a[6]` the code happens to print the value of `num`. – Klas Lindbäck Sep 27 '17 at 08:35
  • sory sir, but in c u can print the any index no. of an array like you declear an array like "int arr[5];" after that u insert the value on 10 index no. like "arr[10]=20" after that you print arr[10] the ans is 20 (so why this will happen) – Ashish Verma Sep 27 '17 at 08:40
  • 2
    @AshishVerma You can *try* to print any index. However, if you use an index outside the area defined for the array undefined behaviour is invoked. That means that anything could happen. You could be afflicted by nasal demons. https://en.wikipedia.org/wiki/Undefined_behavior – Klas Lindbäck Sep 27 '17 at 08:44
  • but sir, if we do like this a[0]=12; and remove the scanf command is perfectly work (so what is the reason); – Ashish Verma Sep 27 '17 at 08:50
  • 1
    @Ashish - There is no explanation. With undefined behavior *anything* can happen, including displaying what you expected. Or something totally different. There are no rules. – Bo Persson Sep 27 '17 at 10:10
  • now the array contain 6 value but when it contain 5 value the output us correct example now a[]={1,2,3,4,5,6} so the output of program is 12 1 2 3 4 5 12 and when a[]={1,2,3,4,5} so the output is 12 1 2 3 4 5 (in the condition when we take 5 element at initial time the result is up to mark it Insert in beginning correctly how????) – Ashish Verma Sep 27 '17 at 10:43

2 Answers2

2

It is undefined behaviour because you are trying to access array out of the bound.

C11 standard say's :

J.2 Undefined behavor

An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a1[7] given the declaration int a[4][5]) (6.5.6).

Undefined Behavior means that "anything can happen".The program may crash or produce nothing or print expected output.

msc
  • 33,420
  • 29
  • 119
  • 214
  • no, if we do like this a[0]=12; is perfectly work – Ashish Verma Sep 27 '17 at 08:27
  • @AshishVerma in your code i<=6 access the a[6] value, which out of bound. – msc Sep 27 '17 at 08:30
  • sory sir, but in c u can print the any index no. of an array like you declear an array like "int arr[5];" after that u insert the value on 10 index no. like "arr[10]=20" after that you print arr[10] the ans is 20 – Ashish Verma Sep 27 '17 at 08:33
  • @AshishVerma Undefined Behavior means that "anything can happen". https://en.wikipedia.org/wiki/Undefined_behavior – msc Sep 27 '17 at 08:35
  • sory, but any thing can't happen in programing for every output some logic is there i what that??? – Ashish Verma Sep 27 '17 at 08:38
  • 1
    @AshishVerma - read K&R C book, in that it is mentioned. that output can be anything. Its not logical, for some device you can come up with one logic but with other device it may fail. that's why undefined behavior – Vishwajeet Vishu Sep 27 '17 at 08:49
  • ok sir you are right , but that is condition of garbage value or increase datatype range ,,,,,,, if we do like this a[0]=12; and remove the scanf command is perfectly work (so what is the reason) the output is 12 1 2 3 4 5 6 and Insert in beginning is happen perfectly can u explain this – Ashish Verma Sep 27 '17 at 08:57
0

Analysing your code:

void main() 
{
int a[]={1,2,3,4,5,6};  //Array of 5 elements
int num,i;
//for(i=6;i>0;i--)         //i. Bound Checking Error.
for(i=5; i>0; i--)           //correct.
{
   a[i]=a[i-1];          //Trying to insert element 6 value over element 5. However you have only 5 elements in consecutive memory location. In i. Reference`
}
printf("enter any no, ");
scanf("%d",&num); // insert 12
a[0]=num;
for(i=0;i<=6;i++)  // you have only 5 elements trying to access 6 elements.
{
     printf("\n\n%d",a[i]);
}
getch();
}

According to C standard its undefined behavior and is not recommended.

Zahid Khan
  • 2,130
  • 2
  • 18
  • 31