This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

What is the correct behavior for the following code?



Hi everyone,

I have been having some portability problems with
scanf %i and %X.

Attached is a program that illustrates the problem.

Can someone tell me what is the "correct" behavior
for this code, and how I can avoid these issues
(if possible).

Thanks,

Chip Webb

#include <stdio.h>

int okcnt=0;
int errcnt=0;

check(int c, int i, int ec, int ei, int tno)
{

  if((i==ei) && (c==ec)){
    okcnt++;
    printf("OK (%d)\n",tno);
  }
  else{
    errcnt++;
    if(c!=ec)
      printf("ERROR(%d): c=%d, expect=%d\n",tno,c,ec);
    if(i!=ei)
      printf("ERROR(%d): i=0x%X, expect=0x%X\n",tno,i,ei);
  }
}

main()
{
  FILE *fp;
  int i,c,tno=1;

  system("echo 0x12345678 >  foo");
  system("echo 0xDEADBEEF >> foo");

  fp = fopen("foo","r");
  i=0; c=fscanf(fp,"%i",&i);  check(c,i,1,0x12345678,tno++);
  i=0; c=fscanf(fp,"%i",&i);  check(c,i,1,0xDEADBEEF,tno++);
  fclose(fp);
  
  fp = fopen("foo","r");
  i=0; c=fscanf(fp,"0x%X",&i);  check(c,i,1,0x12345678,tno++);
  i=0; c=fscanf(fp,"0x%X",&i);  check(c,i,1,0xDEADBEEF,tno++);
  fclose(fp);

  printf("Total OK     = %3d\n", okcnt);
  printf("Total errors = %3d\n", errcnt);

}


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]