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

5

49

What is the practical use of the formats "%*" in scanf(). If this format exists, there has to be some purpose behind it. The following program gives weird output.

#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 d.c
d.c: In function ‘main’:
d.c:8: warning: too many arguments for format
d.c:12: warning: too many arguments for format
manav@manav-workstation:~$ ./a.out
Enter text: manav
D
Enter interger: 12345
372
manav@workstation:~$
Gullet answered 28/1, 2010 at 15:16 Comment(5)
Manav, based upon some of your recent questions, you would do well to read a text book.Thoughtout
@Alok: Well i have read a text book but hadn't gone into such details. Most text-books don't provide examples catering to real-time usage. I think, the best thing to do would be to read "The C Compiler Design" text-book.Gullet
@Manav: You definitely don't need a "C Compiler Design" book. Have you tried K&R?Thoughtout
@Alok: I will start reading K&R but still I feel a C programming course is incomplete without adequate knowledge of Operating Systems and "C Compiler Design"Gullet
@Manav: man 3 printf would answer your question too.Gangland
C
100

For printf, the * allows you to specify the minimum field width through an extra parameter, e.g. printf("%*d", 4, 100); specifies a field width of 4. A field width of 4 means that if a number takes less than 4 characters to print, 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 with no truncation.

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

Currycomb answered 28/1, 2010 at 15:21 Comment(5)
please change this "printf("%*d", 4, 100);" to "printf("%.*d", 4, 100);" .* should be used.Oldster
For scanf the fields with * are not counted in the return value.Treenware
What is a field width?Tenantry
@JananathBanuka answer updated with an exemplification explaining the term.Nougat
Why was printf() add here? It's a totally different function and not what the OP was asking for. It's like someone asking about strstr() and you giving examples of strchr(). As an after thought you included the request information.Alten
T
25

The star is a flag character, which says to ignore the text read by the specification. To qoute from the glibc documentation:

An optional flag character `*', which says to ignore the text read for this specification. When scanf finds a conversion specification that uses this flag, it reads input as directed by the rest of the conversion specification, but it discards this input, does not use a pointer argument, and does not increment the count of successful assignments.

It is useful in situations when the specification string contains more than one element, eg.: scanf("%d %*s %d", &i, &j) for the "12 test 34" - where i & j are integers and you wish to ignore the rest.

Triarchy answered 28/1, 2010 at 15:20 Comment(0)
E
9

The * is used to skip an input without putting it in any variable. So scanf("%*d %d", &i); would read two integers and put the second one in i.

The value that was output in your code is just the value that was in the uninitialized i variable - the scanf call didn't change it.

Elliellicott answered 28/1, 2010 at 15:20 Comment(0)
S
6

See here

An optional starting asterisk indicates that the data is to be retrieved from stdin but ignored, i.e. it is not stored in the corresponding argument.

Silda answered 28/1, 2010 at 15:20 Comment(0)
B
4

In scanf("%*d",&a) * skips the input. In order to read the inputs one has to use an 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/p:

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

If you use another %d i.e: 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 then no value will be taken after %*d i.e; scanf("%d %*d,%d" &a,&b,&c)// 10 20 30 O/p: a=10 b=2 c=3 will be the output.

Bentlee answered 23/9, 2013 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.