473,231 Members | 1,823 Online

# Checking if number is bigger than INT MAX

Hi all,

I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?

In the flex side I have:
{numeral} { yylval.num = strtoul(yytext, NULL, 0);
return NUMERAL; }

Is there a straightforward way to check if the number in yytext is
bigger than INT_MAX?

Cheers,

Paulo Matos

Dec 12 '05 #1
3 9213
po******@gmail.com wrote:
I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?

In the flex side I have:
{numeral} { yylval.num = strtoul(yytext, NULL, 0);
return NUMERAL; }
"flex" is off-topic, so without knowing what 'yylval' is or how its member
'num' is declared, it's hard to say. However, since you're using the C
function 'strtoul', I'll assume that 'yylval.num' is of type 'unsigned
Is there a straightforward way to check if the number in yytext is
bigger than INT_MAX?

It is implementation-specific, most likely, but check 'yylval.num' against

(unsigned long)INT_MAX

instead. Whether it buys you anything or not, is for you to decide. You
could also try to convert 'yytext' to an implementation-specific integral
type that is known to accommodate a larger range than 'int' and check its
value against (that_type)INT_MAX. I here used 'unsigned long' as that
type, and your system may have something better.

V
Dec 12 '05 #2

Victor Bazarov wrote:
po******@gmail.com wrote:
I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?

In the flex side I have:
{numeral} { yylval.num = strtoul(yytext, NULL, 0);
return NUMERAL; }

"flex" is off-topic, so without knowing what 'yylval' is or how its member
'num' is declared, it's hard to say. However, since you're using the C
function 'strtoul', I'll assume that 'yylval.num' is of type 'unsigned
Is there a straightforward way to check if the number in yytext is
bigger than INT_MAX?

It is implementation-specific, most likely, but check 'yylval.num' against

(unsigned long)INT_MAX

You might consider the "more C++y" static_cast<unsigned
long>(std::numeric_limits<int>::max())

Dec 12 '05 #3
po******@gmail.com wrote:
Hi all,

I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?

Easy: Strip the leading '0' from the string, get INT_MAX as a string
without leading zeroes. Compare lengths. If AsString(INT_MAX)
is longer it will fit. If AsString(INT_MAX) is shorter, it won't. If
the
lengths are equal, you can do a string compare and it will fit if

HTH,
Michiel Salters

Dec 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.