Why m getting runtime error?

My code is running successfully but it is showing runtime error.

#include<stdio.h>
void size(int[],int);
int main()
{
	int i,N, S[N],T;
	scanf("%d",&T);
	if(T>=1 && T<=10)
	while(T--)
	{
	scanf("%d",&N);
	for(i=0;i<N;i++)
	scanf("%d",&S[i]);
	size(S,N);}}
	
void size(int S[],int n)
{
	int r,i,t,x;
		for(r=1;r<=n-1;r++)
	{
		for(i=0;i<=n-1-r;i++)
		{
		if(S[i]>S[i+1])
		{
		t=S[i];                            
		S[i]=S[i+1];
		S[i+1]=t;
	}}}
		x=S[1]-S[0];
		printf("%d",x);
}

You have declared the variable N and not initialized it so it takes garbage value. After that you have declared the array with size N which i am really doubtful about.

According to me you should always declare your array after taking the size input. I did that and your code runs fine.
So i might have not told you the reason of runtime error but i have definitely told you the solution i.e. declare the array after taking N as input.

1 Like

Why you people don’t provide question links. It really helps alot to understand the code after knowing what its purpose is to do and yaa please provide solution link or post your solution using codesample option in the editor

there are languages that cannot be provided in the editor, I tried to open Dlang in the editor, and it stuck at loading

but the size of an array must be a constant ?, or is this one of GCC’s extension ?

I believe this feature was introduced in C99 if i correctly remember my programming with C lectures
I am not so sure though but declaring array after the size has been taken input seems logical to me and more importantly it always works. :xD

https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

This might be helpful