HomeLinuxC Program to Learn and Write Hexadecimal Values

C Program to Learn and Write Hexadecimal Values


Hexadecimal integers are continuously utilized in computing for expressing binary values in a extra understandable approach. Base-16 numbers referred to as hexadecimal numbers are extensively utilized in a wide range of programming languages, together with C. We’ll undergo methods to learn and write hexadecimal values and in addition elaborate on it in utilizing the C program on this tutorial on your higher understanding.

What’s Hexadecimal Worth in C?

A hexadecimal worth within the C programming language is a base-16 formatted quantity. Binary values are continuously represented in a extra comprehensible method utilizing hexadecimal numbers.

Hexadecimal numbers are made up of the numerals 0 via 9 and the letters A via F (or a–f), the place every digit stands for a quantity between 0 and 15. The numbers 10 to fifteen are represented by the letters A to F.

Hexadecimal numbers are represented in C by a prefix “0x” or “0X” that precedes the digits. As an illustration, the next syntax can be utilized to assign an integer variable the hexadecimal worth 0x3F:

int hexdecimal_Value = 0x3F;

 
Moreover, we could output a hexadecimal worth on our display screen through the use of the printf() perform and the “%x” format specifier:

int hexdecimal_Value = 123;
printf(“The hexadecimal worth is: 0xpercentx”, hexdecimal_Value);

 
This may output “The hexadecimal worth is: 0x7b” on the display screen. Now, we are going to see the C program implementation of the learn and write methodology to indicate hexadecimal worth on the immediate.

The best way to Learn Hexadecimal Worth in C?

Utilizing the “%x” format specifier and the scanf() methodology, we could learn hexadecimal knowledge in C. It instructs scanf() to interpret the worth entered right into a hexadecimal quantity utilizing this format specifier.

The next is an illustration of a program that reads a user-inputted hexadecimal worth and shows it on the display screen:

#embody <stdio.h>

int important() {
   int value_entered;
   printf(“Please enter a hexadecimal worth: “);
   scanf(“%x”, &value_entered);
   printf(“The decimal of hexadecimal worth you entered is: %dn, value_entered);
   return 0;
}

 
We initially declare an int variable named “value_entered” on this program. We then use the printf() methodology to ask the person to enter a hexadecimal worth. The enter is then learn as a hexadecimal quantity utilizing the scanf() perform and the “%x” format specifier, and it’s then saved within the “value_entered” variable.

Lastly, we printed the worth supplied by the person utilizing the printf() methodology. The decimal worth of the hexadecimal worth is printed with the “%d” format specifier.

Once you execute this system, the next output will present on the show:

The best way to Write Hexadecimal Worth in C?

We will use the printf() perform and the “%x” format specifier in C to put in writing hexadecimal values. Under is an instance that writes a worth in hexadecimal in C language:

#embody <stdio.h>

int important() {
   int decimal_value = 15;
   printf(“The given decimal worth is: %dn, decimal_value);
   printf(“The hexadecimal worth of given decimal worth is: 0xpercentxn, decimal_value);
   return 0;
}

 
On this instance code, the integer variable “decimal_value” is initially declared, and its worth is about to fifteen in decimal kind.

The decimal variety of the variable “decimal_value” is then printed using the “%d” format specifier and the printf() perform.

The contents of the variable “decimal_value” is then printed on the display screen utilizing the printf() perform as soon as extra. This time, we use the “%x” format specifier together with the “0x” prefix to indicate that the worth of the variable is in hexadecimal format.

Once you run this script, the display screen will first show the decimal worth 15, then the hexadecimal worth as proven beneath:

Conclusion

We coated methods to learn and write hexadecimal numbers in C on this information. The prevalence of hexadecimal numerals in quite a few programming languages makes them an important part of pc programming. Hexadecimal numbers are easy to learn and write in C, utilizing the “%x” format specifier, the scanf(), and printf() capabilities.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments