Compiler Error

B

Bill

I'm getting a syntax error on line 18 of this code and I don't see a
missing ";". Does anyone else see it?

#include < stdio.h>

void main()
{
FILE *fp;
int i;
int CharString;

fp = fopen("foo.dat", "w"); /* open foo.dat for writing */

fprintf(fp, "\nSample Code\n\n"); /* write some info */

for (i = 1; i <= 10 ; i++)
fprintf(fp, "i = %d\n", i);

fclose(fp); /* close the file */

fp = fopen("foo.dat", "r"); //THIS LINE CAUSES COMPILE ERROR,
"Syntax Error missing ;"

while(CharString = fgetc(fp)) != EOF)
putchar(CharString);

fclose(fp);

}
 
C

Chris Dollin

Bill said:
I'm getting a syntax error on line 18 of this code and I don't see a
missing ";". Does anyone else see it?
#include < stdio.h>

No such header: erroneous space between "<" and "s".
void main()

`int`. Not `void`.
fp = fopen("foo.dat", "r"); //THIS LINE CAUSES COMPILE ERROR,
"Syntax Error missing ;"

I think you're wrong about which line is causing the syntax
error ...
while(CharString = fgetc(fp)) != EOF)

Count the brackets in that line.
 
R

Roberto Waltman

Bill said:
I'm getting a syntax error on line 18 of this code and I don't see a
missing ";". Does anyone else see it?

#include < stdio.h>

Is a space legal there? ( "< stdio..." )
void main()

Should be int main()
{
FILE *fp;
int i;
int CharString;

fp = fopen("foo.dat", "w"); /* open foo.dat for writing */

fprintf(fp, "\nSample Code\n\n"); /* write some info */

for (i = 1; i <= 10 ; i++)
fprintf(fp, "i = %d\n", i);

fclose(fp); /* close the file */

fp = fopen("foo.dat", "r"); //THIS LINE CAUSES COMPILE ERROR,
"Syntax Error missing ;"

while(CharString = fgetc(fp)) != EOF)

Missing left parenthesis:
while((CharString = fgetc(fp)) != EOF)
putchar(CharString);

fclose(fp);

}

Did not get a missing ";" from gcc 3.4.4 under Cygwin.
Compiles OK after adding the missing parenthesis.

Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
D

Dik T. Winter

> I'm getting a syntax error on line 18 of this code and I don't see a
> missing ";". Does anyone else see it?

In general line numbers can be approximate. Look in the neighbourhood
and you will find this:
 
B

Bill

No such header: erroneous space between "<" and "s".


`int`. Not `void`.


I think you're wrong about which line is causing the syntax
error ...




Count the brackets in that line.

Aren't these types of problems ALWAYS stupid mistakes? Thanks! It
works. :)
 
R

Richard Tobin

Bill said:
I'm getting a syntax error on line 18 of this code and I don't see a
missing ";". Does anyone else see it?

No, but there is an error. Maybe the compiler is confused.
#include < stdio.h>
^
That space shouldn't be there, but maybe it's just a cut-and-paste error.
while(CharString = fgetc(fp)) != EOF)
^
You forgot an open parenthesis.

-- Richard
 
F

Fred Kleinschmidt

Bill said:
I'm getting a syntax error on line 18 of this code and I don't see a
missing ";". Does anyone else see it?

#include < stdio.h>

Error: Can't open include file ' stdio.h'.
Get rid of the extra space
void main()
{
FILE *fp;
int i;
int CharString;

fp = fopen("foo.dat", "w"); /* open foo.dat for writing */

fprintf(fp, "\nSample Code\n\n"); /* write some info */

for (i = 1; i <= 10 ; i++)
fprintf(fp, "i = %d\n", i);

fclose(fp); /* close the file */

fp = fopen("foo.dat", "r"); //THIS LINE CAUSES COMPILE ERROR,
"Syntax Error missing ;"

while(CharString = fgetc(fp)) != EOF)

Error: Unexpected symbol: "!=".
Too many end parentheses
 
K

Keith Thompson

Bill said:
I'm getting a syntax error on line 18 of this code and I don't see a
missing ";". Does anyone else see it?

#include < stdio.h>

void main()
{
FILE *fp;
int i;
int CharString;

fp = fopen("foo.dat", "w"); /* open foo.dat for writing */

fprintf(fp, "\nSample Code\n\n"); /* write some info */

for (i = 1; i <= 10 ; i++)
fprintf(fp, "i = %d\n", i);

fclose(fp); /* close the file */

fp = fopen("foo.dat", "r"); //THIS LINE CAUSES COMPILE ERROR,
"Syntax Error missing ;"

while(CharString = fgetc(fp)) != EOF)
putchar(CharString);

fclose(fp);

}

Ironically, the line immediately following the fopen() call has a
syntax error which could be corrected by adding a semicolon. That
line is:

"Syntax Error missing ;"

which is a valid string literal. Adding a semicolon after the closing
quotation mark would make it a legal (but useless) statement.

A better solution, of course, would be avoid using "//" comments in
Usenet posts. Long lines are often wrapped; in this case, the
wrapping created a syntax error.

Of course, the real error is the missing parenthesis on the while
loop.

You should add a "return 0;" at the end of your main function.

Stylistically, "CharString" is a nonsensical name for your variable.
(Of course, the compiler doesn't care.)
 
M

Martin Ambuhl

Bill said:
I'm getting a syntax error on line 18 of this code and I don't see a
missing ";". Does anyone else see it?

#include <stdio.h> /* mha: fixed name of header */

/* mha:superfluous comments doing nothing more than repeating the C
statements removed. */

int /* mha: fixed illiterate use of 'void'
for the return type of main */
main(void)
{
FILE *fp;
int i;
int CharString;

fp = fopen("foo.dat", "w");
fprintf(fp, "\nSample Code\n\n");
for (i = 1; i <= 10; i++)
fprintf(fp, "i = %d\n", i);
fclose(fp);

fp = fopen("foo.dat", "r");
while (( /* mha: fixed missing '(' */ CharString = fgetc(fp))
!= EOF)
putchar(CharString);

fclose(fp);

}
 
M

madhawi

i do get any error after adding ' ( ' in while loop.
the program would like to be :
#include <stdio.h>
void main()
{
FILE *fp;
int i;
int CharString;


fp = fopen("foo.dat", "w"); /* open foo.dat for writing */


fprintf(fp, "\nSample Code\n\n"); /* write some info */


for (i = 1; i <= 10 ; i++)
fprintf(fp, "i = %d\n", i);


fclose(fp); /* close the file */


fp = fopen("foo.dat", "r");


while((CharString = fgetc(fp)) != EOF)
putchar(CharString);


fclose(fp);
}

output : -

Sample Code

i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10

Press any key to continue
 
A

Andrew Poelstra

i do get any error after adding ' ( ' in while loop.
the program would like to be :
#include <stdio.h>
void main()
{
FILE *fp;
int i;
int CharString;


fp = fopen("foo.dat", "w"); /* open foo.dat for writing */


fprintf(fp, "\nSample Code\n\n"); /* write some info */


for (i = 1; i <= 10 ; i++)
fprintf(fp, "i = %d\n", i);


fclose(fp); /* close the file */


fp = fopen("foo.dat", "r");


while((CharString = fgetc(fp)) != EOF)
putchar(CharString);


fclose(fp);
}

Don't top-post. In this case the context wasn't particularly relevant,
so I removed it.

Your code has a number of problems:
1. It doesn't "want" to be anything. The philosophical reasons for that
are off-topic on this newsgroup.
2. main() returns int and has no parameters. Hence,
int main(void)
is preferred to "void main()"
3. main() returns int. Return an int. And make it 0, EXIT_SUCCESS, or
EXIT_FAILURE, with the latter two defined in <stdlib.h>
4. fopen() is fallible. Check for failure.
5. Loops go from 0 to n-1, not from 1 to n. That's more a style argument
than anything else, but is such an accepted idiom that non-conformance
merely obfuscates your code.
6. fgetc() returns a char, which you know, as you used putchar() to
output the character. Don't use a variable named 'CharString' for that.
7. getc() is potentially faster than fgetc(), and is therefore more
appropriate in this case.
 
K

Keith Thompson

Andrew Poelstra said:
6. fgetc() returns a char, which you know, as you used putchar() to
output the character. Don't use a variable named 'CharString' for that.

No, fgetc() returns an int; it's value is either the value of the
character read (interpreted as an unsigned char and converted to int)
*or* the value of EOF to indicate either and end-of-file condition or
an error. (The OP's code gets this right.)
 
C

Chris Dollin

(It's customary to snip peoples signatures in your response, however
witty the signature-writer might have thought them.)
Aren't these types of problems ALWAYS stupid mistakes?

Sometimes, they're subtle mistakes, and you waste yonks looking
for stupidity. Sometimes, they're really amazingly stupid, but
not as stupid as you feel when someone looks over your shoulder
and says "shouldn't that be `remove`, not `difference`?". Then
it turns out they're not even a programmer.
Thanks! It works. :)

Another day, another problem ...
 
C

CBFalconer

Keith said:
No, fgetc() returns an int; it's value is either the value of the
character read (interpreted as an unsigned char and converted to int)
*or* the value of EOF to indicate either and end-of-file condition or
an error. (The OP's code gets this right.)

I think AP was complaining about the naming of the variable,
although his verbiage is crying out for mis-interpretation.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
R

Richard Heathfield

Andrew Poelstra said:

2. main() returns int and has no parameters.

Or two.
Hence,
int main(void)
is preferred to "void main()"

"preferred"? I'd have used a stronger expression than that. The Standard
actually requires int in almost all situations in which the Standard
might reasonably be said to be relevant (although it works hard to make
people think otherwise).
 
A

Andrew Poelstra

I think AP was complaining about the naming of the variable,
although his verbiage is crying out for mis-interpretation.

Indeed I was, but I still used the word 'char' to indicate fgetc()'s
return type, which was unambiguously incorrect.
 
M

matevzb

Andrew Poelstra said:



Or two.


"preferred"? I'd have used a stronger expression than that. The Standard
actually requires int in almost all situations in which the Standard
might reasonably be said to be relevant (although it works hard to make
people think otherwise).
This is probably OT, but still:
The "void main()" problem is a sort of a magic circle (hopefully a
correct expression) as I see it. You see, things like that are still
"taught" at schools, e.g. at the university I attended. I thought it
would change over time, but it's gotten worse to say the least - the
list of errors I found by only glancing at the material is
overwhelming. Funnily, nowadays they even seem to instruct to "consult
the ISO standard", "read the comp.lang.c FAQ", but the teachers
haven't done it in the first place. Back in the days no one even
mentioned ANSI/ISO, and the only material I could afford were these
brain dead "lecture notes" (no, they weren't even free), with features
like:
- "main()" doesn't return anything, ever
- "void main()" or even "void main(void)"
- when describing gets() and puts(), they are mentioned as "char
*gets( char str[80])" (?!)
- using gets() whenever possible
- "#include < stdio.h >" (yes, a novelty - spaces on both sides)
- never check return values from malloc(), scanf(), fopen() etc.
- tips and tricks like "A fast and economic way to swap values" alias
the XOR thingie
- etc.
The offending material can be found at:
- http://colos1.fri.uni-lj.si/~sis/COMPUTING/C/HTML/index.htm
- http://colos1.fri.uni-lj.si/~sis/PREDMETI/P2/index.html
It's mostly non-English (and there's some Java included), but you'll
get the picture.
I just hope schools elsewhere put more effort into teaching something
rather than producing D.Sc.'s, M.A.'s and the likes.
 
S

santosh

matevzb said:
This is probably OT, but still:
The "void main()" problem is a sort of a magic circle (hopefully a
correct expression) as I see it. You see, things like that are still
"taught" at schools, e.g. at the university I attended. I thought it
would change over time, but it's gotten worse to say the least - the
list of errors I found by only glancing at the material is
overwhelming.
<snip>

Yes. Particularly irritating are the use of gets and fflush(stdin),
even from institutions that claim to be on the "forefront" of IT
technology. It's also sad that in recent years here, C has been
relegated to the level of Pascal or BASIC, something with which to
"learn the ropes", so to speak. It's often mildly ridiculed by people
who claim it's a antedeluvian relic from the past, unfriendly and
error prone, and language X[1], is soo much better.

[1] - The commonest candidate for X, is of course, Java.
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top