We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
scanf() function reads(!) the input. The given format specifier is a bit complex for such a beginner project but here goes the explanation:
"%[^\n]%*c" is the format;
% begins a new conversion specification
%[set] accepts the characters in the set definition. this is similar to regular expressions, so set can be a-z, 0-9, etc. In this case, since set starts with ^ character, just like in regexps, it is the inverse of the set, so all characters NOT in the set are matched.
Therefore, %[^\n] matches a string of characters not including the \n (new line) character.
The next matcher is %*c: %c is for matching a single character. But with a * specifier, it is not assigned to a resulting parameter, so the value is simply ignored. Remember in the first matcher we matched until a \n character is encountered. So this is the step we are skipping that one.
IMHO, this is quite complex for such a simple task but thanks to that I searched and learned a few new bits of C/C++ formatting specifiers. For more information check this link: https://en.cppreference.com/w/c/io/fscanf
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
"Hello World!" in C
You are viewing a single comment's thread. Return to all comments →
scanf() function reads(!) the input. The given format specifier is a bit complex for such a beginner project but here goes the explanation:
"%[^\n]%*c" is the format; % begins a new conversion specification %[set] accepts the characters in the set definition. this is similar to regular expressions, so set can be a-z, 0-9, etc. In this case, since set starts with ^ character, just like in regexps, it is the inverse of the set, so all characters NOT in the set are matched. Therefore, %[^\n] matches a string of characters not including the \n (new line) character.
The next matcher is %*c: %c is for matching a single character. But with a * specifier, it is not assigned to a resulting parameter, so the value is simply ignored. Remember in the first matcher we matched until a \n character is encountered. So this is the step we are skipping that one.
IMHO, this is quite complex for such a simple task but thanks to that I searched and learned a few new bits of C/C++ formatting specifiers. For more information check this link: https://en.cppreference.com/w/c/io/fscanf