Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ These program are written in codeblocks ide for windows. These programs are not
- [Stack implemenation of linklist](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Stack%20-%20Linked%20List.c)
- [Swap integers without 3rd variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapIntegers.c)
- [Swap value without third variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapValueWithoutUsingThirdVariable.c)
- [Identify machine is big-endian or little-endian] (https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/endian.c)



Expand Down
34 changes: 34 additions & 0 deletions endian.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*************************************************************************************/
// Since size of character is 1 byte when the character pointer is de-referenced
// it will contain only first byte of integer.
// If machine is little endian then *c will be 1 (because last byte is stored first)
// if machine is big endian then *c will be 0.

// higher memory
// ----->
// +----+----+----+----+
// |0x01|0x00|0x00|0x00|
// +----+----+----+----+
// c
// |
// &i

// +----+----+----+----+
// |0x00|0x00|0x00|0x01|
// +----+----+----+----+
// c
// |
// &i
/*************************************************************************************/

#include <stdio.h>
int main()
{
unsigned int i = 1;
char *c = (char*)&i;
if (*c)
printf("Little endian\n");
else
printf("Big endian\n");
return 0;
}