Printing value in Decimal, Octal, Hexadecimal using printf in C

This code snippet will print an entered value in Decimal, Octal, and Hexadecimal format using printf() function in C programming language. By using different format specifier we can print the value in specified format.

Print value in Decimal, Octal ad Hex using printf() in C

/*Printing value in Decimal, Octal, Hexadecimal using printf in C.*/
 
#include <stdio.h>
 
int main()
{
    int value=2567;
     
    printf("Decimal value is: %d\n",value);
    printf("Octal value is: %o\n",value);
    printf("Hexadecimal value is (Alphabet in small letters): %x\n",value);
    printf("Hexadecimal value is (Alphabet in capital letters): %X\n",value);
     
    return 0;   
}

Output

Decimal value is: 2567
Octal value is: 5007
Hexadecimal value is (Alphabet in small letters): a07
Hexadecimal value is (Alphabet in capital letters): A07

Here, following format specifiers are used:

  • %d - to print value in integer format
  • %o - to print value in octal format
  • %x - to print value in hexadecimal format (letters will print in lowercase)
  • %X - to print value in hexadecimal format (letters will print in uppercase)

C Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.