views:

62

answers:

1

symbol.c: In function 'symbol_FPrint':

symbol.c:1209: warning: format '%ld' expects type 'long int', but argument 3 has type 'SYMBOL'
symbol.c: In function 'symbol_FPrintOtter':
symbol.c:1236: warning: format '%ld' expects type 'long int', but argument 3 has type 'SYMBOL'
symbol.c:1239: warning: format '%ld' expects type 'long int', but argument 3 has type 'SYMBOL'
symbol.c:1243: warning: format '%ld' expects type 'long int', but argument 3 has type 'SYMBOL'
symbol.c:1266: warning: format '%ld' expects type 'long int', but argument 3 has type 'SYMBOL' 

In symbol.c

1198 #ifdef CHECK
1199     else {
1200       misc_StartErrorReport();
1201       misc_ErrorReport("\n In symbol_FPrint: Cannot print symbol.\n");
1202       misc_FinishErrorReport();
1203     }
1204 #endif
1205   }
1206   else if (symbol_SignatureExists())
1207     fputs(symbol_Name(Symbol), File);
1208   else
1209     fprintf(File, "%ld", Symbol);
1210 }

And SYMBOL is defined as:

typedef size_t SYMBOL

When i replaced '%ld' with '%zu' , i got the following warning:

symbol.c: In function 'symbol_FPrint':
symbol.c:1209: warning: ISO C90 does not support the 'z' printf length modifier

Note: From here it has been edited on 26th of march 2010 and and following problem has beeen added because of its similarity to the above mentioned problem.

I have following statement:

printf("\n\t %4d:%4d:%4d:%4d:%4d:%s:%d", Index, S->info, S->weight,
       Precedence[Index],S->props,S->name, S->length);

The warning I get while compiling in 64 bit architecture is :

format ‘%4d’ expects type ‘int’, but argument 5 has type ‘size_t’

here are the definitions of parameter:

  NAT    props;
  typedef  unsigned int     NAT;

How can i get rid of this so that i can compile without warning in 32 and 64 bit architecture?

What can be its solution?

+1  A: 

Use %zu rather than %ld as the format for size_t and then you'll get correct behaviour (and no warnings) in both 32-bit and 64-bit builds.

If for some reason you can't use %zu (e.g. old or non-standard compiler), then you can do something like this:

#ifdef __LP64__ // if 64 bit environment
#define FMT_SIZE_T "llu"
#else
#define FMT_SIZE_T "lu"
#endif

Then when you need to use printf with somethign of type size_t you can do this:

printf("(sizeof(void *) = %"FMT_SIZE_T" bytes \n", sizeof(void *));
Paul R
that really worked for both , i got one more new warning on the same linei'eISO C90 does not support 'z' gnu_printf length modifier.What can be the alternatives in this case?
thetna
Either use -std=c99 when you compile, or else #define a format specifier, e.g. `FMT_SIZE_T` which is #defined as `%llu` on 64-bit and `%lu` on 32-bit builds.
Paul R
thank you very much Paul.It really worked.
thetna
is it possible to remove such warning without relying into C99 standard?
thetna
@thetna: yes, as I said, you can just conditionally `#define` your own `FMT_SIZE_T` which is e.g. `%lu` for for 32-bit builds and `%llu` for 64-bit builds
Paul R
@pual thanks for the reply. this can be again a stupid question but i am newbie to C.Does this changes make a single package compatible to both the architecture environement or need to define spearately %lu for 32 bit and 64 bit architecture?
thetna
@thetna: I'll add an example to my answer which should hopefully make this clear.
Paul R