What are scanf ("% * s") and scanf ("% * d") format identifiers?

What is the practical use of the "%*" formats in scanf (). If this format exists, there must be some purpose for it. The following program gives a strange result.

 #include<stdio.h> int main() { int i; char str[1024]; printf("Enter text: "); scanf("%*s", &str); printf("%s\n", str); printf("Enter interger: "); scanf("%*d", &i); printf("%d\n", i); return 0; } 

Output:

 manav@workstation:~$ gcc -Wall -pedantic dc dc: In function 'main': dc:8: warning: too many arguments for format dc:12: warning: too many arguments for format manav@manav-workstation:~$ ./a.out Enter text: manav D Enter interger: 12345 372 manav@workstation:~$ 
+44
c formatting scanf
Jan 28
source share
5 answers

For printf *, it allows you to specify the minimum field width using an additional parameter, for example, printf("%*d", 4, 100); sets the width of the field 4. The width of the field 4 means that if less than 4 characters are required to print the number, white space characters are printed until the field width is filled. If the number takes up more space than the specified field width, the number is printed as is, without truncation.

For scanf *, indicates that the field should be read, but ignored, so for example, scanf("%*d %d", &i) to enter "12 34" will ignore 12 and read 34 into integer i.

+90
Jan 28
source share

A star is a flag symbol that says to ignore the text read by the specification. For more information from the glibc documentation:

The optional `* 'flag character says to ignore text read for this specification. When scanf finds a transform specification that uses this flag, it reads the input as indicated by the rest of the transform specification, but it discards this input, does not use a pointer argument, and does not increase the number of successful assignments.

This is useful in situations where the specification line contains more than one element, for example: scanf("%d %*s %d", &i, &j) for "12 test 34" - where I and j are integers and you want to ignore rest.

+19
Jan 28
source share

Look here

An optional start asterisk indicates that data should be retrieved from stdin, but ignored, that is, it is not stored in the corresponding argument.

+6
Jan 28
source share

* Used to skip input without sending it to any variable. So scanf("%*d %d", &i); will read two integers and put the second in i .

The value that was output in your code is just the value that was in the uninitialized variable i - the call to scanf did not change it.

+5
Jan 28
source share

scanf("%*d",&a) * skips input. To read the inputs, you need to use the extra "%d" in scanf . For example:

  int a=1,b=2,c=3; scanf("%d %*d %d",&a,&b,&c); //input is given as: 10 20 30 

O / r

 a=10 b=30 and c=3; // 20 is skipped 

If you use another %d ie: scanf("%d %*d %d %d",&a,&b,&c); //input is given as: 10 20 30 40 scanf("%d %*d %d %d",&a,&b,&c); //input is given as: 10 20 30 40 then a = 10 b = 30 c = 40.

If you use "," in scanf, no value will be accepted after %*d ie; scanf("%d %*d,%d" &a,&b,&c)// 10 20 30 O / P: a = 10 b = 2 c = 3 there will be an output.

+2
Sep 23 '13 at 15:49
source share



All Articles