# How to properly use the \* text modifier in a printf statement.

**URL:** <https://boards.straightdope.com/t/how-to-properly-use-the-text-modifier-in-a-printf-statement/154636>\
**Category:** Factual Questions\
**Created:** [February 12, 2003, 8:12pm UTC](https://boards.straightdope.com/t/how-to-properly-use-the-text-modifier-in-a-printf-statement/154636 "2003-02-12T20:12:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muad\_Dib](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Muad\_Dib](https://boards.straightdope.com/u/Muad_Dib)\
**Post date:** [February 12, 2003, 8:12pm UTC](https://boards.straightdope.com/t/how-to-properly-use-the-text-modifier-in-a-printf-statement/154636/1 "2003-02-12T20:12:16Z")

</div>

I amin a beginning C programming class. While writing a simple program I had to tell a scanf statement to ignore the leading white space:

scanf(" %c" . &char);

I read in the textbook that the \* is used as a more general form that would ignore any data type that was in the buffer. There were example programs showing its use. However, when I tried to use the astrix the program would not function. It compiled and ran, but it treated the statement as if the astrix was not even there.

scanf("\*%c". &char);

So, did I do something wrong? Is this particular to only some compilers?

---

<div class="post-metadata">

**Author:** ![Muad\_Dib](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Muad\_Dib](https://boards.straightdope.com/u/Muad_Dib)\
**Post date:** [February 12, 2003, 8:13pm UTC](https://boards.straightdope.com/t/how-to-properly-use-the-text-modifier-in-a-printf-statement/154636/2 "2003-02-12T20:13:34Z")

</div>

Woops, I screwed up in the title. I meant to say _scanf_ statement.

---

<div class="post-metadata">

**Author:** ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)\
**Post date:** [February 12, 2003, 8:58pm UTC](https://boards.straightdope.com/t/how-to-properly-use-the-text-modifier-in-a-printf-statement/154636/3 "2003-02-12T20:58:21Z")

</div>

I have taught beginning C (although in a corporate rather than academic setting) and am not familiar with the \* formatting feature. I wonder if it’s ANSI.

I would use getchar for what you’re trying to do.

(Using %c is the only time _scanf_ does _not_ skip past white space.)

P.S. “asterisk”

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [February 12, 2003, 9:05pm UTC](https://boards.straightdope.com/t/how-to-properly-use-the-text-modifier-in-a-printf-statement/154636/4 "2003-02-12T21:05:07Z")

</div>

An asterisk may be used _following_ the percent sign to suppress assignment for that match. In other words, saying “%\*c” would say, “match what you normally would for %c, but don’t assign it to anything.”

---

<div class="post-metadata">

**Author:** ![ptr2void](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ptr2void/32/3406_2.png) [@ptr2void](https://boards.straightdope.com/u/ptr2void)\
**Post date:** [February 12, 2003, 9:06pm UTC](https://boards.straightdope.com/t/how-to-properly-use-the-text-modifier-in-a-printf-statement/154636/5 "2003-02-12T21:06:39Z")

</div>

I think you likely would want to use the following (untested, but documented in Kernighan and Ritchie):

```auto

scanf ("%1s", &myChar);

```

This specifier will skip whitespace as it’s expecting a string (which ignores whitespace) but the field width is set to 1, so only a single character is read from the input stream.  
The \* modifier is used to ignore a particular input in the stream (in the case of scanf), and it must be between the % and the conversion specifier. So if someone entered three integers separated by spaces but you only wanted the third integer, you could use:

```auto

scanf("%*d %*d %d, &thirdInt);

```

I also find that some [scanf docs](http://www.cs.jcu.edu.au/Subjects/cp2001/1997/foils/Cscanf/Cscanf.html) which appear to say that if you precede the “c” conversion specifier with a space, the scanf statement will ignore leading whitespace. This may be an implementation difference.  
If you only want a character, you might also consider using the getchar() function:

```auto

char myInput;
while ( (myInput = getchar ()) != '
' )
{
    /* if input is white space, toss it */
}

```

---

<div class="post-metadata">

**Author:** ![LordVor](https://avatars.discourse-cdn.com/v4/letter/l/90ced4/32.png) [@LordVor](https://boards.straightdope.com/u/LordVor)\
**Post date:** [February 12, 2003, 9:18pm UTC](https://boards.straightdope.com/t/how-to-properly-use-the-text-modifier-in-a-printf-statement/154636/6 "2003-02-12T21:18:59Z")

</div>

Example from man(3C) scanf(), Solaris 5.8:

int i; float x; char name[50];  
(void) scanf("%2d%f%\*d %[0123456789]", &i, &x, name);

```
 with input:

 56789 0123 56a72

 will assign 56 to i, 789.0 to x, skip 123, and place the string 56\0 in name. The next call to getchar(3C) will return the character a.

```

-lv
