ss_blog_claim=726e70d7c87c20ae33aa7a61f06eb8aa

Friday, November 28, 2008

The usage of ‘gets’ statement in C Programming

Well we have already discussed about the usage of ‘fflush’ statement, let us now discuss about the usage of ‘gets’ statement. String is an array of characters and we use the format specifier ‘%s’ to directly get these array of characters, which is not possible in the case of array of integers. In the case of array of integers we use the ‘for’ loop to get the values since we don't have any format specifier for array of integers.

To get array of characters we use the format specifier ‘%s’ as shown below

puts(“Enter the string”);
scanf(“%s”,ex_str);
puts(ex_str);

Output:

Enter a string
sample text
sample

What is wrong in the output?? We know that ‘Space’ and ‘Enter’ key act as a delimiter. So if you execute this program and give input as ‘sample text’ the value stored in ‘ex_str’ is ‘sample’ and not ‘sample text’ this is because the space in between the words ‘sample’ and ‘text’ acts as a delimiter thus storing only ‘sample’ in the character array (string). To overcome this problem we use the ‘gets’ statement. The ‘gets’ statement takes only the ‘Enter’ key as delimiter. The below part of the code shows how to use ‘gets’ statement.

puts(“Enter a string”);
gets(ex_str);
puts(ex_str);

Output:

Enter a string
sample text

sample text

Just think of getting a name of a person which contains first name, last name and middle name so this ‘gets’ statement is much useful in those situations. Happy Programming!!

7 comments:

fengfk2008 said...
This post has been removed by a blog administrator.
fengfk2008 said...
This post has been removed by a blog administrator.
Hapi said...

hello... hapi blogging... have a nice day! just visiting here....

indavao said...

hi... just dropping by!
http://www.fileafro.com
http://mobileandetc.blogspot.com
http://kantahanan.blogspot.com

Modern Sport Watches said...

Rare Seiko is biggest watches old seiko watches industry. Made the first self-winding wristwatch vintage 1970's watches.Seiko timepieces are
designed to be water resistant.Collections Elite Collections Tressia and La Grand Sport, which includes the
Arctura ,Sportura and Coutura. Most selling items are Kinetic Perpetual, Kinetic Chronograph, Kinetic Auto Relay, Kinetic, Analog Digital,Chronograph, Flight Computer and Perpetual Calendar
For more information on quality luxury watches

Maharaja Hookah Shisha said...

The Mya is a truly remarkable Hookah that encompasses detail with its ornate design and unique vase. The stem may be disassembled for storage and easy cleaning. The Mya Hookah is stored in a hard style carrying case that contains a security combination code for comfort and privacy.

http://www.maharajahookah.com/mya-hookah/cat_22.html
http://www.maharajahookah.com/mya-hookah/new-mya-hookah-shisha-tall-hookah/prod_462.html

Mya Hookah
Blue Tall Mya Hookah Shisha

tagskie said...

hi.. just dropping by here... have a nice day! http://kantahanan.blogspot.com/