Hi,
I have written the program below and the compiler is not giving any error message and when I run it, the first request for an enter is almost always followed by strange numbers,which I don't know where they come from
#include<stdio.h>
int main()
{
  int value;
  int inputs;
  int number_values;
  int smallest;
  
  printf("Please enter the number of value:  %d\n", number_values);
  scanf("%d", &number_values);
  
  /*Begining of for cicle*/
  for(inputs = 1; inputs <= number_values; ++inputs)
  {
    printf("Please enter the next number: %d\n", value);
    scanf("%d", &value);
       /*Begining of if cicle*/
       if(smallest> value)
       {
	   smallest = value;
       }
       /*End of if cicle*/
    printf("The smallest is: %d\n", smallest);
    
    
  }/*End of for cicle*/
    return 0;
  
}

The second question is about something related to how the computer works out the solution. After all the program is ok, but in depth there is something strange, namely as you can see, the variable "smallest" has no value at the beginning of the program, clearly,and after the first "value"entered, logically, the program should skip the following "if " since there is no value for comparing, but strangely this is not the case. It seems that the variable "smallest" got some value, but I do not understand which one, and where it comes from.
Is there anyone able to help me???

Thank You very much!!

Bye Bye!!!

The variable is just some (named) piece of a computer memory. It always has a value. Before you assign something to it, the value is garbage: useless, unpredictable, yet a value. This is why initialization is so important. Fix your program by initializing:

int smallest = INT_MAX;

hi fab,

May be I am wrong but it seems you have copied this program from somewhere, otherwise you would have known you are printing the variables before assigning any values to them(which i don't know is for what purpose). So they display garbage values as told by nezachem.

This applies to the smallest variable also it contains some garbage value too so you should initialize it.

And furthermore these are called logical errors so compiler won't give you any errors for those.

Anirudh

Thank you very much to both of you anirudh33 and nezachem, but I am a beginner so....I make mistakes.

Thank you again

Bye, bye

This article has been dead for over six months. Start a new discussion instead.