C scanf example

Example 1

Get decimal from console


#include <stdio.h>
//  w w w .j  a  v  a2 s  .  c  o  m
int main(void)
{
  int i, j;

  printf("Enter a decimal number: ");
  scanf("%d.%d", &i, &j);
  printf("left part: %d, right part: %d", i, j);

  return 0;
}

Example 2

o is used for unsigned int expected's value.


#include <stdio.h>
/*  ww w.  ja va2s. co  m*/
main()
{
    int i = 0;
    int k;
    printf("an integer:");
    i=scanf("%o",&k);
    printf("total values inputted %d\n",i);
    printf("The input values %o\n",k);
}

The code above generates the following result.

Example 3

Read string from keyboard


#include <stdio.h>
#include <string.h>
/*w w  w .  j  a v  a 2  s. c  o m*/
int main(void)
{
  char word1[20];

  printf("\nType in the first word:\n 1: ");
  scanf("%19s", word1);
  printf("%19s",word1);
  
  return 0;
}

The code above generates the following result.

Example 4

U is used for unsigned integer in decimal form.


#include <stdio.h>
main()//from  w w w .ja  va2  s  .c om
{
    int i = 0;
    int k;
    printf("integer:");
    i=scanf("%u",&k);
    printf("total values inputted %d\n",i);
    printf("The input values %u\n",k);
}

The code above generates the following result.

Example 5

X is used for unsigned integer in hexadecimal form.


#include <stdio.h>
main()//from ww w  .j  a v  a2  s .  c  o m
{
    int i = 0;
    int k;
    printf("integer:");
    i=scanf("%X",&k);
    printf("total values inputted %d\n",i);
    printf("The input values %X\n",k);
}

The code above generates the following result.





















Home »
  C Language »
    Input / Output »




printf
scanf