This is a program I wrote to compare large integers:
#include<stdlib.h>
int compare(const void *a,const void b)
{
if((long long int )a-(long long int )b<0)
return -1;
if((long long int )a-(long long int )b==0)
return 0;
if((long long int )a-(long long int *)b>0)
return 1;
}
int main()
{
long long int n;
scanf("%lld",&n);
long long int i;
long long int a[n];
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
}
qsort(a,n,sizeof(long long int),compare);
for(i=0;i<n;i++)
{
printf("%lld",a[i]);
}
return 0;
}
It gives following warning:“control reaches end of non-void function”
How do i resolve this?
An improved version of you code is:
long long compare (const void * a, const void * b)
{
return ( *(long long*)a - *(long long*)b );
}
int main()
{
long long n;
scanf("%lld",&n);
long long i;
long long a[n];
for(i=0;i < n;i++)
scanf("%lld",&a[i]);
qsort(a,n,sizeof(long long int),compare);
for(i=0;i < n;i++)
printf("%lld\n",a[i]);
return 0;
}
You faced a problem probably because of the return type you used in compare function is int, which should have been long long.
I could not properly understand your code ( I’m a newbie ) but I know that you need < stdio.h > for printf() and scanf().
@sudipto_roy
the prototype for qsort in stdlib.h is
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
so even if your compare function’s return data type is long long
it will be casted into int
the error is in your compare function it should be
int compare(const void* a, const void* b)
{
if(*(long long int*)a - *(long long int*)b < 0)
return -1;
if(*(long long int*)a - *(long long int*)b == 0)
return 0;
if(*(long long int*)a - *(long long int*)b > 0)
return 1;
}
@eightnoteight Sir, I did not mention the headers in my answer, as they were not mentioned in the question. Including stdlib.h is well understood.