StrongARM processor EOF different?

J

Jobs'R'Us

Hello,

I have compiled the following for Linux Intel and Windows, and the
loop breaks correctly at EOF, but the same code for the StrongARM
processor does not break correctly (keeps going until max number is
reached). What gives? What am I doing wrong? I tried both getc and
fgetc, same result.

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ... main begins... */
char c;
// loop until done or until we have 50000
while(wordnumber < 49999)
{
c = getc(file);

if(c == EOF)
{
break;
}
else
{
/* code keeps going */

Help will be greatly appreciated!

Kirst
 
A

Al Bowers

Jobs'R'Us said:
Hello,

I have compiled the following for Linux Intel and Windows, and the
loop breaks correctly at EOF, but the same code for the StrongARM
processor does not break correctly (keeps going until max number is
reached). What gives? What am I doing wrong? I tried both getc and
fgetc, same result.

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ... main begins... */
char c;
// loop until done or until we have 50000
while(wordnumber < 49999)
{
c = getc(file);

if(c == EOF)
{
break;
}
else
{
/* code keeps going */

Function fgetc(file) returns type int. The macro EOF expands to
type int. So c should be type int, not char.

What type is wordnumber? If it is type int you should be aware
that 49999 may exceed the maximum value of an int. If the type is
unsigned int or a larger integral type, then you are safe.
 
J

Jack Klein

Hello,

I have compiled the following for Linux Intel and Windows, and the
loop breaks correctly at EOF, but the same code for the StrongARM
processor does not break correctly (keeps going until max number is
reached). What gives? What am I doing wrong? I tried both getc and
fgetc, same result.

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ... main begins... */
char c;
// loop until done or until we have 50000
while(wordnumber < 49999)
{
c = getc(file);

if(c == EOF)
{
break;
}
else
{
/* code keeps going */

Help will be greatly appreciated!

Kirst

As others have said, getc() returns an int, and for EOF it returns an
int value less than 0 and not equal to any valid char value.

And on all the ARM implementations I know of, ''plain'' char is
unsigned, not signed.
 
K

Keith Thompson

I have compiled the following for Linux Intel and Windows, and the
loop breaks correctly at EOF, but the same code for the StrongARM
processor does not break correctly (keeps going until max number is
reached). What gives? What am I doing wrong? I tried both getc and
fgetc, same result.

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ... main begins... */
char c;
// loop until done or until we have 50000
while(wordnumber < 49999)
{
c = getc(file);

if(c == EOF)
{
break;
}
else
{
/* code keeps going */

This is question 12.1 in the C FAQ. (Google "C FAQ" to find it.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,145
Messages
2,570,828
Members
47,374
Latest member
anuragag27

Latest Threads

Top