سؤال

I have the following code:

char str[0]; // 0 length

while(fgets(str, 100, stdin)) { // It works.
   fputs(str, stdout);
}

But if I do without loop, then it breaks. like this;

  fgets(str, 100, stdin); 
   fputs(str, stdout); 
هل كانت مفيدة؟

المحلول

Because your code invokes undefined behavior (you make fgets() write past the end of your buffer), so it can do anything it pleases.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top