-
Notifications
You must be signed in to change notification settings - Fork 0
Unsig
Iva edited this page Sep 27, 2024
·
5 revisions
Calculates the number of digits in an unsigned integer:
Returns the number of digits required to represent the given unsigned integern
in decimal notation.
Library | None (Internal helper function) |
---|---|
Signature | int unsig_number(unsigned int n); |
Parameters |
n : The unsigned integer to analyze. |
Return | The number of digits in the unsigned integer n . |
int digits = unsig_number(12345); // Returns 5
Repeatedly divides the number by 10 until it is reduced to 0, counting the divisions.
Prints an unsigned integer to standard output (stdout):
Converts an unsigned integer into its decimal string representation and prints it.
Library |
<unistd.h> , <stdlib.h>
|
---|---|
Signature | int print_unsig(unsigned int n); |
Parameters |
n : The unsigned integer to be printed. |
Return | The number of characters printed. |
int printed = print_unsig(12345); // Prints "12345" and returns 5
If
n == 0
, it directly prints'0'
and returns 1, without further calculations.
Usesunsig_number(n)
to calculate the number of digits in the unsigned integern
.
Allocates memory to store the string representation ofn
. After conversion, the string is printed, and the allocated memory is freed.
Useswrite()
from<unistd.h>
to print the string to standard output.