C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 6th, 2003, 09:38 AM
Alianto Alianto is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 61 Alianto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 2 sec
Reputation Power: 6
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!

Reply With Quote
  #2  
Old April 6th, 2003, 10:35 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #3  
Old April 6th, 2003, 10:43 AM
Alianto Alianto is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 61 Alianto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 2 sec
Reputation Power: 6
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

Reply With Quote
  #4  
Old April 6th, 2003, 05:59 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 h 5 m 45 sec
Reputation Power: 797
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....386&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.

Reply With Quote
  #5  
Old April 7th, 2003, 09:27 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
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.

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Question with scanf


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway