473,545 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9325
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
long'. If it isn't, you need to supply more information.
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
long'. If it isn't, you need to supply more information.
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<uns igned
long>(std::nume ric_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_MA X)
is longer it will fit. If AsString(INT_MA X) is shorter, it won't. If
the
lengths are equal, you can do a string compare and it will fit if
AsString(INT_MA X) < digit_sequence_ without_leading _zeroes.

HTH,
Michiel Salters

Dec 13 '05 #4

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

Similar topics

11
7139
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is prime....
7
4922
by: Jim Cook | last post by:
We have a macro which takes various index constants as an argument and offsets into an array. The macro can be an Lvalue or Rvalue. The index is not zero based. I would like a compile time error displayed if the index is out of bounds. Is there a way to do this well? I read through the FAQ that I could find mentioned, and did not see this...
7
6121
by: vikky | last post by:
hi all, Out of sheer curosity, I decided to initialize an integer with a number bigger than INT_MAX, however I still am not able to justify its output. Here is the program : #include<stdio.h> int main(void) { int t=0xFFFFFFFFFFFFFFFFDABC;
2
3699
by: Marlene Stebbins | last post by:
I am entering numbers into my program from the command line. I want to check whether they are > INT_MAX. Sounds simple, but I've discovered that if(x <= INT_MAX) { /* use x in some calculation */ } else { /* exit with error message */
6
17979
by: angus | last post by:
hello, i'm wondering how i can check if an picturebox has any content(as in got an image). thanks in advanced. angus
21
8712
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a pointer to the start, the value (most of the time zero) and the size of the memory array to clear. Problems appear when the size given is not the...
9
11190
by: LeAnne | last post by:
If the user types some text into a rich textbox, i need to know how many lines there are in the control. Note, I'm not interested in counting the number of the carriage returns or line feeds. Suppose, the rich text is the height of a textbox (can view only one line at a time), then if the user keeps typing and word wrap = TRUE and multi...
4
2351
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw exceptions?
1
1904
by: halcyon943 | last post by:
have 4 folders that I watch and need to move files from to another location. Three constraints: -Finish time. Make sure the program stops transferring files at a specific time -Number of files transferred. Can only move a certain amount of files per time period. -Folders have a priority. The files have to be moved based on the folder...
0
7416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7776
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6001
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5347
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4965
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
729
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.