a simple question

This is a discussion on a simple question within the C Programming forums, part of the General Programming Boards category; say i have a statement: Code: while (c = fgetc(in) != EOF) { .... } where in is some file ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    a simple question

    say i have a statement:

    Code:
    while (c = fgetc(in) != EOF) { .... }
    where in is some file pointer. will "c=fgetc(in)" evaluate to a true/false expression? or will that expression reflect the value of fgetc(in)? i read somewhere that something like:
    Code:
    a = b = c = d = 0;
    only works when you're using a zero. is this right?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    will "c=fgetc(in)" evaluate to a true/false expression? or will that expression reflect the value of fgetc(in)?
    The expression ends up comparing the value stored in 'c' with the EOF check.

    Basicly it's like doing:
    c = fgetc( in );
    if( c != EOF ) ...

    i read somewhere that something like:

    code:
    a = b = c = d = 0;


    only works when you're using a zero. is this right?
    Incorrect.

    This is a simple assignment. Take the right most value, assign it to the item to the left. Take the value in d, assign it to c, take the value of c...

    The end result is that whatever is on the right most side, everything to the left of it will be set to that value (whatever that value is).

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    k, thanks

  4. #4
    hemalatha
    Guest

    Exclamation

    abt what i should reply

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by hemalatha
    abt what i should reply
    hhhmmmm !!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > while (c = fgetc(in) != EOF) { .... }
    Remember your precedence rules

    As written, this is
    while ( c = (fgetc(in) != EOF) ) { .... }
    Which will loop through the file until EOF, but c will always be 1 (ie true, because the result of fgetc was not EOF)

    Normally, you want to do this
    while ( (c = fgetc(in)) != EOF ) { .... }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21