C newbie problem

Discussion in 'C Programming' started by Junmin H., Sep 10, 2007.

  1. Junmin H.

    Junmin H. Guest

    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
     
    Junmin H., Sep 10, 2007
    #1
    1. Advertising

  2. 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
    >
     
    Miguel Guedes, Sep 10, 2007
    #2
    1. Advertising

  3. 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.
     
    Martin Ambuhl, Sep 10, 2007
    #3
  4. "Junmin H." <> wrote in message
    news:46e4e3e8$0$16270$...
    > 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
     
    Malcolm McLean, Sep 10, 2007
    #4
  5. 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/
     
    Bart van Ingen Schenau, Sep 10, 2007
    #5
  6. Junmin H.

    Junmin H. Guest

    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
     
    Junmin H., Sep 11, 2007
    #6
  7. Junmin H.

    Junmin H. Guest

    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
     
    Junmin H., Sep 11, 2007
    #7
  8. 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? :))
     
    Miguel Guedes, Sep 11, 2007
    #8
  9. Junmin H.

    Junmin H. Guest

    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
     
    Junmin H., Sep 11, 2007
    #9
  10. Junmin H.

    Junmin H. Guest

    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
     
    Junmin H., Sep 11, 2007
    #10
  11. "Junmin H." <> writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 11, 2007
    #11
  12. Martin Ambuhl said:

    <snip>

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

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 11, 2007
    #12
  13. On 11 Sep, 00:44, "Junmin H." <> wrote:
    > Martin Ambuhl wrote:
    > > Junmin H. wrote:


    <snip>

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



    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."
     
    Nick Keighley, Sep 11, 2007
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Id0x
    Replies:
    4
    Views:
    1,210
    Erik Max Francis
    Jul 21, 2003
  2. JohnE

    newbie with newbie questions

    JohnE, Aug 17, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    525
    Gregory A. Beamer
    Aug 17, 2009
  3. Jerry C.
    Replies:
    8
    Views:
    263
    Uri Guttman
    Nov 23, 2003
  4. Kruno Saho
    Replies:
    0
    Views:
    157
    Kruno Saho
    Apr 7, 2013
  5. Dave Angel
    Replies:
    0
    Views:
    137
    Dave Angel
    Apr 7, 2013
Loading...

Share This Page