![]() |
| |||
| User Input: Strings and Numbers [C] Very early on when attempting to learn programming in C, you often do exercises that read in and print out strings and numbers. Now you might assume that C has a simple function that will get user input, and indeed it does, but there are a couple of gotchas that can easily trip up those new to programming in C. There are two things to vigorously avoid, and unfortunately they are all too commonly seen: char text[20]; So how do you do it right? The short answer is to use fgets to read a line of text. However, please note that when you enter text, you press the [Enter] or [Return] key and this character does not just vanish, it remains in the input buffer. For example: #include <stdio.h> Quote:
--- skimmers start here --- #include <stdio.h> Quote:
--- skimmers stop here --- If you are reading a number, then first read in the input string and consequently follow it with a call to sscanf or strtol to convert it to a number.#include <stdio.h> Quote:
gets. It is included as a standard library function only as a holdover from pre-C89 code (the standards committee opted in the interest of not breaking existing code). But it is inherently and notoriously unsafe because there is no way to tell it the size of the buffer into which data will be read. So it is always a risk for buffer overflow, which may be used as an exploit. If you ever want to be employed as a programmer, it's best not to ever let a potential employer see you use this function!As for scanf it is a truly complicated beast, and should certainly not be the first choice for someone just starting out programming in C. One of the first issues with scanf("%s", str); is it suffers from a very similar problem as gets, which is the incoming buffer size is not specified. If you didn't look up the description of %s you may not have known that this directive is whitespace-delimited. Which means that it if the user enters hello world, then str will be hello. There are more gotchas, such as if you are also using scanf to read integers, perhaps using scanf("%d", &i);, then the same whitespace issue may bite you. Remember that when you enter text you press the [Enter] or [Return] key and it remains in the input buffer. This can cause issues when mixed with other input functions such as getchar. There is also another insidious issue that comes up when using scanf: the "need" to flush the input buffer.And don't think I presented a comprehensive list of gotchas associated with scanf, these were merely some common ones! |
| ||
| Re: User Input: Strings and Numbers [C] if i want to print * character insted of printing letters while scaning it from keybord just like ATM bank password application what will be code for it in C whenever i type letter from keyboard i want to see it only ***** |
| ||
| Re: User Input: Strings and Numbers [C] This question unfortunately is both frequently asked and has nothing to do with standard C. Hopefully this may help you, though. |
| ||
| Re: User Input: Strings and Numbers [C] This tutorial's original ending: Quote:
|
| ||
| Re: User Input: Strings and Numbers [C] What is missing here is an example of a text entry followed by a numeric entry. I tried to combine the code presented and should the text exceed its length of 19 and have numbers at the end, ouch! |
| ||
| Re: User Input: Strings and Numbers [C] Why not post this failing example so I know what you mean? Brought up recently by tux4life and explained by Narue, the fgets/sscanf code is not bulletproof. I must admit, though, I don't quite follow this bit: Quote:
I suppose I should also mention, since I hadn't back in the day when I could edit the snippet, that this post got quite long. I posted other snippets that went a little bit further with this -- this being an initial start and not a be-all end-all. In fact, even the other linked snippets are not bulletproof and I believe at least a few mention this explicitly. But again, can you clarify your comment for me? |
| ||
| Re: User Input: Strings and Numbers [C] Ah, thanks the explanation by Narue explains it all in detail. Thanks again! |
| ||
| what typpes of questiions in the exam if it is the subject of B.Sc(hon's) computer scienece |
| ||
| Quote:
|
| All times are GMT -4. The time now is 6:24 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC