Expected program to print the value of EOF - c

In the code:
#include<stdio.h>
int main()
{
int t;
for(;scanf("%d",&t);printf("%d",t));
}
The program runs as expected when I give general intergers as input. I am working on Windows so when I scanf Cntrl+Z into the argument t, I do not get the value of EOF i.e -1 on the standard output, but the previous argument that was stored in it.
Also when I press Cntrl + D the program terminates, why does Cntrl+D cause scanf to return 0?
And why on scanf Cntrl+C my compiler says: "Process terminated with status -107......"
I am not understanding why this is happening? Please help.

scanf returns the number of successfully matched formatting specifiers, or EOF if the end of input was reached before matching (or failing to match) the first specifier.
When you press Ctrl+Z, scanf reaches the end of input and returns EOF (because Ctrl+Z terminates input on Windows). This does not terminate your for loop because EOF is nonzero, so the previous value of t is printed (as t was not changed by the call). Note that t will not receive the value EOF on end-of-input as you seem to expect: scanf returns EOF as the return value, it does not write it into the pointers you pass to it.
When you press Ctrl+D, it is treated as any other character. Since it is non-numeric, it causes a matching failure for the %d specifier and scanf returns 0, which terminates the loop.

Try this code and When ever if you press CTL+Z(CTL+D on linux) will give you zero. otherwise prints 1
#include <stdio.h>
main()
{
int c;
while(c=getchar()!=EOF) //here get the character and then compares with the EOF if Not equal 1 will assign to c , if equal 0 will assign to c.
printf("%d",c);
printf("%d",c);//when ever we press ctl+Z(ctl+d on linux) then it will print zero remaing all cases this statement wont execute
getchar();
}

Related

Explain why do while loop has not checking the condition?

void main () {
char f;
do {
scanf("%c",&f);// input a character
printf("%c",f);//output a character
}while(f=='y');
}
any value is going to end the program even if y put is end the program can anyone explain the reason on this program I am stuck at this ..
Other than the void main() (the return value of main should be int) and the failure to check the value returned by scanf (if scanf returns 0 and does not assign a value to f, then attempting to read a value from the uninitialized f is undefined behavior), your program works just fine:
$ echo yyyyyabcd | ./a.out; echo
yyyyya
However, if you are entering data interactively, you may be entering the input stream y\ny\n (hitting enter/return after each y), and the program is terminating when it sees the first newline.
That's because when scanf gets executed again, it is reading a white space character left in the input stream from the previous input you type.
The simplest solution is to include a whitespace character before the %c conversion specifier. Example:
scanf(" %c",&f);// input a character
This tells scanf to skip leading whitespace.

C scanf() function, how to terminate on EOF/EOT?

I wrote a simple program to test the scanf() function in C. It basically reads from the terminal, char by char, reprinting the return value and the char read; and to terminate if EOF/EOT is met or if a \n newline is read.
#include <stdio.h>
#include <stdbool.h>
int main(void) {
char c; int ret;
printf("Enter the chars to test: ");
//LOOP (scan & print) only when return is not EOF and char is not newline
while ( ((ret = scanf("%c", &c)) != EOF) && c!='\n' ) {
printf("%i %c\n", ret, c);
}
return 0;
}
It terminates correctly, if newline (Enter) is pressed. But the it won't just terminate with a single Ctrl-D. A single Ctrl-D will flush the typed 'chars' and printing them. Then after that it will wait again for input, although an EOF has been sent with the Ctrl-D. If we press Ctrl-D again the 2nd time directly after the 1st (2x) or just Enter it will terminate. So you will need two consecutive Ctrl-D to terminate the program (or the loop in this case).
Example:
If you input 987 on the terminal, then press Enter; then 1 9, 1 8, 1 7 will be printed on newline each.
If you input 987 on the terminal, then press Ctrl-D; then 1 9 will be printed on the same line (because there is no Enter typed after inputing the 987 input), 1 8, 1 7 will be printed on newline. Then it will still wait for more inputs, unless it is terminated by directly entering a 2nd consecutive Ctrl-D or with a newline (Enter). So it (the program) will only stop (exit the loop) after a newline or 2nd consecutive Ctrl-D.
I am confused. Shouldn't a single Ctrl-D sent stop the loop here? What should I do to stop the program (scanf loop) after receiving just a single Ctrl-D?
I tested the code on Lubuntu 19.10 with gcc 9.2.1.
The issue is scanf() does not return EOF until there is no more input waiting and EOF is encountered. (your "%c" conversion specifier will otherwise accept any character, including the '\n' character, as valid and consider it a successful conversion)
When you type a line of characters, e.g. "abcdefg" and attempt to press Ctrl+d (indicating end-of-input), the input ("abcdefg") is processed and when 'g' is reached, then scanf() blocks waiting on your next input. (because a successful conversion took place and no matching or input failure occurred)
Once Ctrl+d is typed a 2nd time (indicating end-of-input), when there is no input to process, EOF is reached before the first successful conversion and an input-failure occurs,scanf() then returns EOF.
See: man 3 scanf -- RETURN VALUE section and C11 Standard - 7.21.6.2 The fscanf function(p16)
Test the returned value for one, not EOF. When eof is reached first time, zero is returned. The next scanf after zero returns EOF.
while ( ((ret = scanf("%c", &c)) == 1) && c!='\n' ) {
printf("%i %c\n", ret, c);
}
This behavior is specified for the standard C library.
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror).
You can add if (feof(stdin) != 0) break; into the loop body, if you use more complex input format.
If you read chars only, is it not better use fgetc?

Scanf stores wrong value inside integer variable

I have wrote the following code using c programming language (Standard 89):
#include <stdio.h>
#include <stdlib.h>
int main()
{
int cc,dd;
scanf("%d/%d",&cc,&dd);
int ll;
scanf("%d",&ll);
printf("Value of ll is: %d",ll);
return 0;
}
If I submit the following as an input in one line: 4/5h I get the following output: Value of ll is: 67
So I have 2 questions;
1) where that 67 value came from? (I tried to change the input to something like 1/2t but got the same result)
According to what I have read since there is no integers in the buffer the application should wait until one is available (For example to wait for a new input)
2) When I run my code using debug mode I can see that ll value is 65 but not 67!
By typing non-digit characters in entries like "5h" or "2t" for dd, you're fouling up the read for ll in the second scanf call.
%d tells scanf to skip any leading whitespace, then to read decimal digit characters up to the first non-digit character. If you type a string like "5h" or "2t", that leading digit will be successfully converted and assigned to dd, but the trailing non-digit character will be left in the input stream, and that's fouling up the read for ll. No new value is being read into ll, you're getting whatever indeterminate value it had when the program started up.
Always check the result of scanf (and fscanf and sscanf) - if it's less than the number of inputs you expect, then you have a matching failure (you're not handling some input correctly). If it's EOF, then you have a failure on the input stream itself.
For this particular case, you can work around the problem by checking the result of scanf - if it's 0, then there's a bad character in the stream. Throw it away and try again:
int r;
while ( ( r = scanf( "%d", &ll ) ) != 1 && r != EOF )
getchar();
This will call scanf and try to read a value into ll. We expect scanf to return a 1 on a successful input, so we'll loop while the result of scanf isn't 1 (and isn't EOF, either). If the read isn't successful, we assume there's a non-digit character stuck in the input stream, so we read and discard it with the getchar call.

how putchar works with while loop?

I am new to c programming, so hope you guys can help me out with such questions.
1. I thought putchar() only print 1 char each time, while when I enter several char like 'hello' it print 'hello' before allow me to enter a next input? I thought that it should print only 'h' and then allow me to enter other input because getchar() only return one character each time.
2. how to make the loop stops? I know EOF has value of -1, but when I enter -1, the loop still runs.
#include <stdio.h>
main()
{
int c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
}
After the first getchar() has completed reading one character, the next getchar(); is inside the while() loop, so as per the logic, it will keep reading the input one-by-one, until in encounters EOF.
Following the same logic, putchar(c); is under the while loop, so it will print all the characters [one character per loop basis] read by getchar() and stored in c.
In linux, EOF is produced by pressing CTRL+D. When waiting for input, if you press this key combination, the terminal driver will transform this to EOF and while loop will break.
I'm not very sure about windows, but the key combination should be CTRL+Z.
Note: even if it seems entering -1 should work in accordance with EOF, actually it won't. getchar() cannot read -1 all at a time. It will be read as - and 1, in two consecutive iterations. Also worthy to mention, a character 1 is not equal to an integer 1. A character 1, once read, will be encoded accordingly [mostly ASCII] and the corresponding value will be stored.
getchar() gets the input from the console. In a while loop, it will read all the characters from the input including the return key.
-1 is "-1". It's not a value but just another combination of characters. EOF occurs when there is no more char in the buffer. i.e. when you press Enter (or Ctrl-Z or Ctrl-D depending on your OS)

How to make the below condition fail by giving inputs through stdin

void main()
{
float x;
while(scanf("%f",&x) != 0)
printf("%f\n",x);
}
The above code takes input from stdin and keeps repeating it but how to end this? I know scanf can return EOF so if I add a check like
while(scanf("%f",&x) != EOF)
Which input from stdin can cause any of the above two condition to fail?
ctrl+d will make the program end but I want to know is there any specific input which can make this condition fail?
scanf function returns the number of input items successfully scanned.
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs.
In your program always it return 1. To prove see the code..
#include<stdio.h>
main()
{
int a;float x;
while((a=scanf("%f",&x)) != 0)
printf("%f %d\n",x,a);
}
In this program a is always 1. Be cause only one value is scanned successfully.
a gets 2 for , if you scan two values.
Looking at linux manual pages:
scanf: These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
So, using your original program:
void main()
{
float x;
while(scanf("%f",&x) != 0)
printf("%f\n",x);
}
That means that if you enter anything that is not convertible, will end your program. Try it and enter any letter and hit enter
Ctrl + D or Ctrl + Z,
Similar questions already asked before:
End of File(EOF) of Standard input stream (stdin)

Resources