in Programming in C edited by
489 views
1 vote
1 vote
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
in Programming in C edited by
489 views

1 Answer

0 votes
0 votes
Best answer
  • 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

1 comment

Thanks lakshay

Can you please explain 2 point of my doubt.

It will be helpful.
0
0

Related questions