how to initialize the “min” variable properly?

后端 未结 4 450
野性不改
野性不改 2021-01-29 10:17

I have a small problem in my code for finding the minimum value from a series of numbers. When I initialize min = 0, the minimum value turns out as 0. But when I do

4条回答
  •  花落未央
    2021-01-29 10:35

    You must initialize min with a large number, otherwise 0 might remain as the smallest number.

    You can initialize min as

    int min = INT_MAX;
    

    INT_MAX is the largest value that can be held by an int( Must add for INT_MAX ).

    If you don't initialize min with a value, then it will have an indeterminate value, which can be anything, so always initialize your local variables before using their values..

提交回复
热议问题