Skip to content

Added endian.c which identifies machine is Little/Big Endian #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2019
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;
}