edited by
492 views
1 votes
1 votes
Please suggest on below 2 points .

I'm not able to understand those 2 points.

Char s[100];

1.What is difference between scanf("%s",s) and scanf("%[^\n]s",s)?

2.how below two codes are same

Code 1

       Getchar();

       Scanf("%[^\n]s",s);

 

Code 2

      Scanf("%*[\n] %[^\n]",s);

Thanks
edited by

1 Answer

Best answer
0 votes
0 votes
  • difference between scanf("%s",s) and scanf("%[^\n]s",s)

when we say for scanf("%[^\n]s",s) It will read a string composed of any character except \n. It will end the string at the first \n it encounters.

while when scanf("%s",s) it will read only the string upto first space.

for example

int main()
{
     char str[100];
     char s[100];
    scanf(" %[^\n]s", str);
    scanf("%s",s);
    printf("%s\n", str);
     printf("%s", s);

}

if we give the input as 

lakshay saini (and press enter)

lakshay saini (and press enter )

so the result will be printed as 

lakshay saini

lakshay

selected by

Related questions