' and '\n' inside a loop

This is a discussion on Concerns about '

2Likes

1Post By AndiPersti

1Post By Barney McGrew

' and '\n' inside a loop within the C Programming forums, part of the General Programming Boards category; Good afternoon fellas.
I'm concerned about the usage of a function I wrote to discover if a string is a ...

I don't know how to properly guard that code from overflow.

Share this post on

Registered User

Join Date

May 2012

Posts

1,066

Originally Posted by thames

this has been worrying me a bit... do I have to use the constants from limits.h every time I want to protect my program from overflows ?

I did this:

Code:

#ifndef MAXSIZE
#define MAXSIZE UCHAR_MAX
#endif

is that right?

UCHAR_MAX is the maximum value of an unsigned char (usually 255). So why do you want to limit the size of your string to it?

If you are concerned about overflow, just use the standard function strtol() (or strtoll() since C99) which handles overflow when converting numbers (strtol() returns LONG_MAX and sets errno to ERANGE if there is an overflow).