今天春天
今天春天

Reputation: 993

Remove leading zeros from integer in C

If I declare an integer in C as below :

int an_example = 000300;  

Can I somehow "cut off" the leading zeros from an integer number? I want 000300 to be interpreted as 300.

Upvotes: 1

Views: 6166

Answers (5)

ffhaddad
ffhaddad

Reputation: 1713

It's important to understand that numbers can be expressed in four common representations in C. Binary, Octal, Decimal, and Hexadecimal.

For example:

Binary (base 2):

0b100101100 // 300 in binary, The leading 0b together tells the compiler its a binary value (not all compilers support this).

Octal (base 8)

0300 // This is 192 in decimal. The leading 0 tells the compiler the value is octal.

Hexadecimal (base 16)

0x12C // 300 in decimal. The leading 0x tells the compiler the value is hexadecimal.

Your number: 000300 is an octal number and represents 192 in decimal because you have a leading 0. Note the superfluous leading zeros do not affect the value.

As to your question: No, you can not include leading zeros and have the compiler interpret the value as decimal.

Upvotes: 3

Caleb
Caleb

Reputation: 124997

Can I somehow "cut off" the leading zeros from an intenger number? I want that 000300 is interpreted as 300.

Integers always have the same internal representation -- you don't worry about formatting issues like leading zeroes. As @BasileStarynkevitch pointed out in the comments, a leading 0 in an integer constant makes the compiler interpret the number as octal, or base 8. If you remove the leading zeroes from the code you provided, an_example will change from 300 octal (192 decimal) to 300 decimal (454 octal).

How you display an_example is another matter, and whether there are depends entirely on the format specifier you use. For example, if you want to print the number in octal but without a leading 0, you'd use the %o specifier:

printf("%o", an_example);

If you want to print it as a decimal number, use %d in place of %o above.

On the other hand, if you want to print it with the leading 0 to indicate octal, you'd write:

printf("%#o", an_example);

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612854

000300

This is an octal constant. It can be more concisely written as

0300

This value is equal to the decimal value 192.

If you want the decimal number 300, write

300

But do be very clear that 0300 != 300.

In case you need something a little more concrete, try running this program. You should now be in a position to predict its output:

#include <stdio.h>

int main(void)
{
    printf("%d\n", 000300);
    printf("%d\n", 0300);
    printf("%d\n", 300);
    return 0;
}

Upvotes: 4

Lundin
Lundin

Reputation: 213583

If you write 0 in the start of an integer literal, it means that the number is octal. 300 octal is equal to 192 decimal, so you will get the value 192 from this code.

Cut off leading zeroes with the delete key on your keyboard.

Upvotes: 7

Graham Borland
Graham Borland

Reputation: 60681

The way an integer is formatted for display is completely independent of the way you initialize it.

(By the way, a leading 0 on an integer constant means it's interpreted as an octal number.)

Upvotes: 2

Related Questions