This blog is an effort to demystify how Linux kernel & modern embedded os works in simplest language possible, It also gives insight into C programming language , Wi-Fi & LTE
Saturday, 21 July 2018
Tuesday, 5 June 2018
Typedef
typedef is a reserved keyword in the C and C++ programming languages. It is used to create an alias name for another data type.
Limitations :-
Enum
An enum is a keyword, it is an user defined data type. All properties of integer are apples to Enum. It is used for creating an user defined data type of integer. Using enum we can create sequence of integer constant value.
Key Points :
Key Points :
- With the help of enumeration we can put a name for constant.
- User readability improves;
- Default first member value is 0(zero)
- We cannot increment or decrement.
Syntax :
enum { red, black = 5, white, green };
Union
A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.
Key Points :
- It is an user defined data type;
- Union is a collection of different type of memory but not in contiguous form;
- In union all member will share same location;
- Size of union depends upon the size of the largest member;
Syntax :
union tagname
{
member 1;
member 2;
member 3;
.
.
.
};
STRUCTURE - 3 (Structure padding)
"Allocating the memory for a structure variable more then the required memory is called structure padding, that extra byte of memory is called hole in a structure;"
struct A
{
int a;
char c;
float f;
};
Size of struct A should be 9 but it is 12, that is known as structure padding and extra 3 byte are known as hole.
For removing the hole we can use below keyword:
#pragma pack(1) //only in GCC compiler
STRUCTURE - 2
Now we discuss some key features of structure :-
Structure Pointer :
Pointer size is dependent upon OS ,same applies for structure pointer. we will assume a 32-bit system.
Exp :
struct st
{
int a;
char b;
};
main()
{
struct st a1 ={10, p} , *ST;
ST = &a1;
for access :-
printf("%d %c",ST->a , ST->b );
Ways to Pass a structure in function :
1) print1(a1.a, a1.b);
2) print2(ST);
3) print3(&a1);
Structure Bit Field :
Structure Pointer :
Pointer size is dependent upon OS ,same applies for structure pointer. we will assume a 32-bit system.
Exp :
struct st
{
int a;
char b;
};
main()
{
struct st a1 ={10, p} , *ST;
ST = &a1;
for access :-
printf("%d %c",ST->a , ST->b );
Ways to Pass a structure in function :
1) print1(a1.a, a1.b);
2) print2(ST);
3) print3(&a1);
Structure Bit Field :
- Bit field is a mechanism in C-Language where we can allocate a memory in the form of Bits.
- Bit field can only be used in Structure and Union.
{
int a:6;
chat ch:3;
};
Here int take b bit and char take only 3;
Limitation of bit Field :
- We cannot allocate more bit size, then the actual size.
- bit field are applicable only for char and int.
- Getting address of bit field variable is possible.
Monday, 4 June 2018
STRUCTURE - 1
Structure is an user define data type which is collection of different primitive data types.
Syntax :
struct tagname
{
member 1;
member 2;
member 3;
.
.
.
};
- tagname is nothing but new data type;
- Until and unless we create a variable for structure there is no memory allocated for the member;
- If we want to access the member of structure through structure variable we use dot(.) operator;
- If we want to access the member of structure through structure pointer, we use arrow (->) operator;
Limitation of structure :-
- It is not possible to initialize the member of structure.
- Storage classes are not allowed for structure members but storage classes are allowed for variable of structure.
- In a structure we can't declare the same structure type but we can create a same structure pointer. (Self referential structure)
- Structure within structure is possible but the changed name (using typedef) structure is not possible in that structure.
Subscribe to:
Posts (Atom)




