I understand what is happening when I do this more explicitly using getchar(), putchar() and a while loop; however, I was just wondering is the storing and processing of an entered string the same when the code below is executed (behind the scenes)? Is each character being stored as one element of the array “typed”? How does scanf do this? etc.
#include <stdio.h>
int main(void)
{
char typed[500];
scanf("%[^\tEOF]", &typed);
printf("%s", typed);
return(0);
}
Thanks.
Yes, each character is being stored as one element of the array
typed.Note that your scan string looks for anything except a tab, an ‘E’, an ‘O’, or an ‘F’; the ‘EOF’ in the pattern has no connection with
EOFreturned bygetchar()et al on end of file. Also, your code is vulnerable to buffer overflows (hence the succinct comment from knittl) because you don’t specify the size of the buffer in the format string. You’d be safer with:Also, the return type of
main()isint, notvoid.