Thank you to Mr. Rohit Adalinge
For asking this question.
#include <stdio.h>
#include <stdlib.h>
int main() {
char line[100];
scanf("%[^\n]",line);
printf("Hello,World\n");
printf("%s",line);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
//Case 1
/*
int main() {
char line[100];
scanf("%[^\n]",line);
printf("Hello,World\n");
printf("%s",line);
return 0;
}
*/
/*
Output:
C:\TURBOC3\BIN>TC
SVERI Pandharpur
Hello,World
SVERI Pandharpur
*/
//Case 2
// with %s format specifier it stores only first word
/*It means after pressing of spacebar key whichever word will be there
will not be considered.*/
int main() {
char line[100];
clrscr();
scanf("%s",line);
printf("Hello,World\n");
printf("%s",line);
return 0;
}
/*
Output:
SVERI Pandharpur
Hello,World
SVERI
*/
Note:
In such cases instead of using %[^\n] in scanf, we can use gets() function which will work similar to %[^\n] in scanf ().
In short:
The scanf("%[^\n]", line) has the specifier "%[^\n]".
It scans for unlimited number of characters that match the scan-set ^\n.
If none are read, the specifier fails and scanf() returns with line unaltered.
If at least one is read, all matching are read and saved.
A null character is appended.
For more details please refer:
Question was interesting. Great learning for me though this question.
No comments:
Post a Comment