Learn C - C Console Input






Input Format Control Strings

The following table lists the Conversion Characters and Their Meanings for scanf function.

Conversion characterMeaning
dReads the input as a signed decimal integer.
iReads the input as a signed integer. If the input begins with 0, then the input is assumed to be octal. If the input starts with 0x or 0X, then hexadecimal input is assumed; otherwise decimal input is assumed.
oReads the input assuming it is a signed octal integer.
uReads the input as an unsigned integer.
xReads the input as a signed hexadecimal integer.
cReads the number of characters specified by the field width as type char, including whitespace. If the field width specification is absent, one character will be read. To ignore preceding whitespace, precede the format specification by a whitespace character.
sReads a string of successive nonwhitespace characters as type char, starting with the next nonwhitespace character.
[]Reads characters from the specified set between the square brackets. The first character not found in the set ends the input. The %[]ab] reads characters as long as they are a, b, or ]. The %[^]ab] reads characters as long as they are not a, b, or ].
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. To skip a % character is %%.
pReads input as a pointer. The argument should be of type void**.
nNo input is read, but the number of characters read from the keyboard are stored in the argument.

The following table lists Examples of Options in Conversion Specifications.

SpecificationDescription
%lfRead the next value as type double.
%*dRead 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.
%10lldReads the next ten characters as an integer value of type long long.
%LfReads the next value as a floating-point value of type long double.
%huReads the next value as type unsigned short.

    #include <stdio.h> 
      /*w  w w.j  a  v  a  2 s  .com*/
    int main(void) { 
      int i = 0; 
      int j = 0; 
      int value_count = 0; 
      float fp1 = 0.0f; 
      printf("Enter: fp1 = 3.1 i = 7 8\n"); 
      
      printf("\nInput:"); 
      value_count = scanf("fp1 = %f i = %d %d", &fp1, &i , &j); 
      printf("\nOutput:\n"); 
      printf("Count of values read = %d\n", value_count); 
      printf("fp1 = %f\ti = %d\tj = %d\n", fp1, i, j); 
      return 0; 
    } 

The code above generates the following result.





Variations on Floating-Point Input


#include <stdio.h> 
  //from  w  w  w .  j a v  a  2 s.c  o  m
int main(void) 
{ 
  float fp1 = 0.0f; 
  float fp2 = 0.0f; 
  float fp3 = 0.0f; 
  int value_count = 0; 
  printf("Enter: 3.14.314E1.0314e+02\n"); 
  
  printf("Input:\n"); 
  value_count = scanf("%f %f %f", &fp1, &fp2, &fp3); 
  
  printf("\nOutput:\n"); 
  printf("Number of values read = %d\n", value_count); 
  printf("fp1 = %f  fp2 = %f  fp3 = %f\n", fp1, fp2, fp3); 
  return 0; 
} 

The code above generates the following result.





Reading Hexadecimal and Octal Values

We can read hexadecimal values from the input stream using the format specifier %x.

For octal values you use %o.


    #include <stdio.h> 
      // w w  w .j a  v a 2  s. c o m
    int main(void) { 
      int i = 0; 
      int j = 0; 
      int k = 0; 
      int n = 0; 
      
      printf("Enter three integer values:"); 
      n = scanf(" %d %x %o", &i , &j, &k ); 
      
      printf("\nOutput:\n"); 
      printf("%d values read.\n", n); 
      printf("i = %d   j = %d   k = %d\n", i, j, k ); 
      return 0; 
    } 

The code above generates the following result.

Reading Characters Using scanf()

You can read a single character and store it as type char using the format specifier %c.

To read a string of characters, you use either the specifier %s or the specifier %[].

%[aeiou] will read a string that consists only of vowels.

The first character that isn't a vowel will signal the end of the string.

%[^aeiou] reads a string that contains any character that isn't a vowel.

The first vowel or whitespace character will signal the end of the string.

%[] specification enables you to read a string containing spaces, and %s specification can't do that.

You just need to include a space as one of the characters between the square brackets.


    #include <stdio.h> 
//  w  w  w  .  jav  a  2s.com
    int main(void){ 
      char initial = ' '; 
      char name[80] = { ' ' }; 
      char age[4] = { '0' }; 
      printf("Enter your first initial: "); 
      scanf("%c", &initial, sizeof(initial)); 
      printf("Enter your first name: " ); 
      scanf("%s", name, sizeof(name)); 
      fflush(stdin); 
      
      if(initial  != name[0]) 
        printf("%s,you got your initial wrong.\n", name); 
      else 
        printf("Hi, %s. Your initial is correct. Well done!\n", name ); 
      
      printf("Enter your name and age separated by a comma:\n" ); 
      scanf_s("%[^,] , %[0123456789]", name, sizeof(name), age, sizeof(age)); 
      printf("\name is %s is %s years old.\n", name, age ); 
      return 0; 
    } 

The code above generates the following result.