Its long time since I updated my blog. This time, a real weird puzzle is here for you to solve.
Observe the following program...
#include "stdio.h"
#include "stdlib.h"
int main()
{
char data[20];
printf("Type Something Here with spaces: ");
scanf("%s",&data);
printf("%s",data);
system("pause");
return 0;
}
when I run the code, I asks us to type something with spaces.
I typed "abcd efgh".
Try to guess the output.
The output is just "abcd".
Alas what happened to "efgh"!!!
The answer is in scanf's implementation.
It has been mentioned in "http://www.opengroup.org/onlinepubs/009695399/functions/scanf.html", that,
s
Matches a sequence of bytes that are not white-space characters. The application shall ensure that the corresponding argument is a pointer to the initial byte of an array of char, signed char, or unsigned char large enough to accept the sequence and a terminating null character code, which shall be added automatically.
Isn't it weird?
No comments:
Post a Comment