#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    61
    Rep Power
    12

    Question with scanf


    Hi there,

    Just another stupid question,

    What does the following code mean?

    ........
    int v;
    .......

    while (scanf("%d,&v) == 1) {
    ......
    }

    What exactly does "1" mean here?

    Thanks in advance!
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    often in c questions are asked and the answers come as 0 for no or 1 for yes. also it's 0 for no and 1 to whatever, 1 and anything higher, means yes.

    for example this is an infinate loop
    Code:
    while(1) {
    	/* code */
    }
    while asks the question about what's in the brackets and the answer is always posative in this case. (the loop could be got out by someother way like break or return or something). also, instead of the 1 you could have any number above 1 in the brackets and that would be exactly the same - a posative, yes, as far as the while test is concerned.
    Code:
    while(0) {
    	/* code that'll never ever get run */
    }
    whereas that while's question would never get answered yes. always no, so it's loop would never be entered into (you'd never see this while loop ever - it's silly)

    often you'd have a variable instead of a constant number. in your bit of code instead of 1 or a variable, there's a function call. function calls have results - if they return something that is. if they're defined like:
    void function(int variable)
    then they don't return an answer - void being the key bit there. obviously scanf is a predefined function and it must return a 0 to indicate nothing or a failure or something, and 1 to indicate success.

    i'm pretty sure you could write that line like this (unless scanf returns higher numbers and that is being used, but i don't think that is the case):
    Code:
    while (scanf("%d,&v)) {
    	...  /* gets run if scanf return result is 1 or above */
    }
    same thing, and shorter so probably better.

    so that while line with scanf is calling scanf before the while loops test is answered, and the return result of scanf is used for the while loop test. if the return is 1 (or higher as i've just made it without the == 1) the while loop's code gets run. if the scanf return value is 0 then the while loop's code is not run.

    the scanf's return result is another value, different, although connected/dependent, to the v value.

    so basically the while is looking for a 0 or a 1(or higher). the question that's being asked in your code is does the result of scanf equal 1? and the answer to that question is 1/yes if it is equal or 0/no if it isn't equal.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    61
    Rep Power
    12
    Thanks Balance,
    I see, so in C, ==1 means YES and ==0 means NO, Boolean.

    Well, I am actually looking for something else.
    I wanna make my program to scan for digits and termindate when I press ^D. Here, I assume that when I press ^D, it will automatically terminates, right?

    Cheers,
    I appriciate your help!

    Alianto
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    You might want to read the man pages to see what the return values of scanf() actually mean. Here's one place:
    http://www.openbsd.org/cgi-bin/man.c...86&format=html

    Note that it returns the number of variables that were assigned by the scanf function. Let's say you asked scanf to read one integer value and the user types a string instead, then scanf will return 0, because it couldn't assign the integer value properly from the user's input. However, if you asked it to read a string instead, then it would return 1, because it could process the user's input as a string. If you asked scanf() to read two integers and the user did type 2 integers, then scanf() will return 2. So if you told the code to read three integers and the user typed only two integers and a string instead of an integer, you would know that the user only typed two integers, by reading the returned value (i.e. 2).

    >> I see, so in C, ==1 means YES and ==0 means NO, Boolean.
    Not quite -- 0 means NO in C and non-zero (not just 1 alone, but any non-zero number) means YES.
    Last edited by Scorpions4ever; April 6th, 2003 at 06:23 PM.
  8. #5
  9. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    Well, I am actually looking for something else.
    I wanna make my program to scan for digits
    that's what

    scanf("%d",&v);

    does on it's own. puts an int number value into v. you were asking about the ==1 bit.

    and termindate when I press ^D
    that'll also happen. scanf without the while will do all that. the while allows you to further handle it: "on successfully getting a number from input, do something, otherwise don't do that something"

    stick this in and type in various things:
    Code:
    #include <stdio.h>
    
    main()
    {
    	int v;
    	int i;
    	
    	i = scanf("%d",&v);
    	
    	printf("%d  <scanf's *return* value\n%d  <int v's value\n", i, v);
    }
    when that above code outputs a number like -1881066684 that means the variable hasn't been set.

    the fact that scanf also returns something (as well as putting a number into v) can be used or ignored - it's an added feature. it's up to you if you make use of it or not.
    Last edited by balance; April 7th, 2003 at 09:44 AM.

IMN logo majestic logo threadwatch logo seochat tools logo