Integer Constants

In this tutorial, we will understand different types of integer constants in C, and we will study them in detail.


What are Integer Constants?

Integer constants are numbers that can be written in decimal, octal, or hexadecimal format in C programming. We can represent an integer constant in any of these ways, and each representation is completely different. However, internally, all numbers are represented in binary format.

Now, let’s look at the various ways to represent integer values.


Decimal Representation

The default way to represent an integer constant is in decimal format. A decimal constant can have digits from 0 to 9, but it must not start with the digit zero. That’s the only rule.

For example, 10, 100, and 3942 are decimal constants.

Usually, the type of a decimal constant is int, but we can make the compiler treat it as unsigned, long, or long long by adding specific suffixes.

To indicate that a constant is unsigned, we add the letter ‘u’ or ‘U’ as a suffix:
10u, 1673U, 78u.

To indicate that a constant is long, we add the letter ‘l’ or ‘L’ as a suffix:
90000L, 9903292l.

To indicate that a constant is long long, we add the letters ‘ll’ or ‘LL’ as a suffix: 7473853834ll, 398731231LL.

Note that if a decimal constant has no suffix, its type is determined by the compiler and is the smallest possible type among int, long int, or long long int.


Octal and Hexadecimal Representation

Octal constants consist of digits from 0 to 7, and they always start with a zero. 
For example, 067, 0721, 0345 are octal constants.

On the other hand, hexadecimal constants include digits from 0 to 9 and letters from a to f, and they always start with 0x. 
For example, 0xff, 0x234f are hexadecimal constants.

We can also change the types of octal and hexadecimal constants, just like decimal constants. However, if no suffix is specified, the possible types are int, unsigned int, long int, unsigned long int, long long int, and unsigned long long int, in that order. The appropriate type is determined by the compiler based on the number.


Summary

In this tutorial, we learned about different ways to represent integer constants in the C programming language. We covered decimal, octal, and hexadecimal representations. Decimal constants are the default and must not start with a zero. Octal constants start with a zero and contain digits from 0 to 7. Hexadecimal constants start with “0x” and include digits from 0 to 9 and letters from a to f. We also learned how to change the type of a constant by adding suffixes and clarified that the compiler determines the appropriate type if no suffix is specified.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.