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!!!

Dani AI

Generated

— the “strange numbers” come from using automatic (stack) variables before giving them a value. As and noted, an uninitialized int contains an indeterminate value; using it (printing it or comparing it) is undefined behavior and can produce any output. Two additional problems in the original code that make debugging harder: the prompt printf calls include %d and pass variables that haven’t been set (so the prompt itself prints garbage), and the program prints the current smallest value inside the input loop instead of reporting the final result after processing all inputs.

A simple, robust pattern: read and validate the count, read the first number to initialize smallest, then loop over the remaining inputs comparing each new value to smallest. Check scanf return values and validate the count is positive. Example:

#include <stdio.h>

int main(void) {
    int n;
    if (printf("Enter how many numbers: "), scanf("%d", &n) != 1 || n <= 0) return 1;
    int smallest, value;
    if (printf("Enter value 1: "), scanf("%d", &smallest) != 1) return 1;
    for (int i = 2; i <= n; ++i) {
        printf("Enter value %d: ", i);
        if (scanf("%d", &value) != 1) return 1;
        if (value < smallest) smallest = value;
    }
    printf("The smallest is: %d\n", smallest);
    return 0;
}

Extra tips: compile with warnings enabled (treat warnings seriously), run with sanitizers (e.g., address/undefined behavior sanitizer) or tools like Valgrind to catch use of uninitialized memory, and prefer defensive input handling (fgets + strtol) for production code.

Recommended Answers

All 4 Replies

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

everybody makes mistakes.......we learn that way :)


Anirudh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.