3
int x, sum=0;
while(scanf_s("%d", &x) != EOF) 
{
    sum += x;  
}
printf("sum is %d", sum);
return 0;

No matter how input ,I must type CTRL + Z three times to print sum. I'm using VS 2015.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
orangetimes
  • 35
  • 1
  • 4
  • Shouldn't `scanf_s` be used like this (in your case): `scanf_s("%d", &x, 1)`? – Fiddling Bits Oct 10 '15 at 23:27
  • 1
    @FiddlingBits Nope. The extra argument is required only when using `%c`, `%s` or `%[`. – Spikatrix Oct 11 '15 at 07:22
  • Could you post a [mcve] along with the sample inputs and outputs? – Spikatrix Oct 11 '15 at 07:23
  • thanks for comments, I find using that code on VS2008, it works. so, there's something wrong in VS2015. also use `#pragma warning (disable : 4996)` and `scanf` instad of `scanf_s`, it doesn't work – orangetimes Oct 12 '15 at 00:26
  • 1
    Possible duplicate of [Why do I require multiple EOF (CTRL+Z) characters?](http://stackoverflow.com/questions/5655112/why-do-i-require-multiple-eof-ctrlz-characters) – Joel Trauger Oct 13 '15 at 20:09

1 Answers1

-2

EOF is a special constant that should only be used when reading from files to check for the end of the file

The End Of the File is what it represents and is a handy way to represent that without remembering what actual value it is.

Pressing Ctrl + Z causes differing behaviors depending on the system. On Linux systems it suspends a process. On Windows, it is interpreted as the Undo command by the kernel. It has no behavior on Mac OS, and is substituted for CMD + Z, which functions similarly to Windows Ctrl + Z.

scanf_s returns an int that is the number of characters read from the input.

Joel Trauger
  • 720
  • 1
  • 4
  • 24
  • Good info, but this is a comment, not an answer. – Fiddling Bits Oct 10 '15 at 23:45
  • @FiddlingBits Then post an an "actual" answer instead of complaining about it. He wanted to know why he had to press Ctrl + Z 3 times. My answer is because the behavior is undefined. – Joel Trauger Oct 13 '15 at 20:06
  • @FiddlingBits I don't take criticism if it's non-constructive, much like your comment. See my profile for other places where people have corrected me. I take it well if they offer a correct answer instead of just saying, "You're wrong." – Joel Trauger Oct 13 '15 at 20:24