-1

Can someone please explain me the use of .%[0-9] in the above statement.

This is the problem statement:

The first line of input is an integer N. This is followed by N lines, each starting with the character ‘0’, followed by a dot ‘.’, then followed by an unknown number of digits (up to 100 digits), and finally terminated with three dots ‘...’.

#include <cstdio>
using namespace std;
int N; 
char x[110]; 

int main() {
scanf("%d\n", &N);

while (N--) {                 // we simply loop from N, N-1, N-2, ..., 0
scanf("0.%[0-9]...\n", &x);   // ‘&’ is optional when x is a char array
printf("the digits are 0.%s\n", x);
} } // return 0;

Input for the code:-

3

0.1227...

0.517611738...

0.7341231223444344389923899277...

Output for the code:-

the digits are 0.1227

the digits are 0.517611738

the digits are 0.7341231223444344389923899277
Botje
  • 26,269
  • 3
  • 31
  • 41

1 Answers1

0

From my copy of man scanf (other reference link), although apparently it was part of C89 already.

   [      Matches a nonempty sequence of characters from the specified set
          of  accepted  characters;  the next pointer must be a pointer to
          char, and there must be enough room for all  the  characters  in
          the  string,  plus  a  terminating null byte.

To make it concrete: in your case the call to scanf matches a literal 0, a literal ., and stores the remaining digits (the [0-9] part) into the char* pointed to by x.

Botje
  • 26,269
  • 3
  • 31
  • 41