Home > Backend Development > C++ > Write a sample program about structure using C language

Write a sample program about structure using C language

王林
Release: 2023-08-27 12:01:18
forward
820 people have browsed it

Write a sample program about structure using C language

The structure is a collection of different datatype variables, grouped together under a single name Syntax.

Declaration and initialization of structures

The general form of structure declaration is as follows −

datatype member1;
struct tagname{
   datatype member2;
   datatype member n;
};
Copy after login

Here, struct - keyword

tagname - specifies the name of the structure

member1, member2 - specifies the data items that make up the structure.

Example

struct book{
   int pages;
   char author [30];
   float price;
};
Copy after login

Structure variables

There are three ways to declare structure variables. They are as follows −

1) struct book{
   int pages;
   char author[30];
   float price;
}b;
2) struct{
   int pages;
   char author[30];
   float price;
}b;
3) struct book{
   int pages;
   char author[30];
   float price;
};
struct book b;
Copy after login

Initialization and access of structures

  • The link between members and structure variables is established through the member operator (or dot operator) .

  • Initialization can be done in the following ways:

Method 1

struct book{
   int pages;
   char author[30];
   float price;
} b = {100, “balu", 325.75};
Copy after login

Method 2

struct book{
   int pages;
   char author[30];
   float price;
};
struct book b = {100, “balu", 325.75};
Copy after login

Method 3 (using member operator)

struct book{
   int pages;
   char author[30];
   float price;
} ;
struct book b;
   b. pages = 100;
   strcpy (b.author, “balu");
   b.price = 325.75;
Copy after login

Method 4 (using scanf function)

struct book{
   int pages;
   char author[30];
   float price;
} ;
struct book b;
   scanf (“%d", &b.pages);
   scanf (“%s", b.author);
   scanf (“%f", &b. price);
Copy after login

We can print the contents of either of the above structures in the main method as shown below −

main ( ){
   struct book b;
   clrscr ( );
   printf ( "enter no of pages, author, price of book");
   scanf ("%d%s%f", &b.pages, b.author, &b.price);
   printf("Details of book are");
   printf("pages =%d, author = %s, price = %f", b.pages, b.author, b.price);
   getch();
}
Copy after login

Example

Following is another example of structures −

Live Demo

#include<stdio.h>
struct aaa{
   struct aaa *prev;
   int i;
   struct aaa *next;
};
main(){
   struct aaa abc,def,ghi,jkl;
   int x=100;
   abc.i=0;
   abc.prev=&jkl;
   abc.next=&def;
   def.i=1;
   def.prev=&abc;
   def.next=&ghi;
   ghi.i=2;ghi.prev=&def;
   ghi.next=&jkl;
   jkl.i=3;
   jkl.prev=&ghi;
   jkl.next=&abc;
   x=abc.next->next->prev->next->i;
   printf("%d",x);
}
Copy after login

Output

2
Copy after login

The above is the detailed content of Write a sample program about structure using C language. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template