Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials

Vote on your favorite hash algorithm here!

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-09-2007, 11:45 PM
twitch twitch is offline
Newbie
 
Join Date: Jun 2007
Posts: 11
Credits: 0
Rep Power: 0
twitch is on a distinguished road
Default How to use scanf in C.

A lot of new C programmers have trouble with scanf(). So I'm going to show you how to do it

The main reason beginners have trouble is scanf works like a pointer so you have to point to what your getting input for

Code:
#include<stdio.h>

int main()
{
   int myvariable;

   printf("Enter a number:");
   scanf("%d",&myvariable);
   printf("%d",myvariable);
   
   return 0;
}

See when we used scanf we first declared what the variables type was
"%d" for int ,"%f" for float ,%e for double ,"%c" for char , "%s" for strings.

Then in the second part we have to use & like in a pointer to point to the variable instead of just getting its value Remember without & your program will likely crash

For handling strings with space and in files use fgets Here is a link to a tutorial on using it User Input: Strings and Numbers [C]

Last edited by twitch; 06-10-2007 at 04:53 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-10-2007, 01:34 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

Actually a nice little tutorial, I like your coding style, it's clean and the layout of the tutorial is too. Good job.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-10-2007, 07:47 AM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,241
Last Blog:
Passwords
Credits: 891
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default

Nice tutorial, but even though I know how to use scanf now, I have no idea what it is used for

When/Why is scanf() used?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-10-2007, 09:16 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

Quote:
Originally Posted by Sidewinder
Nice tutorial, but even though I know how to use scanf now, I have no idea what it is used for

When/Why is scanf() used?
You're using scanf() to get userinput in C, like you're using std::cin in C++.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-10-2007, 10:08 AM
rong889 rong889 is offline
Newbie
 
Join Date: Jun 2007
Posts: 2
Credits: 0
Rep Power: 0
rong889 is on a distinguished road
Smile

Nice tutorial.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 10-16-2007, 10:55 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 443
Last Blog:
Just over the next hil...
Credits: 45
Rep Power: 8
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

I'll comment on scanf since I think it's an awful function. Scanf expects data of the correct type. If you enter in the wrong data then it fails to work. Now you can catch this by catching the return code, scanf returns the number of items it read in so if it returns 0 you know that it failed on the first entry. You may think this appropriate

Code:
int check = 1;
int var = 0;
while(check){
    check = !scanf("%d", &var);
}
Since there's only one variable read, it will either return 0 on failure or 1 on success. The ! inverts this and check feeds it back into the while loop (so that this will loop until there is correct input).

You may think job done. Unfortunately scanf doesn't flush the input buffer on failure so if I enter 'error' it will stay on the input buffer for the next time, this program gives an infinite loop on an error.

So what can we do? We could call fflush(stdin); but this behaviour is undefined according to the C standard. Windows happens to define fflush on stdin but I've seen code elsewhere which doesn't work.

Personally I prefer to use fgets.
Code:
char buff[20];
fgets(buff, size, stdin);
and then parse the output. It certainly doesn't leave mess on stdin and is fully portable. Once you've got a set of functions written to do this you won't ever need to touch temperamental scanf again. The one thing to be careful of, fgets leaves the newline character on the end of the output so you will want to write a function that scans it and replaces it with the null character.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-16-2007, 11:11 PM
catherine sea catherine sea is offline
Newbie
 
Join Date: Dec 2007
Posts: 9
Credits: 0
Rep Power: 0
catherine sea is on a distinguished road
Default

it is a perfect tutorial for rookies
__________________
www.dynamsoft.com – the leading developer of
version control and issue tracking software provider
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 08:39 PM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1075.89
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 900.18
John ........ 890.77
Brandon W ........ 770.65
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 232.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads