Skip to main content

Structure in C Programming Language

Structure in C Programming Language

In this tutorial, We will learn structure in C programming language.

Introduction

In C programming language, A structure is another user-defined data type available that allows a programmer to combine data items of a different type.

Declare Structure in C

To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member
 struct [structure tag] {  
   member definition;  
   member definition;  
   ...  
   member definition;  
 } [one or more structure variables];   

 struct Books {  
   char title[50];  
   char author[50];  
   char subject[100];  
   int  book_id;  
 } book;   

Access Structure in C

To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access.
 struct Books Book1;    /* Declare Book1 of type Book */  
 Book1.book_id = 6495407;  

Example 

 #include <stdio.h>  
 #include <string.h>  
 struct Books {  
   char title[50];  
   char author[50];  
   char subject[100];  
   int  book_id;  
 };  
 int main( ) {  
   struct Books Book1;    /* Declare Book1 of type Book */  
   struct Books Book2;    /* Declare Book2 of type Book */  
   /* book 1 specification */  
   strcpy( Book1.title, "C Programming");  
   strcpy( Book1.author, "Nuha Ali");   
   strcpy( Book1.subject, "C Programming Tutorial");  
   Book1.book_id = 6495407;  
   /* book 2 specification */  
   strcpy( Book2.title, "Telecom Billing");  
   strcpy( Book2.author, "Zara Ali");  
   strcpy( Book2.subject, "Telecom Billing Tutorial");  
   Book2.book_id = 6495700;  
   /* print Book1 info */  
   printf( "Book 1 title : %s\n", Book1.title);  
   printf( "Book 1 author : %s\n", Book1.author);  
   printf( "Book 1 subject : %s\n", Book1.subject);  
   printf( "Book 1 book_id : %d\n", Book1.book_id);  
   /* print Book2 info */  
   printf( "Book 2 title : %s\n", Book2.title);  
   printf( "Book 2 author : %s\n", Book2.author);  
   printf( "Book 2 subject : %s\n", Book2.subject);  
   printf( "Book 2 book_id : %d\n", Book2.book_id);  
   return 0;  
 }  

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Comments

Popular posts from this blog

OFFSET in Computer Science

OFFSET in Computer Science In the post, We will understand what is the meaning of OFFSET in computer science with an example. In computer science offset comes picture at may place like packing a data structure, encoding the data etc in some standard protocol so that other parties can deserialise it and understand the data. We will understand its important how does it work with example. suppose we have a data structure for a student like { name:string[20] roll: int[4] course: string[25] batch: int[4] } We want to serialise below student data  { name:"Amit Gupta" roll: 1234 course:Computer Science" batch: 2018 } Our protocol follow the rule that we will insert a value and if space will remain empty we will fill it with $ for example length of "Amit Gupta" is 10 but standard size is 20 so will fill 10 $ after "Amit Gupta". So serialisation of above data will be Amit Gupta$$$$$$$$$$1234Computer Sc...

Function

Function A function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output. Representation - Cartesian  product  of 2 sets is mapping every element to every element of other set. It is generally represented as X A={"a","b"}, B={"y","z"} then A X B= {("a","y"),("a","z"),(b,y),(b,z)} Relation: A Relation R from a non-empty set A to a non empty set B is a subset of cartesian product A X B. n(A)= number of element in set A. n(B)= number of element in set B. Let 's say n(A)=p, n(B)=q n(A X B)=pq then no of relations possible from A to B is 2^pq A relation f from a set A to a set B is said to function if every element of set A has one and only one image in set B. Relation: A function f is X to Y then f: X->Y X is called domain set, Y is called range function. Functions are generally represented as  f ( x ) L...

Install Virtual Environment

VIRTUAL ENV Virtualenv is a tool to create an isolated Python environment. In this post, we will learn how to setup virtualenv on ubuntu. Prerequisite pip Install Virtualenv If you have pip3  sudo pip3 install virtualenv if you have pip sudo pip install virtualenv Create a Virtualenv virtualenv venv venv is a directory in which virtual environment will be created. Working with Virtualenv source venv/bin/activate Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

What is the difference between library and framework?

What is the difference between library and framework? In this post, We will learn about the very confusing term library and framework that widely used in software. Library and Framework Library: Library is a code repository that your code call. For example, curl is a library that can be called by your code. Framework: In a framework, framework call your code. For example, Django is a framework in which you keep your code in a particular structure can Django call your code compilation and execution purpose. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.