Print Hexadecimal in C
In the C language, to display an integer in hexadecimal format, you can use the printf function with the format specifiers %x or %X. These specifiers can be used to convert the integer to lowercase or uppercase hexadecimal representation.
Here is a simple example code demonstrating how to output an integer’s hexadecimal representation in the C language.
#include <stdio.h>
int main() {
int num = 255; // 一个整数
printf("Number in hexadecimal: %x\n", num); // 使用 %x 格式说明符输出小写十六进制数
printf("Number in hexadecimal (uppercase): %X\n", num); // 使用 %X 格式说明符输出大写十六进制数
return 0;
}
In the code above, %x and %X are used to output the hexadecimal representation of the integer num (in lowercase and uppercase, respectively). You can choose the appropriate format specifier to output the corresponding hexadecimal number as needed.