C program stops accepting input at 1023 characters - c

I'm teaching myself C using K&R. Exercise 1-16 asks me to refactor some provided code to give the length of "arbitrarily long input lines".
Whilst working on the problem I found that my terminal ceases to accept input after 1023 characters; a very suspicious number I'm sure you'll agree!! I have tested on Mac OS X and OpenBSD and see the same behaviour. The program hasn't stopped responding because typing backspace and submitting the input works correctly.
I couldn't figure out how to debug this with gdb because the problem occurs during data entry, not after submission when stepping through with gdb.
I could see no reference to a limit in the getchar or bash manpages, and indeed it seems very little input anyway.
I reduced the problem to the following and see the same behaviour.
#include <stdio.h>
main()
{
int c,i=0;
while ((c=getchar()) != EOF && c!='\n')
++i;
printf("%d\n",i);
return 0;
}
Could people please explain:
Why this is happening
How I might debug this kind of issue myself
Many thanks.

As per the comments on my question, it would appear to be a terminal limitation. Piping a file into the program works as expected.

Related

C getchar() and EOF behavior [duplicate]

Why Ctrl+Z does not trigger the loop to finish on the following small program?
#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF)
{
//nothing
}
return 0;
}
If I enter: test^ZEnter, it does not get out of the loop.
I found related questions around (here and here) but none to explain it for C (not C++) under Windows.
Note: I use Visual Studio 2015 PRE on a windows 8.1
You need to hit Enter and then use ctrl+Z and then Enter again.
or, you may also use F6
EOF like you use it is not a character. It's the status in which that stream is.
I mean, heck, you even link this question, so you might as well read the accepted answer:
The underlying form of an EOF is a zero-length read.
It's not an "EOF character".
http://www.c-faq.com/stdio/getcharc.html cites a different case than yours, where someone stored the return value of getchar in a char. The underlying problem still occurs occasionally: different runtimes implement different values for the EOF integer (which is why I said, it's not an EOF character), and things love to go wrong. Especially in Visual C++, which is not a "real" C compiler but a C++ compiler with a compatibility mode, it seems things can go wrong.

inability to input sentences with white space

I'm trying to make the 20 questions game using c. The following is the code excerpt of one of the functions.
int question_input(void)
{
char q[100];
int i=0;
printf("Enter the question or say if you want to guess\n");
scanf("%[^\n]",q);
i=check_if_guess(q);
if(i==0)
{
printf("Say yes or no\n");
scanf("%s",q);
}
return i;
}
I get a general protection fault when I execute and on debugging, I found that the problem is with the
scanf("%[^\n]",q)
statement.
If the same statement is changed to "%s", then, I get no segmentation fault. Functions like gets (general protection fault) and fgets(doesn't ask for input at all) also fail to take inputs.
The thing which is more curious is that when I execute these statements in a seperate file, without the rest of the code, they execute properly.
Even if I try "%99[^\r\n]", it shows the same.
Please help
Well, I found an answer to my question by myself. One of my previous functions in my code had
scanf("%d",&n)
It is a known fact that after we enter our input we press the enter key. This is taken as a '\n' by the compiler and is stored in the buffer. My next scanf statement( the one which I got error in)(scanf("%[^\n]",q) straight away took the '\n' from the buffer. Hence it could not take any input, since \n terminates input. The compiler I use is turbo C++. In some cases of empty strings, it reports an error. Hence I got the error message. To solve this problem, I had to use
getchar()
statement before the scanf("%[^\n]",q) statement, so that the '\n' is removed from the buffer and taken as an input for the getchar statement.

Incomprehensible issue in a loop (C on Clion)

I'm learning C in my school for 2 months and I have to make a Halma's Game in the console, so I'm a newbie for now.
I use Clion and CodeBlock but I prefer Clion for his internal console.
The game work pretty good, but I've got 2 weird issues that I can't fix by myself:
- My loop is presumed to stop with the scanf,but it doesnt.
("saut" variable).
It work only in the 2nd(or more) passage in the loop.
-The variable "h" increment during the loop (in order to ignore the next condition when you are out the loop), but the program always get in.
I don't know why..
Check my code:
MY CODE (IN FRENCH DON'T WORRY ABOUT
Thanks for your help.
Robin.
On some platforms (especially Windows and Linux) you can use fflush(stdin);:
Do modify your code like :-
while(saut==1)
...
....
scanf("%d",&saut);
fflush(stdin);
}
fflush(stdin);
these is another trick to solve this issue :-
It goes into an infinite loop because scanf() will not consumed the input token if match fails. scanf() will try to match the same input again and again. you need to flush the stdin.
if (!scanf("%d", &sale.m_price)) fflush(stdin)

Why Ctrl-Z does not trigger EOF?

Why Ctrl+Z does not trigger the loop to finish on the following small program?
#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF)
{
//nothing
}
return 0;
}
If I enter: test^ZEnter, it does not get out of the loop.
I found related questions around (here and here) but none to explain it for C (not C++) under Windows.
Note: I use Visual Studio 2015 PRE on a windows 8.1
You need to hit Enter and then use ctrl+Z and then Enter again.
or, you may also use F6
EOF like you use it is not a character. It's the status in which that stream is.
I mean, heck, you even link this question, so you might as well read the accepted answer:
The underlying form of an EOF is a zero-length read.
It's not an "EOF character".
http://www.c-faq.com/stdio/getcharc.html cites a different case than yours, where someone stored the return value of getchar in a char. The underlying problem still occurs occasionally: different runtimes implement different values for the EOF integer (which is why I said, it's not an EOF character), and things love to go wrong. Especially in Visual C++, which is not a "real" C compiler but a C++ compiler with a compatibility mode, it seems things can go wrong.

Scan statements executed before print

I have this code.
#include <stdio.h>
int main(void)
{
int dogs;
printf("How many dogs do you have?\n");
scanf("%d", &dogs);
printf("So you have %d dog(s)!\n", dogs);
return 0;
}
To my knowledge, when executing, the program will say 'How many dogs do you have' and I enter a number. Instead, I get a blank page, and I have to first enter a digit. Then I get
How many dogs do you have?
So you have 3 dog(s)! (if I input 3)
I am using Eclipse + MinGW, and its my first time using C, so I'm not sure what I have/had to set up.
I managed to solve this problem. The problem was that eclipse was first processing all the scan statements, and then processing all the print statements. This would be hard to solve if your program has more than one scan statement.
The fix is somewhat simple. Download the binary fix from the following link.
You then need to paste the starter.exe in the path where you have Eclipse installed.
In my case it was this eclipse\plugins\org.eclipse.cdt.core.win32.x86_64_5.2.0.201309180223\os\win32\x86_64
Yours may very a bit. Overwrite the existing starter.exe and it should work.
printf() buffers output. In general (in UNIX) stdlib is smart enough to flush stdout before reading stdin but it might not be the case in MingW. Consider using fflush() or outputting to stderr.

Resources