how to use scanf in for loop as a condition in c programming languange -


as in following codes did not understand usage of scanf in for loop.

how can use or when use it? can me ?

#include<stdio.h>  int main() {  char word[10]; int count[10] = {0};  printf("enter text:\n");  for(scanf("%s", word); word[0] != '.'; scanf("%s", word)) {     int len;      for(len = 0; word[len] != '\0'; len++);         count[len]++; }  int i; for(i = 1; < 10; i++) {     printf("there %d words %d letters\n", count[i], i); } 

a for loop has 3 parts. first part executed first, , never executed again. doing

for(scanf("%s", word); word[0] != '.'; scanf("%s", word)) { 

is equivalent to

scanf ("%s", word); for(; word[0] != '.'; scanf("%s", word)) { 

if you're not understanding how for loop works, see flowchart:

enter image description here

(source)

if want tutorial, see this tutorial on loops.


Comments

Popular posts from this blog

How to check data type in C++? -

javascript - Is getTimezoneOffset() stable during daylight saving transition? -

sql server - SQL Query returns one result, does not return 0 or NULL result -