Scanf() Format Modifiers - C Language Basics

C examples for Language Basics:scanf

Introduction

scanf( ) can have format specifiers.

The format specifiers can set a maximum field length modifier, which is an integer, placed between the % and the format specifier.

The maximum field length modifier limits the number of characters read for that field.

For example, to read no more than 20 characters into str.

scanf("%20s", str);

If the input stream is greater than 20 characters, a subsequent call to input begins where this call leaves off.

Input may terminate before the maximum field length is reached if a white space is encountered, and scanf( ) moves on to the next field.

Suppressing Input

You can tell scanf( ) to read a field but not assign it to any variable by preceding that field's format code with an *.

For example,

scanf("%d%*c%d", &x, &y);

Related Tutorials