Offers
Thanks for visiting www.technoexam.com. Stay connected with us for more latest programming updates...

You are here : Home >> Tutorials >> C

Go
Basic C Tutorial

Contents
What is C ?
History
Structure of C Program
Variables & Keywords
Constants
Data Types
Operators
Operators Precedence
Conditional Statements
if Statement
if else Statement
nested if else
switch case
Looping Statements
While loop
Do While loop
For loop
Break
Continue
Goto
Functions
Passing Value
Returning Value
Passing & Returning
Recursion
Storage Class
Array
Structure
Pointer
Union
Strings
Header File
Programs Library
Projects Library
Learn More+

Contact Us

+91-8806583119

Structures in C

  1. Array in Structures
  2. Structure with Array
  3. Structures within Structures (Nested Structures)

Structure is user defined data type which is used to store heterogeneous data under unique name. Keyword 'struct' is used to declare structure.

The variables which are declared inside the structure are called as 'members of structure'.

Syntax:

struct structure_nm
{
	<data-type> element 1;
	<data-type> element 2;
	- - - - - - - - - - -
	- - - - - - - - - - -
	<data-type> element n;
}struct_var;

Example : struct emp_info { char emp_id[10]; char nm[100]; float sal; }emp;

Note :

1. Structure is always terminated with semicolon (;).

2. Structure name as emp_info can be later used to declare structure variables of its type in a program.

Instances of Structure in C

Instances of structure can be created in two ways as,

Instance 1:

struct emp_info
{
	char emp_id[10];
	char nm[100];
	float sal;
}emp;

Instance 2: struct emp_info { char emp_id[10]; char nm[100]; float sal; }; struct emp_info emp;

In above example, emp_info is a simple structure which consists of structure members as Employee ID(emp_id), Employee Name(nm), Employee Salary(sal).

Accessing Structure Members in C

Structure members can be accessed using member operator '.' . It is also called as 'dot operator' or 'period operator'.

structure_var.member;

Program :

// Program to demonstrate structure
#include <stdio.h>
#include <conio.h>

struct comp_info
{
	char nm[100];
	char addr[100];
}info;

int main(void)
{
	clrscr();
	printf("\n Enter Company Name : ");
	gets(info.nm);
	printf("\n Enter Address : ");
	gets(info.addr);
	printf("\n\n Company Name : %s",info.nm);
	printf("\n\n Address : %s",info.addr);
	getch();
	return 0;
}

Output :

 Enter Company Name : TechnoExam, Technowell Web Solutions
 Enter Address : Sangli, Maharashtra, INDIA
 
 Company Name : TechnoExam, Technowell Web Solutions
 Address : Sangli, Maharashtra, INDIA_

Download PDF
Published on: 12th Nov 2010, Last Updated on: 18th Dec 2014

Mr. Dinesh Kudache
AUTHOR: Mr. Dinesh Kudache

Mr. Dinesh is the Founder and CEO of technoexam.com. He had 6+ years experience in Application Programming, System Programming, Web Design and Development, Google Expertise. He had written 5000+ various articles on programming. Browse more articles by this author.

Link this post on your Blog/Website :