Home C-tutorials C Structure example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

C Structure example

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 6, 2009

Related Tutorials

Ask Questions?    Discuss: C Structure example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Shiben
June 3, 2011
Isn't it Strange?

#include<stdio.h> struct student{ int a; char b[45]; }; void main() { struct student student1; printf("Size of struct: %d" ,sizeof(student1)); } The output in c99 :Size of struct :52 Why is that ?
dE_logics
June 9, 2011
Comment

Sheer stupidity. #include <conio.h> and getch(); It proves you know nothing but Windows.
sabelo
August 23, 2011
digital process control2

your examples are understoodable easy
viky
August 30, 2011
C Structures

struct student st; what is the use of the above statement in the code
Kapil Shishodia
September 10, 2011
strecture

good example ............. i want more example based on strecture.
manish kumar
September 13, 2011
c programming

structure and union........../ pointer in c
Piet
October 3, 2011
c Structure example

I cannot run the example, because the compilers does not know #include <conio.h> What can I do? thx
vinay
October 6, 2011
C

I want to how to pass structure variable to function in simple way.
mukul kshatriya
October 19, 2011
queriy related typedef stuct.

can u tell me how to create a typedef of a employee of a company having a attribute Name,Emp_ID,Dept Name,Designation.. same as for student of a collage with the attributes Name,Rollno,Section,Branch,Total amrks,Result.. do reply as soon as it is possible..
mohamed rizwan
December 15, 2011
HI

Its nice to see many tags for structure i hope this tags may use us in coding our new idea
madhusudan balai
December 21, 2011
Stack by pointer

#include <stdio.h> #include <stdlib.h> int MAX; int count = 0; struct node { struct node *next; int value; }; void push (struct node ** headref,int k) { if( count < MAX ) { struct node *newnode; newnode = malloc(sizeof(struct node)); newnode->value = k; newnode->next = *headref; *headref = newnode; count++; } else { printf(" Stack is Full "); } } void pop(struct node * headref) { int m; if (headref == NULL) { printf(" STACK IS EMPTY "); exit(0); } else { m = headref->value; printf(" POP OUT VALUE %d" ,m); } } int main() { struct node *head = NULL; int val, choice, i,j = 1; printf(" enter the MAXIMUM size for stack\n "); scanf(" %d " ,&MAX); while(j) { printf(" ENTER THE CASE NUMBER WHICH U WANT TO CHOOSE \n\t 1.FOR PUSH \n\t2.FOR POP\n "); scanf(" %d ",&choice); switch(choice) { case 1: printf("enter the value to be pushed "); scanf("%d",&val); push(&head,val); break; case 2: pop(head); head = head->next; break; default: printf(" invalid option\n "); } printf(" enter the one for continue\n "); scanf("%d",&j); } return 0; }
hanumant
January 21, 2012
learn c structure

i become perfert in to the structure in c
v.elango
January 25, 2012
c

palindrome
Amit Godsay
February 3, 2012
data structure

why linked list is useful than array and send the program of linked list
jafar sadiq e
February 12, 2012
a doubt

struct student st; in this, what "st" stands for..????
Ricachan
February 16, 2012
Thanks a lot :D

Thanks man! that was simple and very helpful.
sundar
February 29, 2012
c

fill detail about c..
benjamin ansong
May 11, 2012
c program

how to use functions in c structures.
suresh
May 15, 2012
doubt in structures in c

how can i access structure elemnts with out using dot and arrow aoperators
Mohammed Imtiyaz M
June 5, 2012
C,C++

struct student st; Can any one please explain above statement in detail.
Rahul kumar
June 28, 2012
Thanks for providing help about structure in C

Dear . I am very happy to get knowledge about this structure in C. Thanks.
sweta agrawal
July 10, 2012
ask question

please quickly give the answer of the ques.
suresh
March 9, 2013
c programs

how to solve in oops consept problems
pankaj
July 17, 2012
C

good explanation
pavithra
July 21, 2012
information of c language

studying c language
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
DMCA.com