0% found this document useful (0 votes)
5 views23 pages

Understanding C Structures and Variables

Uploaded by

tejaskumar.7292
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)
5 views23 pages

Understanding C Structures and Variables

Uploaded by

tejaskumar.7292
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

STRUCTURES

 Structure:-
 A Structure is a collection of logically related elements, possibly of different types, having a single
name.
 Definition of a Structure:-
 A structure definition forms a template that may be used to crate structure objects. The variables
that make up the structure are called members of the structure.

 A structure can be defined using three different ways,

 Tagged Structure
 Structure Variables
 Typedef Structure
1) Tagged Structure:-
The structure definition associated with the structure name is referred as tagged structure.
It does not create an instance of a structure and does not allocate any memory.
The general form or syntax of tagged structure definition is as follows:-

struct tag_name
{
type1 member1;
type2 member2;
……………
……………
};

In the above syntax:-


 struct is the keyword which tells the compiler that a structure is being defined.
 Tag_name is the name of the structure.
 member1, member2 … are called members of the structure. The members are declared within
curly braces.
 The closing brace must end with the semicolon.
Example:-
Employee details:-
struct Employee
{
int Eid;
char Ename[20];
int Eage;
};

[Compiled and Executed by MAHESH REDDY CHALLA] Page 1


In the above code:-

 struct is the keyword which tells the compiler that a structure is being defined.
 Employee is an identifier representing the structure name (tag_name).
 Eid, Ename and age are members of a structure and are themselves are not variables. They do not
occupy any memory.

Note:
 Memory is not reserved for the structure definition since no variables are associated with
the structure definition. The members of the structure do not occupy any memory until they
are associated with the structure variables.
How to Declare a Structure variable:-
The structure can be declared in the following way:

struct tag_name var1,var2,… ........ , varN;


struct ------  keyword.
tag_name ----  is the name of the structure.
var1,var2,……., varN -------  are structure variables which are separated by comma and followed by
semicolon.
Note: - structure variables can be declared anywhere in the program.
For our employee structure we will create a structure variable like this:

struct Employee EMP;

 The above statement will create a structure variable for the structure employee.
 Once a structure variable is created memory is allocated for this structure variable.
 Now a variable of type struct Employee is created which is of derived type, the memory is
allocated for the variable EMP.
 In the structure Employee there are 3 structure members
1) Eid
2) Ename
3) Eage
 What is the size of Structure variable?
 Structure size depends upon the members of that structure.
 The size for the structure variable is sum of sizes of the individual members.
 In our example the size of structure variable EMP is 2+20+2 = 24 bytes.
 Because, the members of structure Employee are int, char int types. So, sum of individual
members size = 2 bytes+20 bytes+ 2 bytes (Sum =24 bytes).

Note: - Structure definition appears at the beginning of the program file, before any variables or
functions are defined.

2) structure variables:-

[Compiled and Executed by MAHESH REDDY CHALLA] Page 2


 Here the structure template and structure variable declaration is done in one statement.
 We refer this as structure variables.
 The declaration is as follows:-

struct tag_name
{
type1 member1;
type2 member2;
……………
} var1, var2…;

In the above declaration:-


struct ---  keyword.
tag_name-- is the name of the structure.
Member1, member2, …. ---- are called members of the structure.
Var1, var2, … -----  are structure variables. Each variable occupies memory.

Example:
Employee details:-
struct Employee
{
int Eid;
char Ename[20];
int Eage;
}EMP1,EMP2,EMP3;

Here EMP1,EMP2,EMP3 are three structure variables which represents three employees.

3) Type-defined structure:-

 The structure definition associated with keyword typedef is called type-defined structure.
 This is the most powerful way of defining the structure.
 The syntax of type- defined structure is

typedef struct
{
type1 member1;
type2 member2;
……
} TYPE_ID;

[Compiled and Executed by MAHESH REDDY CHALLA] Page 3


Here
typedef --- is keyword added to the beginning of the definition.
srtuct --- is the keyword which tells the compiler that a structure is being defined.
member1, member2… ---  are called members of the structure.
TYPE_ID ----- it is type definition name.
Example:-
typedef struct
{
int Eid;
char Ename[20];
int Eage;
} Employee;

Note: - by using typedef it is not possible to declare a variable. But, we can have
user defined data type. TYPE_ID will be treated as the new data type.

 INITIALIZATION OF STRUCTURES:-

 Initialization of structures is similar to array initialization.


 Values or Initializers are enclosed in flower braces and separated by commas.
 The values should match their corresponding types in the structure definition.
 The syntax is shown below:-

struct tag_name variable = {value1, value2,… valuen};

 Initialization of structures can be done in different styles they are:-


1) Initialization along with structure definition:-
 In this style the structure definition, structure variable declaration and initialization
of structure variable is done in one statement.
Example:-
struct Employee
{
int Eid;
char Ename[20];
int Eage;
} EMP1 = {514, “Ashish kumar”, 19};

2) Initialization during structure declaration:-


 In this style structure is declared and initialized.
Example:-

struct Employee
{
int Eid;

[Compiled and Executed by MAHESH REDDY CHALLA] Page 4


char Ename[20];
int Eage;
};

struct Employee EMP1 = {514, “Ashish kumar”, 19};

 ACCESSING STRUCTURES:-

 The structure members can be accessed and manipulated.


 To access the structure members we have to use dot operator (.).
 Structures use a dot operator to refer its elements. The dot operator is also known as period
operator.
 To access the structure members we have to follow the following syntax:-

.
structure_variable_name structure_member_name

 Before dot, there must always be a structure variable. After the dot, there must always be a
structure element.

1)./* W.A.P to create a structure which consists of employee details {Example


of a tagged structure */

#include <stdio.h>
struct Employee
{
int Eid;
char Ename[20];
int Eage;
};
int main()
{
struct EMP1,EMP2;
printf(“\n\t Enter Eid for Emp1:”);
scanf(“%d”,&[Link]);
printf(“\n\t Enter Ename for Emp1:”);
scanf(“%s”,[Link]);
printf(“\n\t Enter Eage for Emp1:”);
scanf(“%d”,&[Link]);
printf(“\n\t EMP1 Eid : %d”, [Link]);
printf(“\n\t EMP1 Ename : %s”, [Link]);
printf(“\n\t EMP1 Eage : %d”, [Link]);
printf(“\n\t Enter Eid for Emp2:”);
scanf(“%d”,&[Link]);
printf(“\n\t Enter Ename for Emp2:”);
scanf(“%s”,[Link]);
printf(“\n\t Enter Eage for Emp2:”);

[Compiled and Executed by MAHESH REDDY CHALLA] Page 5


scanf(“%d”,&[Link]);
printf(“\n\t EMP2 Eid : %d”, [Link]);
printf(“\n\t EMP2 Ename : %s”, [Link]);
printf(“\n\t EMP2 Eage : %d”, [Link]);
return 0;
}
Note:-
In the above program initialization of structure is done at runtime.
Try to write the above program to initialize the structure at compile time.

2) /* W.A.P in C to create a structure which should consist the student information


{Using the concept of structure variable style}

#include <stdio.h>
struct Student
{
char Sname[7];
char Sregno[11];
char Sbranch[4];
}s1,s2;

int main()
{
clrscr();
fflush(stdin);
printf("\n\t Enter student s1 Name:");
gets([Link]);
printf("\n\t Enter student s1 registered Number:");
gets([Link]);
printf("\n\t Enter student s1 Branch:");
gets([Link]);
printf("\n\t Enter student s2 Name:");
gets([Link]);
printf("\n\t Enter student s2 registered Number:");
gets([Link]);
printf("\n\t Enter student s2 Branch:");
gets([Link]);

printf("Student1 Info: \n");


printf(" \n");
printf("\nSname:\t");
puts([Link]);
printf("\nSregno:\t");
puts([Link]);
printf("\nSbranch:\t");
puts([Link]);
printf("\n \n");
printf("\n");

printf("Student2 Info: \n");


printf("\n \n");
printf("\nSname:\t");
puts([Link]);
printf("\nSregno:\t");
puts([Link]);

[Compiled and Executed by MAHESH REDDY CHALLA] Page 6


printf("\nSbranch:\t");
puts([Link]);
printf("\n \n");
printf("\n");
printf("ThanQ for accessing our Database service!!");
getch();
return 0;
}

3) /* W.A.P in C to create the structure which consist States information {Using


typedef style}*/

#include <stdio.h>
typedef struct
{
char state_Name[15];
char state_bird[10];
char state_CM[3];
} States;

void main()
{

[Compiled and Executed by MAHESH REDDY CHALLA] Page 7


States s1,s2;
clrscr();
printf(“Enter state name:\n”);
gets(s1.state_Name);
printf(“Enter state bird:\n”);
gets(s1.state_Bird);
printf(“Enter name of state Chief minister :\n”);
gets(s1.state_CM);

printf(“Enter state name:\n”);


gets(s2.state_Name);
printf(“Enter state bird:\n”);
gets(s2.state_Bird);
printf(“Enter name of state Chief minister :\n”);
gets(s2.state_CM);

printf("State1 Info: \n");


printf("*********************\n");
printf("\n state_Name:\t");
puts(s1.state_Name);
printf("\nstate_Bird:\t");
puts(s1.state_Bird);
printf(“\n State chief minister:\t”);
puts(s1.state_CM);
printf(“*********************\n”);
printf("State2 Info: \n");
printf(" \n");
printf("\n state_Name:\t");
puts(s2.state_Name);
printf("\nstate_Bird:\t");
puts(s1.state_Bird);
printf(“\n State chief minister:\t”);
puts(s2.state_CM);
printf(“*********************\n”);

getch();
}

 OPERATIONS ON STRUCTURES:-
1) Copying of structure variables or members:-

 Copying of two structure variables is achieved using assignment operator. But, one
structure variable can be assigned to another structure variable of the same type.

Example:-

struct Student
{
char Sname[7];
char Sregno[11];
char Sbranch[4];
}s1,s2;

[Compiled and Executed by MAHESH REDDY CHALLA] Page 8


 In the above code s1 and s2 are structure variables of Student
type.
 So, we can copy one structure into another structure using
assignment operator in the following way:-

s2 = s1;

 By writing the above statement entire structure s1 will be copied into structure s2.
 We can also copy one structure members into another structure members in the
following way:
[Link] = [Link];

 By writing the above statement the member‟s sname of s1 structure gets copied into
sname of s2 structure.
 Note: - Structures or structure members can be copied only if they are of same type
otherwise copying is not possible.

2) Comparing to structures:-

 We cannot compare two structure variables. I.e. in our example s1 cannot be


compared with s2 and vice-versa. Example: - s1 == s2 // is illegal comparison is not
possible.
 But, we can compare members of one structure with other structure members.
Example: - [Link] == [Link]; // is legal
Note:- Members of two structure variables of same type can only be compared. Members of two
structure variables of different type cannot be compared.
3) Others operations:-

 Other operations like Arithmetic, Relational, Logical, Comparison, Incrementation,


Decrementation, etc.., are possible only on individual members of structure but not
on structure variables.

[Compiled and Executed by MAHESH REDDY CHALLA] Page 9


/* W.A.P in C to demonstrate the different types of operations on structures and
members of structure */
struct Student
{
char Sname[7];
char Sregno[11];
char Sbranch[4];
int rank;
};
int main()
{
struct Student s1= {"Mahesh","01861A0515","CSE",1};
struct Student s2;
clrscr();
s2 = s1; // Here we are copying s1 data into s2 data.
if(strcmp([Link],[Link])== 0)
{
//Compare Structure
members printf("Both structure Members are equal:\n");
}
else
{
printf("\n\tBoth structure Members are Not-equal:\n");
}
printf("\n\tRank:- %d\n",[Link]);
// Incrementation operation on structure member
[Link]++;
printf("\n\tRank after incrementing :- %d",[Link]);
getch();
}

 Complex structures Or Nested Structures:-

 If we nest one structure in another structure such structure is known as Nested Structure. (Or)
 A structure which includes another structure is called a Nested Structure i.e., structure can be
used as a member of another structure.
 The following is the syntax for nesting a structure:-

[Compiled and Executed by MAHESH REDDY CHALLA] Page 10


struct tag_name1
{
type1 member1;
…….
…….
};
struct tag_name2
{
type1 member1;
……
……
struct tag_name1 var;
……
};

 Syntax of the another method of nesting structure is as follows:-

struct tag_name1
{

type1 member1;
…….
…….
struct tag_name2
{
type1 member1;
……
……

} var;

};

 INITIALIZATION OF A NESTED STRUCTURE:-


 In a nested structure we will have two or more structures.
 Suppose assume that we have two structures one included in another structure, the structure
which is inside is called an Inner Structure and which is outside is called Outer Structure.
 Now, if we have to initialize the members of Outer Structure we have to use the following
syntax which is familiar to you:-

[Compiled and Executed by MAHESH REDDY CHALLA] Page 11


OuterStructure_variable_name .Outerstructure_member_name = value;

If we have to initialize the members of inner structure, we have to use the following
syntax:-

OuterStructure_variable_name .Innerstructure_variable_name.innerstructure_member_name
= value;

 Accessing Members of Inner Structure And Outer Structure:-

 Syntax to access the members of outer structure is already familiar to you.

Outerstructure_variable_name .Outerstructure_member_name;
 Syntax to access the members of outer structure members is as follows:-

Outerstructure_variable_name.Innerstructure_variable_name.innerstructure_member_name;

[Compiled and Executed by MAHESH REDDY CHALLA] Page 12


/* W.A.P in C to Demonstrate how to declare, initialize and access a Nested Structure */

In this example we use a nested structure which holds the data of a cricket player.

#include <stdio.h>

#include <string.h>

struct Playerinfo

char pname[30];

char country[20];

struct Trackrecord

int total_matches;

int runs_scored;

float strike_rate;

float Avg;

}tr;

int age;

};

void main()

struct Playerinfo p;

clrscr();

strcpy([Link],"Suresh Raina");

strcpy([Link],"India");

[Link] = 28;

[Link].total_matches = 211;
[Link].runs_scored = 5206;

[Link].strike_rate = 93.38;

[Link] = 35.41;

printf("\n\n \t\tPlayer Info:\n");

[Compiled and Executed by MAHESH REDDY CHALLA] Page 13


printf("\n\n \t\t Player Name:- \t \t");

puts([Link]);

printf("\n\n \t\t Age:- \t\t %d",[Link]);

printf("\n\n \t\t Country:- \t \t");

puts([Link]);

printf("\n \t \tTrack Record:-\n" );

printf("\n\n\t\t ");

printf("\n\n\t\t Total Matches Played:- \t %d",[Link].total_matches);

printf("\n\n\t\t Runs Scored:- \t\t %d",[Link].runs_scored);

printf("\n\n\t\t Strike Rate:- \t\t %f",[Link].strike_rate );

printf("\n\n\t\t Average :- \t\t %f",[Link]);

getch();

O/P :-

 Write the above program to initialize structure member’s at run time?

[Compiled and Executed by MAHESH REDDY CHALLA] Page 14


 STRUCTURES AND POINTERS:-
 The way we can have a pointer pointing to an int, or a pointer pointing to a char, similarly we can
have a pointer pointing to a struct. Such pointers are known as „structure pointers‟.

 To access the values of the members of the structure using pointers we need to perform following
things,

1. Declare structure variable: struct tag_name var;


2. Declare a pointer to a structure: struct tag_name *ptr;
3. Store the address of structure variable in structure pointer: ptr=&var;
4. Access the members of the structure.

 Members of the structures can be accessed in following ways.

1) Using De-Reference Operator * and Dot (.) Operator:-

 If ptr is a pointer to a structure, then the structure itself can be accessed using indirection
operator as shown below,

struct tag_Name *ptr;

// Refers to the whole structure

 Once the structure pointer is initialized, then each member of the structure can be
accessed using dot operator as shown below,

(*ptr).structure_member1

(*ptr).structure_member2

Let‟s take the following segment of code

struct student
{
int rno;
char name[10];
float avg;
};

struct student s1={101,”ABC”,78.98};


struct student *ptr;
ptr=&s1;

[Compiled and Executed by MAHESH REDDY CHALLA] Page 15


Now the members of the structure can be accessed by using the dot operator as shown
below,

(*ptr).name // Access the name


(*ptr).rno // Access roll number
(*ptr).avg // Access average

Note: The parenthesis to all three expressions is necessary. We should not omit the parenthesis.
For example,

*ptr .name // invalid way of accessing the member


2) Using Selection Operator (->):-
 If ptr is a pointer to structure, then the members of the structure can also be accessed using
selection operator denoted by ->(which is formed by minus sign and greater than symbol).
 Using this various members of the structure can be accessed as shown below,

ptr -> structrure_member

For the above example the members can be accessed as follows,


ptr - > name // Access the name
ptr - > rno // Access roll number
ptr - > avg // Access average
 Remember that on the left hand side of the dot operator, there must always be a structure
variable, whereas on the left hand side of -> operator there must be always be a pointer to
structure. This method is efficient, preferred over the previous method.

[Link] [Link] [Link]

101 ABC 78.98


4001 4003 4013

ptr
4001
5001

Memory Representation of Structure Pointer

[Compiled and Executed by MAHESH REDDY CHALLA] Page 16


/* C program to read and display student details using pointer to structure */

#include <stdio.h>
struct TrainRes
{
char P_name[30];
int age;
int train_no;
char coach[5];
int seat_no;
int breath_no;
char doj[25];
char source[30];
char destiny[40];
int fair;
};
int main()
{
struct TrainRes Tr;
struct TrainRes *Trs;
Trs = &Tr;
clrscr();
printf("Enter The details:\n");
printf("\n Enter P_name:\n");
gets(Trs->P_name);
printf("Enter age:\n");
scanf("%d",&Trs->age);
printf("Enter Train Number:\n");
scanf("%d",&Trs->train_no);
fflush(stdin);
printf("Enter Coach No:\n");
gets(Trs->coach);
printf("Enter seat Number:\n");
scanf("%d",&Trs->seat_no);
printf("Enter breath Number:\n");
scanf("%d",&Trs->breath_no);
fflush(stdin);
printf("Enter date of Journy:");
gets(Trs->doj);
printf("Enter Journy From:\n");
gets(Trs->source);
printf("Enter Journy To:\n");
gets(Trs->destiny);
fflush(stdin);
printf("Fair for Journy:\n");
scanf("%d",&Trs->fair);
printf("Displaying Details of passenger reservation:\n");
printf("****************************************");
printf("\n\tP_name:\t");
puts(Trs->P_name);
printf("\n\tage:\t %d",Trs->age);

[Compiled and Executed by MAHESH REDDY CHALLA] Page 17


printf("\n\n\tTrain Number:\t %d",Trs->train_no);
printf("\n\n\tCoach No:\t ");
puts(Trs->coach);
printf("\n\t Seat_no:\t %d\n",Trs->seat_no);
printf("\n\t breath_no:\t %d",Trs->breath_no);
printf("\n\t date of Journy:\t");
puts(Trs->doj);
printf("\n\tSource of Journy:\t");
puts(Trs->source);
printf("\n\tDestiny:\t");
puts(Trs->destiny);
printf("\n\tFair :\t %d",Trs->fair);
getch();
}O/P:-
Enter The details:
Enter P_name:
Mahesh Reddy
Enter age:
32
Enter Train Number:
7542
Enter Coach No:
S9
Enter seat Number:
9
Enter breath Number:
18
Enter date of Journy: 23/12/2015
Enter Journy From:
India
Enter Journy To:
Canada
Fair for Journy:
6000
Displaying Details of passenger reservation:
****************************************

P_name: Mahesh Reddy

age: 32
Train Number: 7542
Coach No: S9
Seat_no: 9
breath_no: 18
date of Journy: 23/12/2015

Source of Journy: India

Destiny: Canada

Fair : 6000

[Compiled and Executed by MAHESH REDDY CHALLA] Page 18


 STRUCTURES AND FUNCTIONS :-
 We should note that structures are more useful if we are able to pass them to functions and
return them. The structures members can be passed to the function in various ways as
shown below,

1. By passing individual members of structure


2. By passing the whole structure
3. By passing structures through pointers

 Passing Individual Members:-


 The first method is to pass each member of the structure as an actual argument of the
function call. The actual arguments are treated independently like ordinary variables.

// Passing individual structure elements

EXAMPLE:-
#include <stdio.h>
#include <conio.h>
struct Employee
{
char ename[30];
float sal;
char location[30];
};
void Empdata_display(char [], char []);
void main()
{
struct Employee e;
clrscr();
printf("Enter Employee name:\n");
gets([Link]);
printf("Enter employee sal:\n");
scanf("%f",&[Link]);
fflush(stdin);
printf("Enter Employee Location:\n");
gets([Link]);

Empdata_display([Link],[Link]);

getch();
}

void Empdata_display(char name[30], char loc[30])


{
printf("Details of Emp:\n");
printf("\n\t\tEmp_Name : \t");
puts(name);

[Compiled and Executed by MAHESH REDDY CHALLA] Page 19


printf("\n \t\t Location: \t");
puts(loc);
}

 In the above example the Empdata_display () is a called function which will display
the details of employee which are stored in structure.

 We will call this function by passing individual members of structure. In the below
statement we pass 2 members of structure individually.

Empdata_display([Link],[Link])

 ename and location are two members of a structure that we had passed to function.
The function will display the name of employee and his location by taking structure
members as parameters.

 Passing Whole Structure


 The second method involves passing a copy of the entire structure to the called
function. Any changes to structure members within the function are not reflected in the
original structure. It is therefore, necessary for the function to return the entire
structure back to the calling function.

 The general format of sending a copy of a structure to the called function is:

return_type function_name (structure_variable_name);

[Compiled and Executed by MAHESH REDDY CHALLA] Page 20


 The called function takes the following form:

return_type function_name(struct_type tag_name)


{
………
………
return(expression);
}

 The called function must be declared for its type, appropriate to the data type it is
expected to return.

 The structure variable used as the actual argument and the corresponding formal
argument in the called function must of the same struct type.

 The return statement is necessary only when the function is returning some data back
to the calling function. The expression may be any simple variable or structure
variable or an expression using simple variables.

 When a function returns a structure, it must be assigned to a structure of identical type


in the calling function. The called functions must be declared in the calling function
appropriately.

 Enumerated data type


 The concept of replacing integers by some descriptive names gives rise to new data
type called enumerated type.
 An enumerated data type is a user defined data type which can take the integer values
from a list.
 These integer values are replaced by meaningful and descriptive names so as to
enhance the readability of the program.
 These descriptive names are called enumerators or enumerator constants.

Syntax to define an enumerated type:-


enum type_Name
{
member1,
member2,
….
….
};
 enum is the keyword tells the compiler about enumerated type definition.
 enum type_Name together represent the user defined data type.
 member1, member2… are integer constants but represented using descriptive names.
These are called enumerator constants or enumerators.
 The definition terminated with a semicolon.
 The syntax for declaring the variables are shown below,

[Compiled and Executed by MAHESH REDDY CHALLA] Page 21


enum type_Name var;

Assigning Values to Enumerated Types:- After an enumerated variable has been


declared, we can store values in it. While, the compiler automatically assigns values to
enumerated types starting with 0, the next values are initialized with a value by adding 1 to
previous value.
Example :-
enum behavior
{
Good, Average,Bad
};
void main()
{
enum behavior student;
student = Good;
if( student == Good)
{
printf(“ Student behavior is Good %d”, Good);
}
}
O/P :- Student behavior is Good 0

 Bit field:-
 A bit field is a set of adjacent bits whose size can be from 1 to 16 bits length. A
word can therefore be divided in to a number of bit fields. The name and size of bit
fields are defined using structure. Since bit fields are defined within a structure, the
various bits can be accessed in a way we access individual members of a structure.

The general for or syntax for bit field definition as follows,

struct tag_Name
{
Unsigned int name1: bit_length;
Unsigned int name2: bit_length;


Unsigned int namen: bit_length;

};

[Compiled and Executed by MAHESH REDDY CHALLA] Page 22


Where,

 unsigned int is always used to define bit fields.


 bit_length is the number of bits used for the specified name.
 field name is followed by semicolon.

 Remember that a signed bit field should have at least 2bits (1bit for sign). The bit_length is
decided by the range of value stored. The largest value that can be stored is 2 n-1, where n is
bit_length.

 The internal representation of bit fields is machine dependent. Some machines store bits from
left to right and others from right to left. The below Figure illustrates the layout of bit fields,
assuming a 16-bit word that is ordered from right to left.

There are several specific points to observe:

 The first field always starts with first bit of the word.
 A bit can not overlap integer boundaries. This is, the sum of lengths of all fields in a
structure should not be more than the size of a word. In case, it is more, the overlapping
field is automatically forced to the beginning of the next word.
 There can be unnamed field declared with size. Example,
unsigned : bit_length; Such fields provide within a word.

 There can be unused bits in a word.


 We cannot take the address of a bit field variable.
 Bit fields cannot be arrayed.
 Bit fields should be assigned values that are the range of their size.

[Compiled and Executed by MAHESH REDDY CHALLA] Page 23

You might also like