-
Notifications
You must be signed in to change notification settings - Fork 0
Hex
Recursively prints a number in hexadecimal format:
Converts the given number to its hexadecimal representation, printing digits and letters (a-f or A-F) in the correct order.
Library | None (Internal helper function) |
---|---|
Signature | int print_hex_rec(unsigned long long n, char c); |
Parameters |
n : The number to be printed in hexadecimal. |
c : Specifies whether the letters in the hexadecimal output should be lowercase (x ) or uppercase (X ). |
|
Return | The number of characters printed. |
int printed = print_hex_rec(255, 'x'); // Prints "ff"
Uses recursion to divide the number by 16, printing the remainders in reverse order.
Handles lowercase and uppercase hexadecimal letters.
Handles variadic arguments to print a number in hexadecimal format:
Converts an unsigned long long number to hexadecimal, printing it with either lowercase (%x
) or uppercase (%X
) letters based on the format specifier.
Library | <stdarg.h> |
---|---|
Signature | int print_hex_varargs(int c, ...); |
Parameters |
c : Specifies whether the hexadecimal output should use lowercase ('x') or uppercase ('X'). |
... : Variadic arguments containing the unsigned long long number to be printed. |
|
Return | The number of characters printed. |
int printed = print_hex_varargs('x', 255); // Prints "ff" and returns 2
int printed_upper = print_hex_varargs('X', 255); // Prints "FF" and returns 2
Calls
print_hex_rec
to recursively convert and print the number in hexadecimal format.
Supports both lowercase ('x') and uppercase ('X') hexadecimal formats based on thec
parameter.
Ifn == 0
,print_hex_rec
will handle the printing of"0"
.
Previous ⬅️ • Top ⬆️ • Next ➡️