Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C newbie problem

Reply
Thread Tools

C newbie problem

 
 
Junmin H.
Guest
Posts: n/a
 
      09-10-2007
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

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Miguel Guedes
Guest
Posts: n/a
 
      09-10-2007
Junmin H. wrote:
> 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){


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

Try instead:

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???


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.

>
> thanks
>

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-10-2007
Junmin H. wrote:
> 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()???


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.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      09-10-2007

"Junmin H." <(E-Mail Removed)> wrote in message
news:46e4e3e8$0$16270$(E-Mail Removed).. .
> 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()???
>

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.



--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      09-10-2007
Junmin H. wrote:

> 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???


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.

>
> thanks
>


Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
 
Reply With Quote
 
Junmin H.
Guest
Posts: n/a
 
      09-10-2007
Miguel Guedes wrote:
> Junmin H. wrote:
>> 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){

>
> You are assigning the boolean result from <getchar() != EOF> and not the char
> as read by getchar().
>
> Try instead:
>
> 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???

>
> 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.
>
>> thanks
>>

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.

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Junmin H.
Guest
Posts: n/a
 
      09-10-2007
Martin Ambuhl wrote:
> Junmin H. wrote:
>> 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()???

>
> 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.

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??

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Miguel Guedes
Guest
Posts: n/a
 
      09-10-2007
Junmin H. wrote:
> Miguel Guedes wrote:
>> Junmin H. wrote:
>>> 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){

>>
>> You are assigning the boolean result from <getchar() != EOF> and not
>> the char
>> as read by getchar().
>>
>> Try instead:
>>
>> 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???

>>
>> 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.
>>
>>> thanks
>>>

> 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? )
 
Reply With Quote
 
Junmin H.
Guest
Posts: n/a
 
      09-10-2007
Bart van Ingen Schenau wrote:
> Junmin H. wrote:
>
>> 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???

>
> 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??
>
>> 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.
>
>> thanks
>>

>
> Bart v Ingen Schenau


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Junmin H.
Guest
Posts: n/a
 
      09-10-2007
Miguel Guedes wrote:
> Junmin H. wrote:
>> 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){

>
> You are assigning the boolean result from <getchar() != EOF> and not the char
> as read by getchar().
>
> Try instead:
>
> 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???

>
> 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.
>
>> thanks
>>

GOT IT THE CAUSE. HEHE. THANKS.

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie with newbie questions JohnE ASP .Net 3 08-17-2009 10:10 PM
VONAGE Newbie w/newbie question New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments