Thursday, 7 March 2013

STRUCTURE

A structure contains a number of data types grouped together. These data types may or may not be of the same type. The following example illustrates the use of this data type.
main( )
{
    struct book
    {
        char name ;
        float price ;
        int pages ;
    } ;

    struct book b1, b2, b3 ;
    printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
    scanf ( "%c %f %d", &b1.name, &b1.price, &b1.pages ) ;
    scanf ( "%c %f %d", &b2.name, &b2.price, &b2.pages ) ;
    scanf ( "%c %f %d", &b3.name, &b3.price, &b3.pages ) ;
    printf ( "\nAnd this is what you entered" ) ;
    printf ( "\n%c %f %d", b1.name, b1.price, b1.pages ) ;
    printf ( "\n%c %f %d", b2.name, b2.price, b2.pages ) ;
    printf ( "\n%c %f %d", b3.name, b3.price, b3.pages ) ;
}

And here is the output...
Enter names, prices and no. of pages of 3 books
A 100.00 354
C 256.50 682
F 233.70 512


And this is what you entered


A 100.000000 354
C 256.500000 682
F 233.700000 512



This program demonstrates two fundamental aspects of structures:
(a) declaration of a structure

    struct book
    {
        char name ;
        float price ;
        int pages ;
    } ;
    struct book b1, b2, b3 ;

(b) accessing of structure elements

    b3.name, b3.price, b3.pages




Source By Let Us C "Yashwant Kanetkar"

No comments:

Post a comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...