Skip to content

Commit cd3b426

Browse files
committed
Added endian.c which identifies machine is Little/Big Endian
1 parent c7be60d commit cd3b426

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ These program are written in codeblocks ide for windows. These programs are not
100100
- [Stack implemenation of linklist](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Stack%20-%20Linked%20List.c)
101101
- [Swap integers without 3rd variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapIntegers.c)
102102
- [Swap value without third variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapValueWithoutUsingThirdVariable.c)
103+
- [Identify machine is big-endian or little-endian] (https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/endian.c)
103104

104105

105106

endian.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*************************************************************************************/
2+
// Since size of character is 1 byte when the character pointer is de-referenced
3+
// it will contain only first byte of integer.
4+
// If machine is little endian then *c will be 1 (because last byte is stored first)
5+
// if machine is big endian then *c will be 0.
6+
7+
// higher memory
8+
// ----->
9+
// +----+----+----+----+
10+
// |0x01|0x00|0x00|0x00|
11+
// +----+----+----+----+
12+
// c
13+
// |
14+
// &i
15+
16+
// +----+----+----+----+
17+
// |0x00|0x00|0x00|0x01|
18+
// +----+----+----+----+
19+
// c
20+
// |
21+
// &i
22+
/*************************************************************************************/
23+
24+
#include <stdio.h>
25+
int main()
26+
{
27+
unsigned int i = 1;
28+
char *c = (char*)&i;
29+
if (*c)
30+
printf("Little endian\n");
31+
else
32+
printf("Big endian\n");
33+
return 0;
34+
}

0 commit comments

Comments
 (0)