Number helpers
- C course
- Intermediate
- Lesson 15/116
- 6 min
Add number conversion and calculation functions.
Number helpers
The third lesson defines num_abs and num_sign. num_abs returns the absolute value of a number; num_sign returns its sign (+1, 0, -1).
For num_abs a single-line ternary suffices: return n < 0 ? -n : n;. num_sign has three cases, so an if chain fits. These functions are common in math and statistics. The previous lesson's solution is in the starting code; you add the new functions.
num_sign implementation
num_sign returns one of three values: +1 (positive), 0 (zero) and -1 (negative). The natural form is an if chain.
Example: int num_sign(int n) { if (n > 0) return 1; if (n < 0) return -1; return 0; }. The first if catches positives, the second negatives; the remainder is zero. This pattern is common for sign, direction and comparison functions that need three-way logic. It can be written without else thanks to early returns.
Practice Add num_abs and num_sign
Below the existing definitions, write int num_abs(int n) that returns n < 0 ? -n : n;. Write int num_sign(int n) that returns 1 for positive, -1 for negative and 0 for zero. In main add printf("%d\n", num_abs(-7)); and printf("%d\n", num_sign(-5));.
Starter code
#include <stdio.h>
int str_len(const char *s);
int str_is_empty(const char *s);
int num_abs(int n);
int num_sign(int n);
int arr_max(int *a, int n);
int arr_sum(int *a, int n);
int str_len(const char *s) {
int n = 0;
while (*s) { n++; s++; }
return n;
}
int str_is_empty(const char *s) {
return str_len(s) == 0;
}
int main(void) {
printf("Modül hazır\n");
printf("%d\n", str_len("Merhaba"));
printf("%d\n", str_is_empty(""));
return 0;
}
// num_abs fonksiyonunu yaz: mutlak değer
// num_sign fonksiyonunu yaz: pozitifse 1, negatifse -1, sıfırsa 0
// main içinde ikisini çağır ve yazdırSample solution
#include <stdio.h>
int str_len(const char *s);
int str_is_empty(const char *s);
int num_abs(int n);
int num_sign(int n);
int arr_max(int *a, int n);
int arr_sum(int *a, int n);
int str_len(const char *s) {
int n = 0;
while (*s) { n++; s++; }
return n;
}
int str_is_empty(const char *s) {
return str_len(s) == 0;
}
int num_abs(int n) {
return n < 0 ? -n : n;
}
int num_sign(int n) {
if (n > 0) return 1;
if (n < 0) return -1;
return 0;
}
int main(void) {
printf("Modül hazır\n");
printf("%d\n", str_len("Merhaba"));
printf("%d\n", str_is_empty(""));
printf("%d\n", num_abs(-7));
printf("%d\n", num_sign(-5));
return 0;
}Expected output
Modül hazır 7 1 7 -1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the num_abs ternary
int num_abs(int n) {
return n < 0 ? ____ : n;
}
int num_abs(int n) {
return n < 0 ? -n : n;
}
Explanation: For negative n, -n is returned; that's the absolute value.
2 Match the number helpers
| Term | Meaning |
|---|---|
num_abs(-7) |
7 (negative input) |
num_abs(7) |
7 (positive input) |
num_sign(-5) |
Returns -1 |
num_sign(0) |
Returns 0 |
3 What does num_sign(-5) return?
- 0
- -1 Correct answer
- 1
- 5
Explanation: num_sign returns -1 for negative numbers.