[C language algorithm] Endianness

Implemented endian confirmation algorithm in C language

Environment used for learning

Reference material

Latest algorithm dictionary in C language (written by Haruhiko Okumura / 1991 first edition Gijutsu-Hyoronsha: 16 pages)

Endian overview

The order of high-order bytes and low-order bytes is commonly called endian. Little endian is the youngest address in memory, and big endian is the opposite.

endian.png Source: http://www.ertl.jp/~takayuki/readings/info/no05.html

Source code

endian.c


/*endianness endianness*/
/*Examine the endianness of int type*/

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int i = 1;
    
    if(*((char *)&i))
    {
        printf("little-endian\n");
    }
    //i char*Cast to int-True if the value obtained by shifting the pointer reference destination by one size is ≠ 0
    else if(*((char *)&i + (sizeof(int) - 1)))
    {
        printf("big-endian\n");
    }
    else
    {
        printf("unknown\n");
    }

    return EXIT_SUCCESS;
}

Execution result

The result was that it was little endian.

result.txt(Any)


Success #stdin #stdout 0s 4380KB

little-endian

Recommended Posts

[C language algorithm] Endianness
[C language algorithm] Block movement
[C language algorithm] Binary search tree
C language ALDS1_3_B Queue
Machine language embedding in C language
C language ALDS1_4_B Binary Search
Heapsort made in C language
[C language] readdir () vs readdir_r ()
Multi-instance module test in C language
Function pointer and objdump ~ C language ~
Writing C language with Sympy (metaprogramming)
Introduction to Protobuf-c (C language ⇔ Python)
C language 8 queens problem solving 3 patterns
Segfault with 16 characters in C language
Call c language from python (python.h)
Closure 4 language comparison (Python, JavaScript, Java, C ++)
Algorithm in Python (ABC 146 C Binary Search
Socket communication by C language and Python
Generate C language from S-expressions in Python