Integer array declaration and definition

When declaring the array int arr[9] to
define the array, the input data is up to the subscript arr[6] , and the system automatically assigns arr[7] arr[8] to any value **-...**


 int x; //输入数据 
 int i = 0 ; //记录位数
 int a[5]; //存储逆序
 int temp; 
  printf("输入数据x:");
 scanf("%d",&x);
 while( x >= 100000 && x > 0)
 {
    
    
  printf("数据超过5位,请重新输入:");
  scanf("%d",&x);
 }
  //输出
 printf("\n%d是%d 位数\n",x,i);
 printf("原来数据顺序:");
 for( int k = i-1 ; k >= 0  ; k-- )
 {
    
    
  printf("%d",a[k]);
 }
  printf("\n逆序后:");
 for(int j = 0 ; j < i ; j++ )
 {
    
    
  printf("%d" , a[j]);
 }

Result:
Insert picture description here
If you modify the code: for( int k = i; k >= 0; k--)
Result:
Insert picture description here
Reason: At this time, only 0 8 3 is assigned to arr[0] arr[1] arr[2] , remaining arr[3], arr[4] are automatically assigned by the system
**Summary: **The value of the unassigned subscript when defining a static array is automatically assigned any value by the system;
if you want to enter the data by yourself, please use dynamic Declare array

Guess you like

Origin blog.csdn.net/angelsweet/article/details/104225452