Reading Characters by char and by string Using scanf() - C Language Basics

C examples for Language Basics:scanf

Description

Reading Characters by char and by string Using scanf()

Demo Code

#include <stdio.h>
#define MAX_TOWN 10//from   w  ww. jav a2 s . 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 );
    scanf("%[^,] , %[0123456789]", name, sizeof(name), age, sizeof(age));
    printf("\nYour name is %s and you are %s years old.\n", name, age );
    return 0;
}

Related Tutorials