C/C language
Expert: Prince M. Premnath - 11/30/2006
Questionhell Prince M Premnath will u plz tell me about functions and structure with a simple examplr?
AnswerDear qamar ul islam!
Function is a set of valid C statements , that used to perform a given task , whereas a structure is simply a memory location used to store different type of data's consecutively .
Eg for function !
here i present a simple function ,
void foo()
{
printf("Hello world!");
}
This is a simple function , its objective is to display the text "Hello World!" , this operation is quit simple ,functions are used to perform complex operations , and provides lot of advanteges
the above function can be used in main() as follows
void main()
{
foo();
}
STRUCTURE!
As i said earlier structure used to store different type of data's consecutively
int values can be stored in int data type similarly for float and other available data types
In a real time application a subject exhibits different properties say( A customer ia s subject his properties are his name , age , a/c no and so on ) there is no datatype available by default to store this type of MIXED data
C provide a best tool to solve this problem Ie : by collecting required data types as a group called structure!
a structure for the above said subject can be defined as follows
struct customer
{
int CustID;
char name[202];
int age;
float balance;
};
struct customer c;
So all the information regarding a customer can be stored in a single data type ( in this case the variable is C ).
Note : Structure can also used to store numerous number of customers if its used as an array !
Regards!
Prince M. Premnath.