Scanf and CIN giving different outputs. Please help.

I wrote a program and it was satisfying the test cases. I submitted the program but it gave TLE.
So, I replaced all CINs with SCANFs. I don’t know C, so please tell me what’s going wrong.

I cannot share the entire code, because its part of an ongoing contest.

cin>>t;                        ->    scanf("%d",&t);
cin>>n>>q;                     ->    scanf("%d",&n);scanf("%d",&q);
cin>>left[i]>>right[i];        ->    scanf("%d",&left[i]);scanf("%d",&right[i]);
cout<<sum<<endl;               ->    printf("%d",sum);

EDIT:
My output has changed after using scanf. Why is that happening?

It seems fine to me except printf("%d",sum);, you missed new line -> printf("%d\n",sum);.

You can also replace scanf("%d",&n);scanf("%d",&q); with scanf("%d%d",&n,&q); :wink:

I am getting a different value at the output all together. Yes, I must include the ‘\n’ when I submit the solution.

If you declare variables long long then you must use %lld. Other then, that your code looks fine.

also depends what are the types of t, n, q, and so on %d is for signed integer, read more here - http://www.cplusplus.com/reference/cstdio/scanf/ for example for unsigned long long int use %llu

you should read format specifiers for scanf() and printf(), for e.g., “%d” is a format specifier which prints integer value, just go through it and every thing is ok :slight_smile:

I am using ‘int’ everywhere

again your code looks fine. Now, check while inputing scanf("%d",&left[i]);scanf("%d",&right[i]); Both of these are enclosed with in braces.

I guess my there is some problem with my compiler (Dev C++). The solution got accepted when I used Counting Sort instead of QuickSort.