enormous input test

#include<stdio.h>
int main()
{
int n,k,count=0,t,i;
scanf("%d %d",&n,&k);

for(i=0;i<n;i++)
//while(n>0)
{
scanf("\n%d",&t);
if(t%k==0)
count++;
}
printf("%d",count);;
return 0;
}

My code ran successfully with no problem.Just i want to know why using int instead of long int is possible in this program coz in test cases we use 999996 values for an integer where as range of int is -32768 to 32767.Also why scanf is faster than cin

No that’s not true!

In modern machines ranges of datatypes are changed

Try running this code

    # include < bits/stdc++.h >
    int main() {
    printf("Range for INT is from %d to %d\n\n",INT_MIN,INT_MAX);
    printf("Range for LONG INT from %ld to %ld\n\n",LONG_MIN,LONG_MAX);
    printf("Range for LONG LONG INT from %lld to %lld\n\n",LONG_LONG_MIN,LONG_LONG_MAX);
            return 0;
    }

You’ll get this output:

Range for INT is from -2147483648 to 2147483647
Range for LONG INT from -2147483648 to 2147483647
Range for LONG LONG INT from -9223372036854775808 to 9223372036854775807

Ideone Link for above code: http://ideone.com/sG2t5Y

So 4 bytes is allocated for INT and the max range of INT is 2^(32-1) and not 2^(16-1) i.e. 32767.

Now about comparison of scanf and cout

So you may read this link:

http://www.quora.com/Is-cin-cout-slower-than-scanf-printf/answer/Anders-Kaseorg

in which

Anders-Kaseorg says,

There are a few potential performance pitfalls with cin/cout:

By default, cin/cout waste time synchronizing themselves with the C library’s stdio buffers, so that you can freely intermix calls to scanf/printf with operations on cin/cout. gnu.org. Turn this off with

std::ios_base::sync_with_stdio(false);.

Many C++ tutorials tell you to write cout << endl instead of cout << ‘\n’. But endl is actually slower because it forces a flush, which is usually unnecessary. (You’d need to flush if you were writing, say, an interactive progress bar, but not when writing a million lines of data.) Write ‘\n’ instead of endl.
There was a bug in very old versions of GCC (pre-2004) that significantly slowed down C++ iostreams. Don’t use ancient compilers.

Hope you understood!!

:slight_smile:

1 Like