Exceeding scanf's maximum input (Standard C)

  • Thread starter Thread starter spursfan2110
  • Start date Start date
  • Tags Tags
    Input Maximum
Click For Summary
SUMMARY

The forum discussion addresses an issue with using the `scanf()` function in C to input a number between 0 and 50,000. The user encounters a problem where inputting a number greater than 37,504 causes the program to stop unexpectedly. The root cause is identified as the incorrect declaration of the variable `max` as `long int &max`, which is not standard C syntax. The solution is to declare `max` as a regular `long int` variable, ensuring proper compilation and functionality of the program.

PREREQUISITES
  • Understanding of C programming syntax and data types
  • Familiarity with the `scanf()` function for input handling
  • Knowledge of variable declaration and memory management in C
  • Basic understanding of compiler error messages and debugging
NEXT STEPS
  • Learn about proper variable declarations in C, specifically regarding data types
  • Explore the differences between C and C++ regarding variable references and pointers
  • Study common `scanf()` pitfalls and best practices for input validation
  • Investigate compiler flags like `-Wall` for enabling warnings during compilation
USEFUL FOR

C programmers, software developers troubleshooting input issues, and students learning about variable management and compiler behavior in C.

spursfan2110
Messages
19
Reaction score
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
The declaration should be "long int max" (that is, remove the '&').

(Does it even compile as is?)
 
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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K