Question:
Unresolved: C Language: Array Size, String doubt?
Reddy
2011-11-21 07:12:56 UTC
Earlier I have already posted this question but i am not satisfied with the answers:

Program 1: When i am running the following program and entering more than one character then it is displaying error.

--------------------------------
"Obviously because of the Array Size" but how the Second program is working?
--------------------------------

#include
void main()
{
char ch[1];

printf("\n enter a character to print:");
gets(ch);

printf("\n entered character is: %s", ch);

}

--------------------------------

Program 2: Compared to the above program how it is working fine?

how the variable is taking more than one character?


#include
void main()
{
char ch[1];

while ((ch[0])!='#')
{
printf("\n enter a character to print [enter # to stop]:");
scanf("%s",ch);

printf("\n entered character is: %s", ch);
}
}
Four answers:
oops
2011-11-21 07:25:47 UTC
What was the temperature when you ran the program? That could have an effect.



Seriously though, what you are doing, in both cases, is undefined behavior. When you invoke undefined behavior, anything can happen, including your program appearing to work correctly. This doesn't mean your program is okay. A program that has undefined behavior is always wrong, even if it appears to be right.



Never use gets. And never use an unadorned "%s" in a scanf format string. Make sure you always put a limit on it:



char arr[10];

scanf("%9s",arr);



But since all you seem to want is a single char, why are you using the %s format specifier anyway? You should be using %c.



@Reddy

"Here the question is "why the program is behaving like this when the size of the array is very less"?"



I answered that question in the paragraph about undefined behavior. Re-read "anything can happen". There are no requirements on the behavior of either of those programs. I looked at the other question where you weren't satisfied with the answers, and "One That Leads" said the same thing. Why are you not satisfied with the correct answer?





"To my perception both User's answers appear to be different, can i conclude that "there is a bug in "scanf" function"?"



One that Leads said it's undefined behavior, that's what I'm referring to. No, there's not a bug in the scanf function, it does exactly what it's documentation promises. You, the programmer, must be careful to make sure your arrays have enough room for any possible amount of data that scanf might put into them. If your arrays don't have enough space, then scanf will write into memory that is not owned by your program. It's undefined behavior for it to do this, but the fact that it will do this is perfectly documented, so it's up to you to make sure it doesn't happen. If you use "%s" without a limit like "%10s", then you are allowing the user to determine whether your program will exhibit undefined behavior.



"Considering the words "scanf isn't type safe" or my "compiler" sucks?"



Neither of those things, see the above point. When I said that One that Leads said the same thing, I was referring to when he called what you're doing undefined behavior. However, those statements "scanf isn't type safe", and "your compiler sucks", both of them are true( you're using Turbo C++, right? yes, it's terrible). But neither of those points are relevant to the current situation. Even though Turbo C++ does suck, I'm still pretty sure it does scanf right. At least, nothing you have said indicates it is doing something wrong in this situation.



"So a While-loop confuses the Compiler to accept more than one character?"



I wouldn't say the compiler is confused here. Inside scanf, there is a loop that reads characters from standard input. You didn't tell it to only read one character, so it just keeps reading until it comes to a space. All scanf sees is a pointer, it has no idea what size the array is.



"When i am executing the first program and entering more than one character then it is saying "General Protection Exception"

but not in the second program why?"



How many times do I need to say this?



Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard. Undefined Behavior. Anything can happen. The behavior is not dictated by the C standard.



Get it?
Gautham Reddy
2011-11-21 16:28:40 UTC
@oops and Will H:



Here the question is "why the program is behaving like this when the size of the array is very less"?



How it is able to take more than one character in the Second Program? when the actual size is [1]?

-----------



@Oops said:



1. what you are doing, in both cases, is undefined behavior. When you invoke undefined behavior, anything can happen, including your program appearing to work correctly. This doesn't mean your program is okay. A program that has undefined behavior is always wrong, even if it appears to be right.



2. Never use gets. And never use an unadorned "%s" in a scanf format string. Make sure you always put a limit on it:



3. "One That Leads" said the same thing. Why are you not satisfied with the correct answer?



======



"One That Leads" said:



I think what the second program is doing is overwriting storage it shouldn't be. scanf isn't type safe anyways.

Another possibility is that your compiler sucks, if so you should upgrade.

======



My Reply:

--------------



1. To my perception both User's answers appear to be different, can i conclude that "there is a bug in "scanf" function"?



2. Considering the words "scanf isn't type safe" or my "compiler" sucks?



3. So a While-loop confuses the Compiler to accept more than one character?



thank you.
2011-11-24 14:31:08 UTC
@Oops:



1. so it's up to you to make sure it doesn't happen. If you use "%s" without a limit like "%10s", then you are allowing the user to determine whether your program will exhibit undefined behavior.



2. Inside scanf, there is a loop that reads characters from standard input. You didn't tell it to only read one character, so it just keeps reading until it comes to a space. All scanf sees is a pointer, it has no idea what size the array is.

-----------



Reply:



When i am executing the first program and entering more than one character then it is saying "General Protection Exception"



but not in the second program why?





In the first program it is "Accepting a string but not displaying where as in the second program "it is both Accepting and Displaying Back" why?
Will H
2011-11-21 15:32:55 UTC
Do not use th gets() function. It does not know how many characters can be safely stored in the string passed to it. Thus, if too many are read, memory will be corrupted. Many security bugs that have been exploited on the Internet use this fact! Use the fgets() function instead (and read from stdin). But remember that unlike gets(), fgets() does not discard a terminating \n from the input.



The scanf() functions can also be used dangerously. The %s format can overwrite the destination string. However, it can be used safely by specifying a width. For example, the format %2s will not read more than 2 characters.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...