This tutorial will give you the basics of how to use the scanf function in the C programs.
Format Specifiers
Scanf format specifiers preceded by a percentage sign describe the type and format of the data received from the input stream.
The syntax for the scanf format specifier is:
The parameters inside the square brackets are optional.
- The asterisk (*) indicates the data is read from the input stream but not stored into the specified argument.
- Width – The width defines the maximum number of characters from the standard input.
- Length – The length determines the size. The length is specified in the form of hh, h, l, ll, j, z, t, L
- Specifier – defines the type of data to be read from the input stream.
- The scanf function supports the following specifiers.
- %i, %d – Specifier for signed integer format
- %u – Unsigned integer
- %f, %e or %E – Floating-Point format
- %0 – Unsigned octal integer number
- %x, %X – Unsigned Hexadecimal integer number.
- %s – String format
- %c – Character format specifier
Scanf Example In C
The following examples show how to use scanf to read data from standard input.
The above example reads an integer value from the input and stores it into the scanme variable.
The example above reads a character value from the standard input.
This example reads a string from the standard input.
In Closing
In this guide, we learned the basics of the scanf function and how we can use it to read input from the standard input. To learn more, consider the stdio.h reference manual or run the command man scanf in Linux.
from https://ift.tt/2WglsIp

0 Comments