There are numerous problems with this code. We'll fix the badly named variables and functions and investigate the problems:. The function CharToInt [sic. It doesn't check if the user accidentally passes in a NULL pointer. It doesn't validate input, or more correctly, skip invalid input. If the user enters in a non-digit the result will contain a bogus value. Next, the nondescript temp in CharToInt [sic. Likewise MAX is badly named since it may appear to conflict with the standard usage.
There is no need to use and clutter up the code with yet another temporary index i. This is bad because it can overflow the input string buffer. For example, if the buffer size is 2, and you enter in 16 characters, you will overflow str. You mention " when using scanf function, the result is completely wrong because first character apparently has a ASCII value. This is safe because you can guarantee you never overflow the input string buffer by passing in the buffer size which includes room for the NULL.
Lastly, this implementation is buggy in that it doesn't detect integer overflow. If the user enters too large a number the number may become negative! Let's fix that too. This is trivial to fix for an unsigned int. If the previous partial number is less then the current partial number then we have overflowed and we return the previous partial number. For a signed int this is a little more work. In assembly we could inspect the carry-flag, but in C there is no standard built-in way to detect overflow with signed int math.
Since we don't want to assume how many bits an int has we only need to test if the top 3 msb's Most Significant Bits are set. Additionally, a second overflow test is needed.
If the msb is set sign bit after the digit concatenation then we also know the number overflowed. Here is a fixed safe version along with code that you can play with to detect overflow in the unsafe versions.
You're correct that you should never use gets. If you want to use fgets , you can simply overwrite the newline. This does assume there are no embedded NULLs.
The advantage to getline is it does allocation and reallocation for you, it handles possible embedded NULLs, and it returns the count so you don't have to waste time with strlen. Note that you can't use an array with getline.
The pointer must be NULL or free-able. If your string array is of size and i enter characters, i can buffer overflow your program. It essentially validates the input digits and ignores anything else. This is very crude so modify it and salt to taste. So I am not much of a programmer but let me try to answer your question about the scanf ;. I think the scanf is pretty fine and use it for mostly everything without having any issues.
But you have taken a not completely correct structure. It should be:. It tells the program where in which variable to save the scanned value. How are we doing?
Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. No not nice at all.
What happens when you have different length strings? A different macro for each of these different sizes? That indeed looks a bit messy. But to answer the question, isn't it the case with the fgets too, that you need to set the buffer size limit. Which I believe is same with the scanf anf fgets. Page 1 of 2 1 2 Last Jump to page:. Similar Threads fgets and scanf By kkk in forum C Programming. Replies: 10 Last Post: , AM. Replies: 4 Last Post: , PM.
By Matus in forum C Programming. Replies: 65 Last Post: , PM. Replies: 2 Last Post: , PM. All times are GMT The time now is AM. Now, study the following program :. In line 4, the fgets function is used to take input from the file, which will print the string up to 20 characters. Although scanf is mostly used to take user input, it is preferable to use fgets while taking string inputs as it makes the program error-free. Your email address will not be published.
Skip to content In this article, we will take a look at two of the most basic functions used in the C language and subsequently, we will compare them on the basis of their usage.
Welcome to The Crazy Programmer. Welcome to The Crazy Programmer! Hello World!
0コメント