-1
  1 #include <stdio.h>
  2 #include <string.h>
  3
  4 size_t  my_strlen(const char *s)
  5 {
  6         size_t  i;
  7
  8         i = 0;
  9         while (s[i])
  10                 i++;
  11         return (i);
  12 }
  13
  14 int     main()
  15 {
  16         char s[10] = "bonjour";
  17         char s2[10] = "bonjour";
  18
  19         printf("%d\n", strlen(s2));
  20         printf("%d\n", my_strlen(s));
  21         return (0);
  22 }

When i test this code at my school on a Imac it works fine, but on my macbook Pro i get this message:

test.c:19:17: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
        printf("%d\n", strlen(s2));
                ~~     ^~~~~~~~~~
                %lu
test.c:20:17: warning: format specifies type 'int' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
        printf("%d\n", my_strlen(s));
                ~~     ^~~~~~~~~~~~
                %zu

when i changed it , it's fine, but why i get this message on my personnal computer ? (2 GHz Intel Core i7,4 Go 1333 MHz DDR3,Intel HD Graphics 3000 384 Mo)

user3540997
  • 49
  • 1
  • 1
  • 9

3 Answers3

2

Change format specifiers in these statements

    printf("%d\n", strlen(s2));
    printf("%d\n", my_strlen(s));

to

    printf("%zu\n", strlen(s2));
    printf("%zu\n", my_strlen(s));

Your program in fact has undefined behaviour because you are using incorrect format specifier %d that is designed for objects of type int with an object that has unsigned type size_t. For example sizeof( size_t ) can be greater than sizeof( int ), or a positive value of an object of size_t can not be represented in object of type int. And the compiler warns you about this.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • yes i understood that but why when i compile it at my school with %d i don't get any error message and i works fine, but on my personnal computer i va to change it ? – user3540997 Mar 03 '15 at 09:55
  • 1
    @user3540997 It depends on settings of the compiler: what level of warnings the compiler shall catch. – Vlad from Moscow Mar 03 '15 at 09:56
  • `size_t` is a typedef that depends on architecture. On the computer that doesn't generate the warnings it's probably typedef'd to `unsigned int` with the default compiler settings, on your computer it's typedef'd to `unsigned long` as said in the warning text. – Ionut Mar 03 '15 at 10:03
  • ok thank you !! do you know where we can check the compiler settings ? – user3540997 Mar 03 '15 at 10:14
  • @user3540997 If you are using an IDE then you should check its options. Or if you are using a batch file then see the settings in the file. – Vlad from Moscow Mar 03 '15 at 10:19
0

First sorry for langage, i'm not english You shoud use %d for int variables, you should use %zu (i think but you have to verify) The error is that %d is not correct

0

I got different warning on both computers because the compiler setting were differents as well as Processor and RAM.

user3540997
  • 49
  • 1
  • 1
  • 9