First of all there is nothing wrong with your code which can lead to segmentation fault (or a run time error, like you said).
If you are using an Old Compiler which really rely on this, well happens only because that compiler is using an earlier version then C99.
Starting with C99 there is no need of return type of main as return 0, because is already implicit, this means that the following program is corect an Legal:
#include<stdio.h>
int main(void){
/* code here */
/* no return needed */
}
Another thing, starting with C11 you can use VLA (Variable Length Array) so this mean that the following program is Legal and you can take the benefit of creating an Unknown fixed Array SIZE (Unknown as long, as you don’t consume the whole STACK of course):
#include <stdio.h>
#include <stdlib.h>
int main(void){
int size,temp;
printf("Please type the SIZE of the Array: ");
if( (scanf("%d",&size)) != 1){
printf("Error scanf, Fix it!");
exit(1);
}
int array[size];
printf("Please type those %d Elements of the Array: ",size);
for (int i=0 ; i<size ; i++){
if ( (scanf("%d",&array[i])) != 1){
printf("Error scanf, Fix it!");
exit(2);
}
}
for (int i=0 ; i<size ; i++){
for (int j=i+1 ; j<size ; j++){
if (array[i] > array[j]){
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("The sorted Array is: ");
for(int i=0 ; i<size ; i++){
printf("%d ",array[i]);
}
printf("\n");
}
Output:
Please type the SIZE of the Array: 5
Please type those 5 Elements of the Array: 9 3 4 8 2
The sorted Array is: 2 3 4 8 9
You probably noticed that I checked the return of SCANF and if fails the program stops. Many programmers ignores this important part. Your program should not continue if there is an SCANF error.
Another benefit (starting with C99) is declaring the int i direct inside of the for loop like this:
for (int i=0 ; i<size ; i++){}
But of course avoid declaring the same i in nested for loops :)…like here:
for (int i=0 ; i<size ; i++){
for (int i ; i<size ; i++){
/* code here */
}
}
Read more about C11 standard and if possible use it.