0

I have this code, I'm trying to introduce n number of texts using fgets() and then know what's the length with a while and print this value (later i would do other things with this) but the first iteration always prints "0" without doing the fgets call and i want to know why and how to fix it.

#include <stdio.h>
int main(){
    int n;
    scanf("%d", &n);

    int i;
    for(i=0; i<=n; i++){
        char texto[50];
        fgets(texto, 50, stdin);
        int length = 0;
        while(texto[length]!='\n'){
            length++;
        }
        printf("%d\n", length); 
    }
    return 0;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148

0 Answers0