Stephen Mather
Stephen Mather

Reputation: 275

printf is only printing "1537" rather than the value of the integer

I'm just now learning programming, so this might seem a tiresome question. But when I try to print the value of an integer, it prints "1537" instead. Here is the function, feel free to scrutinize.

void compare(void) {
int num1;
int num2;
int num3;
int num4;
int smallest;

printf("Please enter four integers:\n");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);

num1 = smallest;
if (num2 < smallest)
    num2 = smallest;
if (num3 < smallest)
    num3 = smallest;
if (num4 < smallest)
    num4 = smallest;

printf("%d is the smallest\n", smallest);

}

Upvotes: 1

Views: 178

Answers (2)

Martin James
Martin James

Reputation: 24847

You don't initialize smallest. Actually, you don't write to it at all, anywhere.

Upvotes: 3

Vandesh
Vandesh

Reputation: 6894

you need to assign the values to smallest rather than from it.

Change it to

smallest=num1;
if (num2 < smallest)
    smallest= num2;
if (num3 < smallest)
    smallest=num3;
if (num4 < smallest)
    smallest=num4;

~Sometimes its tough to catch the smallest mistakes~ :)

Upvotes: 13

Related Questions