C Structures

  • C structure is collection of different types of data elements in a single entity.
  • In C programming language data types are classified into three types.
    1. Primitive Data Types
    2. Examples: int, char,float, double

    3. Derived Data Types
    4. Examples: pointer,array, string

    5. User-defined Data Types
    6. Examples: Structure, Union, Enum

  • All predefined and derived data types are design to work with basic data elements only like integer or character or float types only.
  • In real world every information will be there in object format i.e. it contains their own properties and behaviors.
  • In implementation whenever a predefined or derived data types are not supporting user requirements then go for user-defined data types.
  • By using structures we can create our own data types, i.e. user-defined data types.
  • Structure is a combination of primitive and derived datatype members.
  • C programming language structure contains data members only where as in c++ data members and function also.
  • The size of the structure is sum of all member variable size.
  • The least size of structure is 1 Byte.
  • In C programming language empty structure are not possible.

Syntax:

struct struct_name
{
datatype member1;
datatype member2;
datatype member3;
datatype member4;
————–
————–
};

At the end of structure creation (;) must be required, it indicates that an entity is constructed.

Examples:

struct student
{
int rno;
char sname[20];
char fname[20];
int marks[6];
int tmakrs;
float avg;
};

Syntax to create structure type variable:

struct tagname variable;

Syntax to create structure type pointer:

struct tagname *ptr;

Syntax to create structure type array:

struct tagname arr[size];

Powered by k2schools