R
RoSsIaCrIiLoIA
#include <stdio.h>
#include <limits.h>
#include <errno.h>
/* C99 behaviour for negative operands even in C89, probably broken; I
* should've replaced it with something that I know to work. */
#define DIVC99( x, y ) ((x) / (y) \
+ ((x) / (y) < 0 && (x) % (y) > 0))
The problem seems not there but
^^^^^^^^^^^^^^^^^^^^^^^^^) : (
(i < INT_MIN + VAL( c )) ? (
Here (don't know why)
in the borland compileri = INT_MIN,
errno = ERANGE
) : (
i -= VAL( c )
E.g.
C:\Documents and Settings\giu\
77
77
-77
Out of range for int.
-2147483648
^Z
0
^Z
This seems better
#include <stdio.h>
#include <limits.h>
#include <errno.h>
#define P printf
#define R return
#define W while
#define F for
/* C99 behaviour for negative operands even in C89, probably broken; I
* should've replaced it with something that I know to work. */
#define DIVC99( x, y ) ( (x)/(y) + ( (x)/(y)< 0 && (x)%(y)> 0)
)
#define DIVC00( x, y ) ( (x) / (y) )
#define VAL( c ) (((c) >= '0' && (c) <= '9') ? c - '0' \
: ((c) == 'a' || (c) == 'A') ? 0xa \
: ((c) == 'b' || (c) == 'B') ? 0xb \
: ((c) == 'c' || (c) == 'C') ? 0xc \
: ((c) == 'd' || (c) == 'D') ? 0xd \
: ((c) == 'e' || (c) == 'E') ? 0xe \
: ((c) == 'f' || (c) == 'F') ? 0xf \
: -1)
/* Acts mostly like a hypothetical strtoi( instr, NULL, 0 ) */
int readINT( FILE *fp )
{static int i, base, sign, j;
int c;
/* IF=&&; ELSE IF=||; */
c = fgetc(fp);
(base==0) &&
( i = 0 , sign = 1 ,
/*---------------------------------*/
(c=='-')
&& ( sign = -1, c = fgetc( fp ), 1)
|| (c=='+')
&& (c = fgetc( fp )) ,
/*---------------------------------*/
(c=='0')
&& ( base = 8,
c = fgetc(fp) ,
(c=='x' || c=='X')
&& ( base = 16, c = fgetc(fp) ) ,
1
)
|| (base = 10)
/*----------------------------------*/
);
return
(c>='0'
&& c<='9'
&& c-'0'< base
|| base==16
&& ( c=='a' || c=='A' || c=='b' || c=='B'
|| c=='c' || c=='C' || c=='d' || c=='D'
|| c=='e' || c=='E' || c=='f' || c=='F')
) ?
(
/*--------------------------------------------------*/
(sign > 0)
? ( (i > INT_MAX / base)
? (i = INT_MAX, errno = ERANGE )
: (i *= base)
)
: ( (i < DIVC99( INT_MIN, base ))
? (i = INT_MIN, errno = ERANGE )
: (i *= base)
) ,
/*--------------------------------------------------*/
(j = VAL(c), sign > 0)
? ( (i > INT_MAX - j )
? (i = INT_MAX, errno = ERANGE )
: (i += j)
)
: ( (i < INT_MIN + j )
? (i = INT_MIN, errno = ERANGE)
: (i -= j)
) ,
/*--------------------------------------------------*/
readINT( fp )
)
: ( c!=EOF && ungetc( c, fp ), base = 0, i );
}
int main( void )
{
int i, c;
char a[81]={0};
do {
P("Enter an int[1919 or EOF for end]>\n");
errno = 0;
i = readINT( stdin );
P( "%d==NUMBER\n", i );
if(errno == ERANGE)
puts( "Out of range for int." );
if( !feof(stdin) && ((c = fgetc(stdin))!='\n' && c!=EOF) )
{ ungetc(c, stdin);
P("Scan..."); fflush(stdout);
if(scanf("%80[^\n]", a)>0)
/*## in stdin there is '\n' ##*/
P("\"%s\"\n", a);
else P("NO out from scanf\n");
}
}while(i!=1919 && !feof(stdin));
R 0;
}