structure in c
//structures are user defined data types in c
//in structures we can store any type of data types like int floate char etc
//declaring a structure
#include <stdio.h>
#include <string.h>
struct library //here strut is a keyword for creating a structure, here library also called structure tag
{
char name[20]; //members or elements of library tag or structure name
int pages;
char author[20];//character array or string of size 20, we have to use string for a char value
//there are two methods to initialize a variable from structure
}; //book1; //method 1
int main()
{
struct library book1,book2; //method 2
//there are two ways to assign values to the variables
//struct library book1={'h',2,'s'}; //way 1,but this is an error, like this we can only assign int values not char
strcpy(book1.name,"softwareharish"); //way 2
book1.pages=100;
strcpy(book1.author,"N Harish");//for assigning a char value we have to use strcpy() from <string.h>
//accessing structure members
printf("name of the book:%s\n",book1.name); //here dot (.) is called structure member operator
printf("author of the book:%s\n",book1.author);
printf("number of pages of the book:%d",book1.pages);
return 0;
}
Comments
Post a Comment