TopBottom

Followers



Click on more
SUBSCRIBE

Enter your email address:

Delivered by FeedBurner



VIDEO

Announcement: wanna exchange links? contact me at ravikrak@yahoo.com

Inputting Character Strings in C

Posted by Ravi Kumar at Saturday, September 10, 2011
Share this post:
Ma.gnolia DiggIt! Del.icio.us Yahoo Furl Technorati Reddit

Single character can be read from the terminal using
the getchar function .The same can be achieved using the scanf
function also. In addition,a function can input strings contain
ing more than one character. Following are the specifications
for reading character strings:
%ws or %wc
Program: /* Reading of strings using %wc and %ws */

main()
{
int num;
char name1[15],name2[15],name3[15];
printf("enter serial number and name one \n");
scanf("%d %15c",&num,&name1);
printf("%d %15s\n\n",num,name1);
printf("enter serial number and name two \n");
scanf("%d %s",&num,&name2);
printf("%d %15s\n\n",num,name2);
printf("enter serial number and name three \n");
scanf("%d %15s",&num,&name3);
printf("%d %15s\n\n",num,name3);
}

Output: enter serial number and name one
1 123456789012345
1 123456789012345r
enter serial number and name two
2 New York
2 New
enter serial number and name three
2 York
enter serial number and name one
1 123456789012
1 123456789012 r
enter serial number and name two
2 New-York
2 New-York
enter serial number and name three
3 London

In the above program,the specification %s terminates
reading at the encounter of a blank space.Therefore,name2
has read only the first part of " New York" and the
second part is automatically assigned to name3.However,
during the second run,the string "New York" is correctly
to name2.

Share |

Labels:

0 comments:

Post a Comment