Skip to main content

Simple Structure Example Program

1 min read
Share:
On this page (7sections)

About this program

This is an example program in c structure and union programs. Read the concept first: C Structures, then study the code and output below.

Definition

  • C Structure is a collection of different data types which are grouped together under a common name.
  • Each element in a Structure is called structure member variables.
  • Structure member variables may in same or different datatypes.

Syntax:

struct tag_name {
   type member1;
   type member2;
   /* declare as many members as desired*/
};

Simple Structure Example Program


/*##Simple Structure Example Program with declaration,assign and Print Values*/
/*##Simple Structure Programs,strcpy Example*/

#include <stdio.h>
#include <string.h>
 
// Define Strcutre 
struct student 
{
           int id;
           char name[20];
           int mark;
};
 
int main() 
{
	// Declare Strcutre Variable
	struct student record;
 
	// Assign Values for Strcutre Memebrs : id,name and mark
	record.id=1001;
	
	// Assign Values name using strcpy function;
	strcpy(record.name, "Balan");    
	record.mark = 89;
 
	// Print Strcutre Memebrs Values
	printf("Student Id   : %d \n", record.id);
	printf("Student Name : %s \n", record.name);
	printf("Student Mark : %d \n", record.mark);
	
	return 0;
}

Sample Output:

Student Id   : 1001 
Student Name : Balan 
Student Mark : 89 

Learn the concept first, then study the code:

Related Tutorials

Search tutorials