L
lovecreatesbeauty
I ever missed a `return' statement when write a function `int
HighDigit(Num)' to get the highest digit of an integer.
But even if the `return' statement is ignored the function still can
obtain an `correct' return value when the argument `Num' is larger than
or equal to the Macro `NUM_SYS'.
If the argument is less than the Macro, the function without a `return'
get an undefined value. I've made a test on Ms Windows 2000 and VC 6.
Thank you in advance for explaining why.
And would you please comment on this algorithm? Thank you.
i.e.
Num HighDigit(Num)
============================
123 | 1
321 | 3
2 | 2
10 | 1
____________________________
The code I write is:
//highdigit.h
#ifndef _HIGH_DIGIT_H_
#define _HIGH_DIGIT_H_
#define NUM_SYS 10 //number system, decimal assumed as default
int HighDigit(int Num);
#endif //_HIGH_DIGIT_H_
//highdigit.c
#include "highdigit.h"
int HighDigit(int Num){
while (Num >= NUM_SYS){
Num /= NUM_SYS;
}
//return Num; /*This statement be missed by me at first*/
}
HighDigit(Num)' to get the highest digit of an integer.
But even if the `return' statement is ignored the function still can
obtain an `correct' return value when the argument `Num' is larger than
or equal to the Macro `NUM_SYS'.
If the argument is less than the Macro, the function without a `return'
get an undefined value. I've made a test on Ms Windows 2000 and VC 6.
Thank you in advance for explaining why.
And would you please comment on this algorithm? Thank you.
i.e.
Num HighDigit(Num)
============================
123 | 1
321 | 3
2 | 2
10 | 1
____________________________
The code I write is:
//highdigit.h
#ifndef _HIGH_DIGIT_H_
#define _HIGH_DIGIT_H_
#define NUM_SYS 10 //number system, decimal assumed as default
int HighDigit(int Num);
#endif //_HIGH_DIGIT_H_
//highdigit.c
#include "highdigit.h"
int HighDigit(int Num){
while (Num >= NUM_SYS){
Num /= NUM_SYS;
}
//return Num; /*This statement be missed by me at first*/
}