|
||||||
| C Tutorials All C Tutorials and Code |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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. |
| Sponsored Links |
|
|
|
|||||
|
Actually a nice little tutorial, I like your coding style, it's clean and the layout of the tutorial is too. Good job.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
|
|||||
|
Quote:
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
|
|||
|
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);
}
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); |
|
|||
|
it is a perfect tutorial for rookies
__________________
www.dynamsoft.com – the leading developer of version control and issue tracking software provider |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| Xav | ........ | 160.68 |
| neerlin | ........ | 100 |
| satrian | ........ | 100 |
| delia | ........ | 100 |
| chili5 | ........ | 70.08 |
| morefood2001 | ........ | 36.91 |
| MeTh0Dz|Reb0rn | ........ | 28.44 |
| RyanTuosto | ........ | 20 |
| gamiR | ........ | 19.64 |
| John | ........ | 14.46 |
Goal: 100,000 Posts
Complete: 68%