0

I'm supposed to write a simple C program to read in an integer and loop n times to work on the string, but the first loop automatically passes an empty string if I use the scanf integer, but if I use a constant number the loop works right. Somebody please explain to me what's going on.

#include <stdio.h>
#define MAX 80

int main(){
    char sentence[MAX];
    int n, i;
    scanf("%d", &n);
    for(i=0; i<3; i++){//it loops with empty string automatically if I replace 3 with n
        gets(sentence);
        printf("%s\n", sentence);
    }
}
styvane
  • 59,869
  • 19
  • 150
  • 156
JSamson
  • 31
  • 6

1 Answers1

0

Try this code

    #include<stdio.h>
    #include<conio.h>
    #define MAX 80
    void main()
    {
    char sentence[MAX];
    int n,i;
    clrscr();
    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf(" %99[^\n]",sentence);
    printf("\n%s\n",sentence);
    getch();

    }
Vasanth Kumar
  • 150
  • 1
  • 9