File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,7 @@ These program are written in codeblocks ide for windows. These programs are not
100
100
- [ Stack implemenation of linklist] ( https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Stack%20-%20Linked%20List.c )
101
101
- [ Swap integers without 3rd variable] ( https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapIntegers.c )
102
102
- [ 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 )
103
104
104
105
105
106
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments