Why does scanf break my code!

Why does scanf break my code!


I’m trying to write a basic code to count the number of words in a line. And I’ve to do multiple test cases. If I use scanf to get the number of testcases instead of what I’m doing, it counts the input from scanf as the first string. And so I don’t get the desired input if I use scanf.


int main(void)
	int i;
	char val[10];
//Get number of testcases
	int T = atoi(val);
	int T1 = T; 
    int count[T];
    for (i=0;i<T;i++)
		char line[200];
		int j=0;
		int length = strlen(line);
			if(line[j]==' ')
		}while (j<=length);
		printf("No of words %d \n",count[i]);
	return 0;


First, do everyone a favour and start indenting your code properly.

It seems scanf doesn’t consume the input, so you’ll just have to fast forward the stream yourself — by the amount read, which is what scanf returns if there was no error (but in items, not chars). So really, gets to use in this case.


Yeah sorry about the indentation, I fixed it. But, I don’t understand what you mean by “scanf doesn’t consume the input, so you’ll just have to fast forward the stream yourself”. I understand that scanf returns the number of items successfully read, but how does that affect the part where I read the strings?


When you look at stdin, it still contains the same values you read with scanf. You’d have to advance a pointer, look past those few characters.


there’s no scanf shown in your code - so I have to guess - which is no fun btw - maybe you’re not processing the first newline correctly?

scanf("%u\n", &T);


btw man gets to read why gets should never be used - moot once you get scanf to work


I’m using gets() to get the value(val) for T (the number of test cases) because when I use scanf("%d",&T)
and say I input “2” on prompt, I can only enter one sentence and not 2 .Also on using scanf it seems fgets somehow takes “2” as the first string and the next sentence as the 2nd string and gives me the output as
"No of words 1
No of words X (say)"
That’s what I want to know what is causing this. I had a test yesterday and this only problem wasted half of my time.


How do I do that? Sorry if that’s a noob question.


thanks for including your scanf snippet - it’s exactly what I surmised - did you miss the scanf usage I provided in my first post?


I’ve never really used %u format specifier. “\n” in scanf fixed the problem. Facepalm!
Can you explain what’s happening?


It’s the newline that did it, not the change of signedness. Before you weren’t reading the whole line.


Don’t understand why scanf shouldn’t work. I’d do

int main(void)
	int i;
	int T;
    scanf("%d", &T);
	int T1 = T; 
    int count[T];
    for (i=0;i<T;i++)


Yeah that’s what I was doing, but it doesn’t work. Using the new line “\n” after %d fixes the issue though.


first like with any libc function man scanf - reading man pages is essential if you intend to program in C

the scanf format says exactly what to consume from the input - there are just 3 choices - whitespace, a literal character or a % specifier

a %d does this

d      Matches an optionally signed decimal integer; the  next  pointer
       must be a pointer to int.

after the input number is consumed, the newline remains unconsumed - I matched it with \n - a literal character - you can match it other ways within the scanf format or consume it through other means like getc, fgets etc

please mark the question solved if the solution has been provided