C Online Compiler
Example: Decimal to Hexadecimal using sprintf in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Decimal to Hexadecimal using sprintf #include <stdio.h> // Required for printf and sprintf int main() { // Step 1: Declare variables for decimal input and hexadecimal output string int decimalNum; char hexString[20]; // Buffer to store the hexadecimal string // Step 2: Prompt user for input printf("Enter a decimal number: "); scanf("%d", &decimalNum); // Step 3: Use sprintf to convert decimal to hexadecimal string // %X format specifier converts an integer to uppercase hexadecimal. // %x converts to lowercase hexadecimal. sprintf(hexString, "%X", decimalNum); // Step 4: Print the original decimal number and its hexadecimal equivalent printf("Decimal %d is Hexadecimal %s\n", decimalNum, hexString); return 0; }
Output
Clear
ADVERTISEMENTS