0% found this document useful (0 votes)
98 views

Structures and Unions in C

Structures and unions allow users to define custom data types that can store multiple data types. Structures store different data types similarly to an array, while unions store one data type at a time, using less memory than structures. Typedef allows assigning an alternative name to existing data types for clarity. Structures and unions are defined using the struct and union keywords respectively, with members accessed via the dot operator. Typedef can also be used to define custom structure types for improved readability and maintenance of code.

Uploaded by

Amlan Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Structures and Unions in C

Structures and unions allow users to define custom data types that can store multiple data types. Structures store different data types similarly to an array, while unions store one data type at a time, using less memory than structures. Typedef allows assigning an alternative name to existing data types for clarity. Structures and unions are defined using the struct and union keywords respectively, with members accessed via the dot operator. Typedef can also be used to define custom structure types for improved readability and maintenance of code.

Uploaded by

Amlan Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Structures and Unions

Structures in C
• The structure is a user-defined data type in C, which is used to store a collection of
different kinds of data.

• The structure is something similar to an array; the only difference is array is used to
store the same data types.

• struct keyword is used to declare the structure in C.

• Variables inside the structure are called members of the structure.

• Syntax for Defining a Structure in C:


struct structureName Example:
{ struct Courses
//member definitions {
char WebSite[50];
};
char Subject[50];
int Price;
};
Accessing Structure Members in C
#include<stdio.h>
#include<string.h>
struct Courses
{
char WebSite[50];
char Subject[50];
int Price;
};
void main( )
{
struct Courses C;

//Initialization
strcpy( C.WebSite, “abc.in");
strcpy( C.Subject, "The C Programming Language");
C.Price = 100;

//Print
Output:
printf( "WebSite : %s\n", C.WebSite); WebSite : abc.in
printf( "Book Author : %s\n", C.Subject); Book Author: The C Programming Language
printf( "Book Price : %d\n", C.Price); Book Price : 100
}
Unions in C
• Unions are user-defined data type in C, which is used to store a collection of different
kinds of data, just like a structure.

• However, with unions, information can be stored in one field at any one time.

• Unions are like structures but uses less memory.

• The keyword union is used to declare the structure in C.

• Variables inside the union are called members of the union.

• Syntax for Defining an Union in C:


Example:
union unionName
union Courses
{ {
//member definitions char WebSite[50];
}; char Subject[50];
int Price;
};
Accessing Union Members in C
#include<stdio.h>
#include<string.h>
union Courses
{
char WebSite[50];
char Subject[50];
int Price;
};
void main( )
{
union Courses C;

strcpy( C.WebSite, “abc.in");


printf( "WebSite : %s\n", C.WebSite);

strcpy( C.Subject, "The C Programming Language");


printf( "Book Author : %s\n", C.Subject);
Output:
C.Price = 100; WebSite : abc.in
Book Author: The C Programming Language
printf( "Book Price : %d\n", C.Price);
Book Price : 100
}
typedef in C
• typedef is a C keyword implemented to tell the compiler for assigning an
alternative name to C's already exist data types.

• Syntax:
– typedef <existing_names_of_datatype> <alias__userGiven_name>;

• Example
– typedef signed long slong;
• slong in the statement as mentioned above is used for a defining a
signed qualified long kind of data type. Now the thing is this 'slong',
which is an user-defined identifier can be implemented in your program
for defining any signed long variable type within your C program.
• This means:
slong g, d;
will allow creation of two variables name 'g' and 'd' which will be of type
signed long and this quality of signed long is getting detected from the
slong (typedef), which already defined the meaning of slong in your
program.
Use of typedef in Structures
• The concept of typedef can be implemented for defining a user-defined data type
with a specific name and type. This typedef can also be used with structures of C
language.

Example:
• Syntax:
#include<stdio.h>
typedef struct #include<string.h>
{ typedef struct professor
{
type first_member; char p_name[50];
type sec_member; int p_sal;
}
type thrid_member; prof;
}
nameOfType; void main(void)
{
prof pf;
• typedef can be used with pointers as well: printf("\n Enter Professor details: \n \n");
printf("\n Enter Professor name:\t");
• Syntax: scanf("% s", pf.p_name);
typedef int* pntr; printf("\n Enter professor salary: \t");
pntr g, h, i; scanf("% d", &pf.p_sal);
printf("\n Input done ! ");
}

You might also like