1

My code is this

#include<stdio.h>
int main(void)
{
    unsigned short height = 0;
    unsigned short width = 0;
    const unsigned short MIN_SIZE = 3;
    printf("Enter the values for the width and the height minimum of %u\n:",
           MIN_SIZE);
    scanf(" %hd %hd", &width, &height);
    if (width < MIN_SIZE)
    {
        printf("The value of width   %u is too small. I set this to %u    \n",
               width, MIN_SIZE);
        width = MIN_SIZE;
    }
    if (height < MIN_SIZE)
    {
        printf
            ("The value of height %u is too small. I setting this to   %u \n"),
            height, MIN_SIZE;
        height = MIN_SIZE;
    }
    for (unsigned int i = 0; i < width; ++i)
    {
        printf("*");
    }
    return 0;
}

When I give a width of 7 for example, and a height of 0, the printf() presents weird numbers. Can you explain please why this is happening?

DiMono
  • 3,308
  • 2
  • 19
  • 40
paulakis
  • 89
  • 2
  • 7

1 Answers1

6

THis one would probably compile with a warning. You need to keep the closing parenthesis after providing all the argument.

printf
            ("The value of height %u is too small. I setting this to   %u \n"),
            height, MIN_SIZE;

Probably you meant:

printf("The value of height %u is too small. I setting this to   %u \n", height, MIN_SIZE);

The main issue is that we should use "%hu" for short intergers. I would try this:

#include<stdio.h>
int main(void)
{
    unsigned short height = 0;
    unsigned short width = 0;
    const unsigned short MIN_SIZE = 3;
    int i ;
    printf("Enter the values for the width and the height minimum of %u\n:", MIN_SIZE);
    scanf(" %hu %hu", &width, &height);
    if (width < MIN_SIZE) {
        printf("The value of width   %hu is too small. I set this to %hu    \n", width, MIN_SIZE);
        width = MIN_SIZE;
    }
    if (height < MIN_SIZE) {
        printf("The value of height %hu is too small. I setting this to %hu \n", height, MIN_SIZE);
        height = MIN_SIZE;
    }
    for (i = 0; i < width; ++i)
    {
        printf("*");
    }
    return 0;
}

There was a good related discussion on SO about this: What is the format specifier for unsigned short int?

Community
  • 1
  • 1
Manoj Pandey
  • 4,528
  • 1
  • 17
  • 18
  • 2
    It does compile (comma operator), but any decent compiler should warn that there are more '%' conversions than data arguments. – Martin R Sep 10 '13 at 20:07
  • +1 for finding the problem. But interestingly, it will compile with the C99 standard in `gcc`. – lurker Sep 10 '13 at 20:08
  • I meant that when i set for example the width 7 and the height 0 printf() presenting correctly the width but in height prints something like 2476842 – paulakis Sep 10 '13 at 20:08
  • @Martin R, I agree (+1). I have updated the answer to be more accurate. – Manoj Pandey Sep 10 '13 at 20:09
  • I know that i can use integers but i want to know why compiler does that – paulakis Sep 10 '13 at 20:12
  • Thank you buddy.... I didn't see it before.... But the weird is that compiler started and doesn't hit any error – paulakis Sep 10 '13 at 20:14
  • `%hu` isn't necessary for `printf` (though it *is* necessary for `scanf`), since all arguments of type `char` and `short` (and their `unsigned` variants) get promoted to `int` or `unsigned int` when passed to variadic functions like `printf` due to the default argument promotions. – Adam Rosenfield Sep 10 '13 at 20:16
  • In scanf() i didn't write %hu but %hd... Is this wrong ? My book doesn't reference anywhere about "variadic" – paulakis Sep 10 '13 at 20:25
  • 1
    I would say yes. Since "%hd" is a short and "%hu" is an unsigned short . – Manoj Pandey Sep 10 '13 at 20:28
  • Do you want "... minimum of %hu\n:"? `%hu` – chux - Reinstate Monica Sep 11 '13 at 02:48