simple question

This is a discussion on simple question within the C Programming forums, part of the General Programming Boards category; im reading a book and i got to the point where i came accross this bit of code and they ...

  1. #1
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32

    simple question

    im reading a book and i got to the point where i came accross this bit of code and they dont expain it good and ive seen it before.
    Code:
    while ( scanf ("%d", &exp ) == 2);
    what i want to know is what that means in regards to if its equal to 2. i have seen it used as ==0 or ==1. what does 1 ,2 ,3 mean?

  2. #2
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    scanf returns an integer, either the number of values read in, or EOF if an end of file is reached. EOF is a special termination character, specified in stdio.h, which designates the end of a file. If no values are successfully read, scanf returns 0.

    taken from http://www.geocities.com/learnprogra...n4Beginner.htm
    It is not who I am inside but what I do that defines me.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    In addition, C99 states,
    The scanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    ok so if its == 1 than scanf is reading in one value and if it correctly reads in than it exits the while loop?
    Code:
    while ( scanf ("%d", &exp ) == 1);
    or 2 values returned true
    Code:
    while ( scanf ("%lf %d", &x , &exp ) == 2);
    is my thinking correct?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by tat
    ok so if its == 1 than scanf is reading in one value and if it correctly reads in than it exits the while loop?
    Code:
    while ( scanf ("%d", &exp ) == 1);
    is my thinking correct?
    No, the loop continues while the condition is true.

    While you are successfully reading text from the stdin into an integer, the loop will continue. When you fail to successfully read text from the stdin into an integer, the loop breaks.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    if you want to test if a double value was successfully stored into x and an int value was stored into exp, then your thinking is correct. or so i think so... try experimenting so that u'll know for urself.

    also it's like this:

    Code:
     while ( scanf ("%d", &exp ) == 1);
    can be translated to:
    Code:
    int flag=0;
    flag=scanf("%d",&exp);
    while(flag==1)
       flag=scanf ("%d", &exp );
    Last edited by sangken; 10-18-2006 at 09:26 PM.
    It is not who I am inside but what I do that defines me.

  7. #7
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Quote Originally Posted by Dave_Sinkula
    No, the loop continues while the condition is true.

    While you are successfully reading text from the stdin into an integer, the loop will continue. When you fail to successfully read text from the stdin into an integer, the loop breaks.
    ok that makes sense but im confused by what the one means then?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    From the man page:
    RETURN VALUES
    These functions return the number of input items assigned, which can be
    fewer than provided for, or even zero, in the event of a matching fail-
    ure. Zero indicates that, while there was input available, no conver-
    sions were assigned; typically this is due to an invalid input character,
    such as an alphabetic character for a `%d' conversion. The value EOF is
    returned if an input failure occurs before any conversion such as an end-
    of-file occurs. If an error or end-of-file occurs after conversion has
    begun, the number of conversions which were successfully completed is
    returned.
    You have one conversion inf your scan set, so you check to see that one item was stored successfully.


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

  9. #9
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    correct me if im wrong but i think the number 1 indicates that there were "1" successful conversions and successful "storing" of that converted value into the variable exp.

    that purpose of that test for equality is to determine if what you intended to store into those variables were indeed successfully converted and stored. else, there was a problem storing.
    It is not who I am inside but what I do that defines me.

  10. #10
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32

    Smile

    thank you quzah and sangken & everyone else!
    i totally understand now =)

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, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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