C Structure example

Structures in C defines the group of contiguous (adjacent) fields, such as records or control blocks.

C Structure example

     

This section illustrates you the concept of structure in C.

Structures in C defines the group of contiguous (adjacent) fields, such as records or control blocks. A structure is a collection of variables grouped together under a single name. It provides an elegant and powerful way for keeping related data together. 

Structure Declaration:   

struct struct-name{
type field-name; 
type field-name; 
... };

Once the structure is defined, you can declare a structure variable by preceding the variable name by the structure type name. In the given example, a small structure i.e struct is created student and declared three instances of it as shown below.

struct student{
int id;
char *name;
float percentage;
}
  

In structures, we have assigned the values to the instances i.e, id, name, percentage in the following way:

student1.id=1;
student2.name = "Angelina";
student3.percentage = 90.5;

Here is the code:

STRUCTUR.C

#include <stdio.h>
#include <conio.h>

struct student {
  int id;
  char *name;
  float percentage;
student1, student2, student3;
int main() {
  struct student st;
  student1.id=1;
  student2.name = "Angelina";
  student3.percentage = 90.5;
  printf(" Id is: %d \n", student1.id);
  printf(" Name is: %s \n", student2.name);
  printf(" Percentage is: %f \n", student3.percentage);
  getch();
  return 0;
}

Output will be displayed as:

STRUCTUR.EXE

Download Source Code

Advertisements

Share on Google+Share on Google+

C Structure example

Posted on: February 6, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: C Structure example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:34
krishnakumari
August 1, 2012
structures

very nice
shuham
September 18, 2012
help to understand plz

struct stud { int roll ; char name[10]; struct stud *next; } what that line means 'struct stud *next'
DIVYA
October 3, 2012
c-structures

want programme for cheak validation of date
Mangesh
October 19, 2012
c quiz

what is the use of getch in c
abhinaya
November 11, 2012
c progaram

this is good
lapta
November 22, 2012
NO So Bad

Good Attempts
rahul sharma
November 24, 2012
structure in c

when some one ask for the simplest example of ...kindly do'nt use the pointer .... difne the sturct example ..in a very simple way so that begners cn understand it easily .............
rinku sehrawat
April 18, 2013
structure

w.a.p for structure record of a student
????
April 21, 2013
structure in c

good

Ads

 

Ads