w3resource

C scanf() function

C library function - scanf()

The scanf() function is use to read data from the standard input stream stdin into the locations given by each entry in argument-list. Arguments must be pointers to variables whose types correspond to those in format-string. Input fields are interpreted by a format-string, which consists of a multibyte character string that begins and ends in a shift state.

Syntax:

int scanf(const char *format-string, argument-list);

Parameters:

Name Description Required /Optional
format-string The format argument is a string containing C language conversion specifications. The conversion specification specifies the notation, alignment, significant digits, field width, and other aspects of the output format. To represent non-printing characters, such as newlines and tabs, the format string may contain escape characters. Below are the details. Required
argument-list Depending on the format string, the function may require arguments. Required

The format is a character string, beginning and ending in its initial shift state, if any, composed of zero or more directives. Each directive is composed of one of the following: one or more white-space characters ( <space>, <tab>, <newline>, <vertical-tab>, or <form-feed>); an ordinary character; or a conversion specification. Each conversion specification is introduced by the character '%' or the character sequence "%n$", after which the following appear in sequence:

  • An optional assignment-suppressing character '*'.
  • An optional non-zero decimal integer that specifies the maximum field width.
  • An optional assignment-allocation character 'm'.
  • An option length modifier that specifies the size of the receiving object.
  • A conversion specifier character that specifies the type of conversion to be applied. The valid conversion specifiers are described below.

The conversion characters and their meaning are shown in the following table:

Conversion character Meaning
d Matches an optionally signed decimal integer.
i Matches an optionally signed integer.
o Matches an optionally signed octal integer.
u Matches an optionally signed decimal integer.
x Matches an optionally signed hexadecimal integer,
a, e, f, g Matches an optionally signed floating-point number, infinity, or NaN
s Matches a sequence of bytes that are not white-space characters.
[ Matches a non-empty sequence of bytes from a set of expected bytes.
c Matches a sequence of bytes of the number specified by the field width.
p Matches an implementation-defined set of sequences.
n No input is consumed. The application shall ensure that the corresponding argument is a pointer to the integer into which shall be written the number of bytes read from the input so far by this call to the fscanf() functions. Execution of a %n conversion specification shall not increment the assignment count returned at the completion of execution of the function. No argument shall be converted, but one shall be consumed.
C Equivalent to lc.
S Equivalent to ls.
% Matches a single '%' character; no conversion or assignment occurs. The complete conversion specification shall be %%.

Following table shows the length modifiers and their meanings:

Length modifiers Description
hh Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to signed char or unsigned char.
h Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to short or unsigned short.
l Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long or unsigned long; that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to double.
ll Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long long or unsigned long long.
j Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to intmax_t or uintmax_t.
z Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to size_t or the corresponding signed integer type.
t Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to ptrdiff_t or the corresponding unsigned type.
L Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to long double.

Return value

  • Upon successful completion, this function shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure.

Example-1: scanf() function

Following function accept and display the item number and the price where item number is an integer and price is fractional.
#include <stdio.h>
int main(void)
{
   int item_id;
   float item_price;
   printf("Input item ID:\n");
   scanf("%d", &item_id);
   printf("Input item price:\n");
   scanf("%f", &item_price);
   fflush(stdin);
   printf("\nItem ID: %d", item_id);
   printf("\nItem price: %f ", item_price);
}

Output:

Input item ID:
123
Input item price:
225.60

Item ID: 123
Item price: 225.600006

Example-2: scanf() function:

Here is an example of how the scanf() function works when the width of input data is specified:

#include <stdio.h>
int main(void)
{
   int item_id;
   float item_price;
   printf("Input item ID:\n");
   scanf("%d", &item_id);
   printf("Input item price:\n");
   scanf("%f", &item_price);
   fflush(stdin);
   printf("\nItem ID: %d", item_id);
   printf("\nItem price: %f ", item_price);
}

Output of the said program for different values:

Input:
23445.34
test
Output:
23 445.339996 te
--------------------------
Input:
9 8764
test
Output:
9 8764.000000 te

It must be noted that when an user accept strings using scanf(), a space is considered to be a string terminator. Therefore, scanf() cannot be used to accept strings containing spaces unless the number of words is known. Consider the following example:

#include <stdio.h>
int main(void)
{
 char text[50];
 printf("Input a string (max 50 characters):\n");
 scanf("%50s", text);
 fflush(stdin);
 printf("Output:\n");
 printf("%s", text);
}

Output:

Input a string (max 50 characters):
C programming
Output:
C

There was a space after 'C' in the input, which caused the string to be terminated. In order to accept strings that may contain spaces, the gets() function should be used.

C Programming Code Editor:

Previous C Programming: C fscanf()
Next C Programming: C sscanf()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming/stdio/c_library_method_scanf.php