Exceeding scanf's maximum input (Standard C)

In summary, the person is having trouble using scanf to input numbers greater than 37504 in C. They have tried changing the data type and declaring the variable as a reference, but it is not working. They are seeking suggestions for a solution and have also received advice to use a normal long int variable instead of a reference. They are also encouraged to read more about pointers.
  • #1
spursfan2110
21
0
Hey everyone, I have a kind of large problem.

I am trying to input something in C, a number between 0 and 50,000, using scanf(). I understand how scanf generally works, and it does for the most part, but for some reason if the number I enter is greater than 37504, the program just stops. Doesn't freeze, doesn't crash, just stops. I have tried changing the data type from int, and have tried unsigned int and long int as well, any other suggestions about what's going on? Thanks!

Here's the pertinent part of code:

int main ()

{long int &max;


printf("Enter an integer between 2 and 50,000: ");
scanf("%ld", &max);

etc.
 
Last edited:
Physics news on Phys.org
  • #2
The declaration should be "long int max" (that is, remove the '&').

(Does it even compile as is?)
 
  • #3
It is always instructive to tell the compiler to give you all warnings.
"long int &max;" is not standard C. Self respecting C compilers won't even compile your code.
It is bad practice to use C++. C++ is a language which successfully combined all bad properties of a low-level programming language and an object-oriented one. If you want to stay close to hardware or write fast code use C, if you want to code clean and fast, use python or ruby.
Here is your code edited a bit:
Code:
#include <stdio.h>
int main ()
{
long int &max;
printf("Enter an integer between 2 and 50,000: ");
scanf("%ld", &max);
}
This is what GNU c says to it:
Code:
p$ gcc -Wall a.c -c
a.c: In function ‘main’:
a.c:4: error: expected identifier or ‘(’ before ‘&’ token
a.c:8: error: ‘max’ undeclared (first use in this function)
a.c:8: error: (Each undeclared identifier is reported only once
a.c:8: error: for each function it appears in.)
this is what GNU c++ says:
Code:
$ g++ -Wall a.c -c
a.c: In function ‘int main()’:
a.c:5: error: ‘max’ declared as reference but not initialized
So max is declared as a reference. it is something like a pointer, but in the confused c++ way.
First and foremost, you should tell in declaration time what it refers to.
But I suggest to forget this c++ crap, and use a normal long int variable instead, and everyone will be happy.
And read something about pointers: http://home.netcom.com/~tjensen/ptr/pointers.htm
 

Related to Exceeding scanf's maximum input (Standard C)

1. Can scanf handle unlimited input?

No, scanf has a maximum input limit which is typically set to 1024 characters. This means that it can only read up to 1024 characters from the input stream.

2. What happens if I enter more than 1024 characters in scanf?

If you enter more than 1024 characters in scanf, it will only read the first 1024 characters and the rest will be ignored. This can lead to unexpected results or errors in your code.

3. How can I read more than 1024 characters using scanf?

You can use a loop to read the input in chunks of 1024 characters until you have read all the input. Another option is to use a different input function such as fgets, which has a larger maximum input limit.

4. Is there a way to change the maximum input limit of scanf?

No, the maximum input limit of scanf is determined by the implementation and cannot be changed. If you need to read larger inputs, it is recommended to use a different input function.

5. Why does scanf have a maximum input limit?

The maximum input limit of scanf is in place to prevent buffer overflows and to ensure the efficient use of memory. If there was no limit, it could lead to unexpected errors or crashes in your program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
917
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
969
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top