Input Format Control Strings for scanf - C Language Basics

C examples for Language Basics:scanf

Introduction

Conversion Characters and Their Meanings

Conversion character Meaning
d Reads a signed decimal integer.
i Reads a signed integer. If the input begins with 0, octal is assumed. If the input starts with 0x or 0X, hexadecimal is assumed; otherwise decimal input is assumed.
o Reads a signed octal integer.
u Reads unsigned integer.
x Reads a signed hexadecimal integer.
c Reads the number of characters specified by the field width as type char, including whitespace.
s Reads a string as type char, starting with the next non whitespace character.
[] Reads characters from the specified set between the square brackets.
a, A, e, E,f, F, G, or gConverts the input to type float. A sign, a decimal point, and an exponent in the input are optional.
% Reads a % character that is not stored. Thus the specification to skip a % character is %%.
p Reads input as a pointer. The argument should be of type void**.
n Sets the number of characters to read.

The following table shows a few examples of applying the various options.

Specification Description
%lf Read the next value as type double.
%*d Read the next integer value but don't store it.
%15cRead the next 15 characters as type char.
\n%cRead the next character as type char ignoring whitespace characters.
%10lld Reads the next ten characters as an integer value of type long long.
%Lf Reads the next value as a floating-point value of type long double.
%hu Reads the next value as type unsigned short.

The following code reads various types of data three times with various format strings.

Demo Code

#include <stdio.h>

#define SIZE  20                              // Max characters in a word
void try_input(char *prompt, char *format);   // Input test function

int main(void){
  try_input("Enter as input: -2.35 15 25 ready2go\n",
              "%f %d %d %[abcdefghijklmnopqrstuvwxyz] %*1d %s%n" );

  try_input("\nEnter the same input again: ",
              "%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n");

  try_input("\nEnter as input: -2.3A 15 25 ready2go\n",
              "%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n");
  return 0;/*w  ww. ja va2  s  . co m*/
}

void try_input(char* prompt, char *format){
  int value_count = 0;                  // Count of input values read
  float fp1 = 0.0f;                     // Floating-point value read
  int i = 0;                            // First integer read
  int j = 0;                            // Second integer read
  char word1[SIZE] = " ";               // First string read
  char word2[SIZE] = " ";               // Second string read
  int byte_count = 0;                   // Count of input bytes read
  printf(prompt);
  value_count = scanf(format, &fp1, &i , &j,
                    word1, sizeof(word1), word2, sizeof(word2), &byte_count);
  fflush(stdin);                        // Clear the input buffer
  printf("The input format string for scanf() is:\n     \"%s\"\n", format);
  printf("Count of bytes read = %d\n", byte_count);
  printf("Count of values read = %d\n", value_count);
  printf("fp1 = %f   i = %d   j = %d\n", fp1, i, j);
  printf("word1 = %s   word2 = %s\n", word1, word2);
}

Result


Related Tutorials