What Does an Integer That Has Zero in Front of It Mean and How to Print It

What does an integer that has zero in front of it mean and how can I print it?

The JLS 3.10.1 describes 4 ways to define integers.

An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).

An octal numeral consists of a digit 0 followed by one or more of the digits 0 through 7 ...

A decimal numeral is either the single digit 0, representing the integer zero, or consists of an digit from 1 to 9 optionally followed by one or more digits from 0 to 9 ...

In summary if your integer literal (i.e. 011) starts with a 0, then java will assume it's an octal notation.

octal conversion example

Solutions:

If you want your integer to hold the value 11, then don't be fancy, just assign 11. After all, the notation doesn't change anything to the value. I mean, from a mathematical point of view 11 = 011 = 11,0.

int a = 11;

The formatting only matters when you print it (or when you convert your int to a String).

String with3digits = String.format("%03d", a);
System.out.println(with3digits);

The formatter "%03d" is used to add leading zeroes.

formatter

Alternatively, you could do it in 1 line, using the printf method.

System.out.printf("%03d", a);

Printing leading zeros in a number in C

The best way to have input under control is to read in a string and then parse/analyze the string as desired. If, for example, "exactly five digits" means: "exactly 5 digits (not less, not more), no other leading characters other than '0', and no negative numbers", then you could use function strtol, which tells you where number parsing has ended. Therefrom, you can derive how many digits the input actually has:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

int main() {

char line[50];
if (fgets(line,50,stdin)) {
if (isdigit((unsigned char)line[0])) {
char* endptr = line;
long number = strtol(line, &endptr, 10);
int nrOfDigitsRead = (int)(endptr - line);
if (nrOfDigitsRead != 5) {
printf ("invalid number of digits, i.e. %d digits (but should be 5).\n", nrOfDigitsRead);
} else {
printf("number: %05lu\n", number);
}
}
else {
printf ("input does not start with a digit.\n");
}
}
}

Printing leading 0's in C

printf("%05d", zipCode);

The 0 indicates what you are padding with and the 5 shows the width of the integer number.

Example 1: If you use "%02d" (useful for dates) this would only pad zeros for numbers in the ones column. E.g., 06 instead of 6.

Example 2: "%03d" would pad 2 zeros for one number in the ones column and pad 1 zero for a number in the tens column. E.g., number 7 padded to 007 and number 17 padded to 017.

C++ int with preceding 0 changes entire value

An integer literal that starts from 0 defines an octal integer literal. Now in C++ there are four categories of integer literals

integer-literal:
decimal-literal integer-suffixopt
octal-literal integer-suffixopt
hexadecimal-literal integer-suffixopt
binary-literal integer-suffixopt

And octal-integer literal is defined the following way

octal-literal:
0 octal-literal
opt octal-digit

That is it starts from 0.

Thus this octal integer literal

0110

corresponds to the following decimal number

8^2 + 8^1 

that is equal to 72.

You can be sure that 72 in octal representation is equivalent to 110 by running the following simple program

#include <iostream>
#include <iomanip>

int main()
{
std::cout << std::oct << 72 << std::endl;

return 0;
}

The output is

110

How does C Handle Integer Literals with Leading Zeros, and What About atoi?

Leading zeros indicate that the number is expressed in octal, or base 8; thus, 010 = 8. Adding additional leading zeros has no effect; just as you would expect in math, x + 0*8^n = x; there's no change to the value by making its representation longer.

One place you often see this is in UNIX file modes; 0755 actually means 7*8^2+5*8+5 = 493; or with umasks such as 0022 = 2*8+2 = 10.

atoi(nptr) is defined as equivalent to strtol(nptr, (char **) NULL, 10), except that it does not detect errors - as such, atoi() always uses decimal (and thus ignores leading zeros). strtol(nptr, anything, 0) does the following:

The string may begin with an arbitrary
amount of white space (as determined
by isspace(3)) followed by a single
optional '+' or '-' sign. If base is
zero or 16, the string may then
include a "0x" prefix, and the number
will be read in base 16; otherwise, a
zero base is taken as 10 (decimal)
unless the next character is '0', in
which case it is taken as 8 (octal).

So it uses the same rules as the C compiler.



Related Topics



Leave a reply



Submit