Freshers Aptitude technical questions
Freshers Job Alert
Bookmark and Share

C Programming Tutorial

Learn C programming in an easy way.This C programming tutorial is completly free for your use.

How you can help us back is let us know about more tutorials and send us the tutorial with your small photo and name.We will publish tutorial in your name.

 

<< Tutorial Home


STRUCTURES
A Structure is a data type suitable for grouping data elements together. Lets create a new data structure suitable for storing the date. The elements or fields which make up the structure use the four basic data types. As the storage requirements for a structure cannot be known by the compiler, a definition for the structure is first required. This allows the compiler to determine the storage allocation needed, and also identifies the various sub-fields of the structure.

struct date {
int month;
int day;
int year;
};
This declares a NEW data type called date. This date structure consists of three basic data elements, all of type integer. This is a definition to the compiler. It does not create any storage space and cannot be used as a variable. In essence, its a new data type keyword, like int and char, and can now be used to create variables. Other data structures may be defined as consisting of the same composition as the date structure,

struct date todays_date;
defines a variable called todays_date to be of the same data type as that of the newly defined data type struct date.

ASSIGNING VALUES TO STRUCTURE ELEMENTS
To assign todays date to the individual elements of the structure todays_date, the statement

todays_date.day = 21;
todays_date.month = 07;
todays_date.year = 1985;
is used. NOTE the use of the .element to reference the individual elements within todays_date.

/* Program to illustrate a structure */
#include <stdio.h>

struct date { /* global definition of type date */
int month;
int day;
int year;
};

main()
{

struct date today;

today.month = 10;
today.day = 14;
today.year = 1995;

printf("Todays date is %d/%d/%d.\n", \
today.month, today.day, today.year );
}

 

 

 

<< Tutorial Home