Standard Library Functions in C

adplus-dvertising
scanf() Function
Previous Home Next

Description

The C library function, This scanf() function can be used to enter any combination of numerical values, single characters and strings. scanf() enters data from the standard input device and stores it in the computer's memory. This function return the number of data items that have been entered sucessfully.

Declaration

Following is the declaration for scanf() function.

int scanf(const char *format, ...);

Parameters

Format - This is the C string that contains one or more of the following items - Whitespace character, Non-whitespace character and Format specifiers.

A format specifier for scanf follows this prototype:

[=%[*][width][modifiers]type=]

The format specifier can also contain sub-specifiers: asterisk (*), width and length (in that order), which are optional and follow these specifications:

argument description
*This is an optional starting asterisk indicates that the data is to be read from the stream but ignored, i.e. it is not stored in the corresponding argument.
widthThis specifies the maximum number of characters to be read in the current reading operation.
modifiersSpecifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument: h : short int (for d, i and n), or unsigned short int (for o, u and x) l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g) L : long double (for e, f and g).
typeA character specifying the type of data to be read and how it is expected to be read. See next table.

Where the specifier character at the end is the most significant component, since it defines which characters are extracted, their interpretation and the type of its corresponding argument:

type Qualifying Input Type of argument
cSingle character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.char *
dDecimal integer: Number optionally preceded with a + or - signint *
e, E, f, g, GFloating point: Decimal number containing a decimal point, optionally preceded by a + or - sign and optionally followed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4float *
oOctal Integer int *
sString of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).char *
uUnsigned decimal integer.unsigned int *
x, XHexadecimal Integerint *

additional arguments - Depending on the format string, is a string for formatting, and arg1,arg2... are argument that represent the individual input data items. The argument are written as variable or arrays, Whose types match the corresponding character groups in the format string. Each variable name must be preceded by an ampresand(&).

Return Value

The scanf function returns the number of characters that was read and stored. If an error occurs or end-of-file is reached before any items could be read, it will return EOF.

Example

#include <stdio.h>

int main()
{
   char s1[20], s2[30];

   printf("Enter name: ");
   scanf("%s", &s1);

   printf("Enter your second name: ");
   scanf("%s", &s2);

   printf("Entered Name: %s\n", s1);
   printf("Entered second name:%s", s2);
   
   return(0);
}

Output

Enter name: sandeep
Enter your second name: www.r4r.co.in

Entered Name: sandeep
Entered second name: www.r4r.co.in
Previous Home Next