Credits

Monday, February 9, 2009

Input of Strings

A C program may accept an input of string of characters apart from the input of numbers or single characters.

Check this sample program to input a name and a birth date:



[+/-] show/hide



#include stdio.h /*the stdio.h must be enclosed in <> */
main( )
{ char firstname[15];
char lastname[15];
int birthyear;
int currentyear;
int age;

clrscr();
printf("\nPlease enter your firstname: ");
scanf("%s",&firstname);
printf("\nPlease enter your lastname");
gets(lastname);
printf("\nPlease enter the current year: ");
scanf("%d",&currentyear);
printf("\nPlease enter your birth year: ");
scanf("%d",&birthyear);
age=currentyear-birthyear;
printf("\nHello %s %s",firstname,lastname);
printf("\You are %d years old",age);
getche();
}

What is the output of this code?
Translate this into BLoodshed C++.

AddThis