C newbie problem

J

Junmin H.

hello, i just started to learn C. i have got some problems:

1)
#include <stdio.h>

/* copy input to output; 1st version */

main(){
int c;
while(c = getchar() != EOF){
putchar(c);
}

}

why can "c = getchar() != EOF " can use as an argument of the loop
while()???

c = getchar() != EOF

is equivalent to

c = ( getchar() != EOF )

is it returning an ture or false???



2)


#include <stdio.h>

/* copy input to output; 1st version */

main(){
int c;
while((c = getchar()) == EOF){
printf("%d ",c);
//putchar(c);
}

}

why it doesnt output the value of EOF???

thanks
 
M

Miguel Guedes

Junmin said:
hello, i just started to learn C. i have got some problems:

1)

You are assigning the boolean result from <getchar() != EOF> and not the char
as read by getchar().

Try instead:

while((c = getchar()) != EOF){
why can "c = getchar() != EOF " can use as an argument of the loop
while()???

c = getchar() != EOF

is equivalent to

c = ( getchar() != EOF )

is it returning an ture or false???

Yes it is but I think you want to assign the char as read by getchar() to
variable c?
2)


#include <stdio.h>

/* copy input to output; 1st version */

main(){
int c;
while((c = getchar()) == EOF){
printf("%d ",c);
//putchar(c);
}

}

why it doesnt output the value of EOF???

It does. If you only enter CTRL-Z (in Windows) or CTRL-D (*nix) you get -1
output. Typing something else results in no output.
 
M

Martin Ambuhl

Junmin said:
hello, i just started to learn C. i have got some problems:

1)


why can "c = getchar() != EOF " can use as an argument of the loop
while()???

You can, but you need to know what you mean by it. If you are not
willing to learn the grammar completely enough, use parentheses:
while((c = getchar()) != EOF){
or
while((c = (getchar() != EOF))){
depending on what you mean.

#include <stdio.h>

/* copy input to output; 1st version */

main(){
int c;
while((c = getchar()) == EOF){
printf("%d ",c);
//putchar(c);
}

}

why it doesnt output the value of EOF???

If your input signals EOF before anything else, it does. Forever.
If your input has anything before signalling EOF, then, obviously, c !=
EOF and the while condition is false, so the program does nothing.

You are mixing two different versions of C, by the way.
Before C99, main() would implicitly be declared int, but then you would
need something equivalent to 'return 0;':

#include <stdio.h>
main(void){
int c;
while((c = getchar()) == EOF)
printf("%d ",c);
return 0; /* note */
}

From C99, main needs to be explicitly declared to return an int, but
the return statement, still a good idea, is not needed, the compiler
providing its equivalent:

#include <stdio.h>
int /* note */ main(void){
int c;
while((c = getchar()) == EOF)
printf("%d ",c);
return 0; /* not needed, but a good idea */
}

Your avoidance of typing has led you to write a program without defined
behavior under either the old or new standard.
 
M

Malcolm McLean

Junmin H. said:
hello, i just started to learn C. i have got some problems:

1)


why can "c = getchar() != EOF " can use as an argument of the loop
while()???
This is C's terseness. Generally thinking is moving away from writing a very
compact C style. Older code would often try to do as much as possible with a
single expression, more modern code tends to focus on readability.
However one exception is a loop to extract characters from a file. You want
to keep on extracting characters until you hit an EOF, so code is

get:
c = getcchar();
if(c == EOF)
goto inputend
/* any processing here */
printf("Read a %c\n", c);
goto get;
inputend:
printf("That's all the input\n");

those gotos are awkward. By writing

while( (c = getchar()) != EOF)
{
/* process c here */
}

we've made the flow a lot easier to see, at the cost of a little bit of
extra complexity in the loop condition.
The parentheses are important. First getchar() is called, and the result
assigned to c. Then c is tested against EOF. Only if c is not EOF is the
main body of the loop entered. The expression c = foo() has the value c.
That's one of the quirks of C. The condition will be repeatedly called on
each iteration of the loop, and so you will iterate over all the characters
in the input stream, excluding the EOF flag.
 
B

Bart van Ingen Schenau

Junmin said:
hello, i just started to learn C. i have got some problems:

1)


why can "c = getchar() != EOF " can use as an argument of the loop
while()???

c = getchar() != EOF

is equivalent to

c = ( getchar() != EOF )

is it returning an ture or false???

That depends on the input to the program.
If getchar() can read a character from the standard input stream
(typically associated with the keyboard), then that character will, by
definition, compare not equal to EOF. Thus the !=-operator yields the
value 1, which is stored in the variable c and is also used to control
the while-loop.

For a very long time, C never had a boolean type. Loops and conditional
statements are controlled by integer values, where the value 0 is
treated as 'false' and any non-zero value as 'true'.
2)


#include <stdio.h>

/* copy input to output; 1st version */

main(){
int c;
while((c = getchar()) == EOF){
printf("%d ",c);
//putchar(c);
}

}

why it doesnt output the value of EOF???

Most likely, there was some input available to the program, which caused
the loop to terminate before the first iteration.
In your loop test, you say you want to continue as long as the getchar()
function signals an End-Of-File condition. The loop will end as soon as
getchar() returns something else.

Bart v Ingen Schenau
 
J

Junmin H.

Miguel said:
You are assigning the boolean result from <getchar() != EOF> and not the char
as read by getchar().

Try instead:

while((c = getchar()) != EOF){


Yes it is but I think you want to assign the char as read by getchar() to
variable c?


It does. If you only enter CTRL-Z (in Windows) or CTRL-D (*nix) you get -1
output. Typing something else results in no output.
have tried what you said. it outputs -1. but why this happens when i
only enter CRTL-D??? and when typing any else results in no output???

thx.
 
J

Junmin H.

Martin said:
You can, but you need to know what you mean by it. If you are not
willing to learn the grammar completely enough, use parentheses:
while((c = getchar()) != EOF){
or
while((c = (getchar() != EOF))){
depending on what you mean.



If your input signals EOF before anything else, it does. Forever.
If your input has anything before signalling EOF, then, obviously, c !=
EOF and the while condition is false, so the program does nothing.

You are mixing two different versions of C, by the way.
Before C99, main() would implicitly be declared int, but then you would
need something equivalent to 'return 0;':

#include <stdio.h>
main(void){
int c;
while((c = getchar()) == EOF)
printf("%d ",c);
return 0; /* note */
}

From C99, main needs to be explicitly declared to return an int, but
the return statement, still a good idea, is not needed, the compiler
providing its equivalent:

#include <stdio.h>
int /* note */ main(void){
int c;
while((c = getchar()) == EOF)
printf("%d ",c);
return 0; /* not needed, but a good idea */
}

Your avoidance of typing has led you to write a program without defined
behavior under either the old or new standard.
i am just following the 2nd edition book of Dennis M. Ritchie ... i
think this because i am in the beginning of the book, so ... and i think
inside the book it uses the old standard. anyway, thanks for your
remind. by the way, should i learn the new standard later? o right now??
 
M

Miguel Guedes

Junmin said:
have tried what you said. it outputs -1. but why this happens when i
only enter CRTL-D??? and when typing any else results in no output???

Because typing CTRL-D has a special meaning and that is EOF, which is how you
signal a program you've finished entering input from the keyboard. EOF is short
for End-Of-File and is defined as -1.

You don't get any "other results" when you type something other than CTRL-D
because of how you devised the loop. Remember you're looping _while_ it is EOF:

while((c = getchar()) == EOF){


So in short, your program is looping while the user is saying "I have no more
input" (EOF). Maybe you've designed the loop wrongly? (or you're seeking to
answer a textbook question? :))
 
J

Junmin H.

Bart said:
That depends on the input to the program.
If getchar() can read a character from the standard input stream
(typically associated with the keyboard), then that character will, by
definition, compare not equal to EOF. Thus the !=-operator yields the
value 1, which is stored in the variable c and is also used to control
the while-loop.

For a very long time, C never had a boolean type. Loops and conditional
statements are controlled by integer values, where the value 0 is
treated as 'false' and any non-zero value as 'true'.

now C has boolean type already??
 
J

Junmin H.

Miguel said:
You are assigning the boolean result from <getchar() != EOF> and not the char
as read by getchar().

Try instead:

while((c = getchar()) != EOF){


Yes it is but I think you want to assign the char as read by getchar() to
variable c?


It does. If you only enter CTRL-Z (in Windows) or CTRL-D (*nix) you get -1
output. Typing something else results in no output.
GOT IT THE CAUSE. HEHE. THANKS.
 
K

Keith Thompson

Junmin H. said:
Bart van Ingen Schenau wrote: [33 lines deleted]
For a very long time, C never had a boolean type. Loops and
conditional
statements are controlled by integer values, where the value 0 is
treated as 'false' and any non-zero value as 'true'.

now C has boolean type already??
[32 lines deleted]

When you post a followup, please trim the quoted material to exclude
anything that's not relevant to your reply.

Yes, the C99 standard added a boolean type called '_Bool' (the name
was chosen to avoid breaking any old code that uses "bool" as an
identifier). If you include the new standard header <stdbool.h>,
you'll get 'bool', 'true', and 'false' defined as macros.

Note that the C99 standard is not widely implemented.

The comp.lang.c FAQ is at <http://www.c-faq.com/>; section 9 discusses
Boolean expressions and variables.
 
R

Richard Heathfield

Martin Ambuhl said:

You are mixing two different versions of C, by the way.
Before C99, main() would implicitly be declared int, but then you
would need something equivalent to 'return 0;'

Whilst I agree with you, it must nevertheless be pointed out that your
criticisms apply equally to some of the example programs in K&R2.
 
N

Nick Keighley

Please try to post in standard english as far as you can. It makes
your posts easier to understand. Capitalise "I". Sentences are not
normally terminated with "...".
i am just following the 2nd edition book of Dennis M. Ritchie ...

this book is written in (very close) to the 1989 standard. For maximal
portability stick with this standard. I wouldn't omit the return
even if I were writing to the 1999 standard.
i
think this because i am in the beginning of the book, so ... and i think
inside the book it uses the old standard. anyway, thanks for your
remind. by the way, should i learn the new standard later? o right now??

the 1989 is still widely used. Few compilers are yet fully compliant
with the 1999 standard. 1989 will take you a long way.



--
Nick Keighley

Dan Pop: "When was the last time you've implemented a real life
application as a strictly conforming program?"
Richard Heathfield: "About 20 minutes ago. It was a new, heavily
optimised pig-launching routine, which gets us a 70% range increase
on previous porcine aeronautic programs."
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top