0
#include <stdio.h>
int main()
 int t,i,j,k;
 scanf("%d",&t); // 'line5'
 char str[10000];
 for(i=0;i<t;i++)
 {
   scanf("%[^\n]s",str);// 'line9'
   k=strlen(str);
   for(j=0;j<k;j++)
   {
     printf("%c",str[j]);
   }
   printf("\n");
 }
  return 0;
 }

## scanf function is not working

I have tried this question by another way but still not working.I am facing this problem for 1st time.

dkrity
  • 1
  • 2
  • That `Enter` key you pressed after inputting the number will still be in the input buffer as a newline. Think about what happens to that in the next `scanf` call. – Some programmer dude Jul 12 '17 at 11:17
  • 3
    The first `scanf()` is leaving a newline character in the input stream, picked up by the second call. Try `scanf("%[^\n]s",str);` --> `scanf(" %[^\n]",str);`. Note the leading space in the format string to skip over the newline character; also note that the trailing `s` was not needed as it is not part of the `%[]` directive. – ad absurdum Jul 12 '17 at 11:19
  • `scanf("%[^\n]s",str);` <- wrong. 1. it's **either** `[]` **or** `s`, not both. 2. it overflows any buffer. 3. it can't scan the `\n` that's left in the buffer from the `scanf()` call before. –  Jul 12 '17 at 11:19
  • From where i will get to know these things ? – dkrity Jul 12 '17 at 11:20
  • @dkrity If in doubt, from the C standard (eg, see [a draft here](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)), but this of course is a reference, hard to read if you don't know the issues already. And then, for learning, there are resources like on this site [How to read / parse input in C? The FAQ](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) and e.g. a [document I wrote myself](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) –  Jul 12 '17 at 11:22
  • 1
    [Get a good book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – ad absurdum Jul 12 '17 at 11:22
  • Thanks ! @David Bowling it's working ! and good books like ? – dkrity Jul 12 '17 at 11:27
  • Follow the link in my last comment to a list. – ad absurdum Jul 12 '17 at 11:28

0 Answers0