C Programming

Scanf in C

The scanf function is one of the most famous and useful C functions. It allows a C program to accept input from the standard input stream, mainly the keyboard. Once scanf reads data from the standard input, it stores the value according to the specified parameter format.

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.

int main() {
    int scanme;
    scanf("%d", &scanme);
    printf("The value of scanme is %d", scanme);
    return 0;

The above example reads an integer value from the input and stores it into the scanme variable.

int main() {
    char scanme;
    scanf("%c", &scanme);
    printf("The value of scanme is %c", scanme);
    return 0;

The example above reads a character value from the standard input.

int main() {
    char scanme[10];
    scanf("%s", &scanme);
    printf("The value of scanme is %s", scanme);
    return 0;

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list