[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[linux-team] programme en C :probleme de byte-order



Salut,

voici un bout de programme C :
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) 
{
  unsigned char x[4], *c; 
  char *tail;
  int i = 1, j;
  unsigned int y;

  if (*(char *)&i == 1)
    printf("little endian\n");
  else
    printf("big endian\n");
  
  if (argc > 1)
    for (i = 1; i < argc; i++)
      {
	printf("Scanning arg %d : %s \n", i, argv[i]);
	y = strtoul(argv[i], &tail, 0);
	c = (unsigned char *)&y;
	  for (j = 0; j < 4; j++)
	    x[j] = *c++;
	printf("value of y %d\n", y);
	printf("internal: x0 0x%x x1 0x%x x2 0x%x x3 0x%x\n",
	       x[0], x[1], x[2], x[3] );
      }
  return 0;
}

Le premier block "if" vient de la FAQ de comp.lang.c.moderated et affiche
comme résultat : little endian. Le reste du
code tends à montrer que, sur i386, la partie la plus significative de y
est stockée à l'adresse la plus elevée (x[3]) : big endian ?

Plateforme: gcc-2.7.2.3, i486-pc-linux-gnu. Cette machine est-elle bien
big endian ? Le premier block "if" est-il faux, ou ai-je raté un détail ?

Merci

Pascal Dupuis 

-- 
Premature optimization is the root of all evil.
                -- D.E. Knuth

---------
This message was sent by Majordomo 1.94.3. Please repport problems to
manu@rtfm.be. If you want to be deleted from the list, send a mail to
majordomo@rtfm.be with "unsubscribe linux-team" in the body.