0

Why does the shorter string ("paid") get printed by this program?

#include <stdio.h>
int main()
{
    char s[] = "paid", t[] = "paviDboss";
    if ((strlen(s) - strlen(t)) > 0)
        printf("%s\n", s);
    else
        printf("%s\n", t);
}
rici
  • 234,347
  • 28
  • 237
  • 341
Pavi
  • 29
  • 1
    What is wrong with this code? – Iharob Al Asimi Aug 29 '18 at 17:06
  • `main` does not return anything. – Osiris Aug 29 '18 at 17:07
  • 2
    `size_t` is unsigned so the difference cannot be < 0. – Weather Vane Aug 29 '18 at 17:10
  • 3
    @Osiris `main` does not have to explicitly return anything, it is the only function returning a value that is exempt. – Weather Vane Aug 29 '18 at 17:12
  • @WeatherVane You are right, i did not know that. – Osiris Aug 29 '18 at 17:14
  • @WeatherVane in fact it can be signed too, if `size_t` has conversion rank less than `int`... – Antti Haapala -- Слава Україні Aug 29 '18 at 17:26
  • if i print (strlen(s)-strlen(t)) then i wil get -5 so its returning ant int value – Pavi Aug 29 '18 at 17:31
  • @Pavi that depends oh *how* you print it. Which format specifier? If for example you used `%d` then `printf` *believes* what you tell it. You should use `%zu` for a `size_t` type. – Weather Vane Aug 29 '18 at 17:37
  • @AnttiHaapala can you cite that? I can only find in C11 "7.19 2 The types are . . . **size_t** which is the unsigned integer type of the result of the sizeof operator;" – Weather Vane Aug 29 '18 at 17:42
  • 1
    @WeatherVane I mean that the result of the subtraction can be signed if `size_t` maps to say `unsigned short`, and all of values of `unsigned short` are representable in an `int` then that's where the integer promotions take them to. Nothing in the C standard says that it cannot be `unsigned short` - on the contrary, the only thing that is said is that it shouldn't be greater than `unsigned long` *except when necessary*. – Antti Haapala -- Слава Україні Aug 29 '18 at 17:46
  • @weathervane: right; nothing requires `size_t` to be as wide as an `int`. And if it is narrower, integer promotion of the operands to the subtraction will convert it to an `int`, not an `unsigned int`. – rici Aug 29 '18 at 17:47
  • @AnttiHaapala promoting `unsigned short` to `int` does not make `size_t` negative, although a difference can be. How does that help OP understand his problem? – Weather Vane Aug 29 '18 at 17:48
  • The strictly correct answer, I think, is because the two strings are not the same length. Unless you have a very unusual compiler, your `if` statement is checking whether an unsigned integer is greater than zero. It certainly isn't going to be less. – Tim Randall Aug 29 '18 at 18:00
  • One thing wrong with the code is that it doesn't `#include ` so `strlen()` is an undeclared function. C99 and beyond says that isn't allowed; older versions of C interpreted the function name as a function returning `int`. However, the result you claim is not consistent with this — what warnings did your compiler give? – Jonathan Leffler Aug 29 '18 at 18:11

1 Answers1

7

Return type of strlen is size_t which is an unsigned type. The result of the subtraction is also size_t and can therefore only be positive.

Just use

if(strlen(s) > strlen(t))
Osiris
  • 2,783
  • 9
  • 17