RUN TIME ERROR ( SIGSEGV )

//http://www.codechef.com/problems/GCD2/
#include<stdio.h>
unsigned int gcd(unsigned int a,unsigned int b)
{
if(b==0) return a;
else return(gcd(b,a%b));
}
void print(int a[],int len)
{
int i;
for(i=0;i<len;i++)
printf("%d",a[i]);
printf("\n");
}
main()
{
int t,length,i,j;
int a[251];
unsigned int b;
char c;
scanf("%d",&t);
while(t–)
{
scanf("%u",&b);
getchar();

	  length=0;
	  while((c=getchar())!='\n')
	  {
	     a[length++]=c-'0';
	  }//Input a while
	  if(b==0){
		//printf("detected");
		print(a,length);
		continue;
		}
	  unsigned int num=a[0];
	  int length2=1;
	  while(num<b){
		num = num*10+a[length2++];
	}
	//calculating a%b in first case
	num = num%b;
	while(length2<length)
	{
	    num = num*10+a[length2++];
	    num = num % b;
	}
	//printf("%u\n",num);
	printf("%d\n",gcd(b,num));
}
}

Although i don’t know whether this will correct your SIGSEGV error, but, one should always return 0 at the end of main() function.Otherwise this leads to NZEC (Non Zero Exit Code).And some errors lead us to other errors misleading us from the actual error.Hope this helps!!

1 Like

This code works fine on my laptop But when i submit it is giving this error :frowning: … Ofcourse I am testing with small numbers … It would be lot more helpful if some more test cases available

@n1e13 :It will work fine in your laptop,but the online judge requires your program to return 0. It indicates successful completion of your program,regardless of your output being correct or not.Have you tried submitting after writing return 0; statement? if not, please do.if it continues to give the error then, for now, i dont know what is the problem.