• edited by
979 views
1 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

1 Answer

Best answer
0 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
Position:
Show:

Related questions

5 5 votes
3 3 answers
339
339 views
GO Classes asked Jun 16
339 views
What is the output of the following code?#include <stdio.h int fun(int n) { if (n == 0) return 1; return n + fun(n - 1); printf("%d ", n); } int main() { printf("%d", fun...
8 8 votes
3 3 answers
321
321 views
GO Classes asked Jun 15
321 views
What happens when the following code is executed?#include <stdio.h void fun(int n) { if (n == 0) return; printf("%d ", n); fun(n ); } int main() { fun(3); return 0; }$\te...
8 8 votes
3 3 answers
329
329 views
GO Classes asked Jun 15
329 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int x = 0; int y; if (n == 0) return 0; x++; y = fun(n - 1); return y + x; } int main()...
7 7 votes
2 2 answers
278
278 views
GO Classes asked Jun 13
278 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int count = 0; if (n == 0) return count; count = count + n; return fun(n - 1); } int ma...