length of integer OR position of integer
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: length of integer OR position of integer

1. Member
Join Date
Jan 2010
Posts
57

## length of integer OR position of integer

for example:

Code:
`int x = 05;`
how do you know the length of x? and when you print it, it print as 5. 0 is disregard.

or how do you know the position of 0 in x? unlike string, how do you traverse it?
this case is a variable, so lets say I have an array for example:

Code:
```car the_car[20];
the_car[i].min```

Where car is a struct data type, the_car[20] is an array, and min is a member of a struct where is a int variable example (int min);

so how do I know the length of min, if min for example is

02, how do I know the position of 2 or put it another way how I do I know that there is a 0 before 2?

thanks!!!

2. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: length of integer OR position of integer

Leading zeroes have no meaning in integral data types. 02 is the same as 2 is the same as 0000002. You can use sizeof to get the size of an integer, but that won't tell you anything about the value it contains.

3. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: length of integer OR position of integer

Originally Posted by Johnseito
for example:

Code:
`int x = 05;`
how do you know the length of x?
x is an integer. There is no "length", only a minimum and maximum integer value. Secondly, when you assign 05 to an int, or any number starting with 0, you are assigning an octal number to the int. Try this, and you will get a syntax error.
Code:
`int x = 039;`
The reason is there is no such digit as '9' in octal.
and when you print it, it print as 5. 0 is disregard.
You are responsible for adding leading 0's to whatever integer is printed.

So the rest of your question really doesn't make sense. There is no "position" of 0 in x. There is no "traversing" an integer. An integer is an integer. An integer is not a string.

Regards,

Paul McKenzie

4. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: length of integer OR position of integer

Originally Posted by Johnseito
for example:

Code:
`int x = 05;`
how do you know the length of x? and when you print it, it print as 5. 0 is disregard.
The 0 is disregarded long before you print it. It's probably dropped as irrelevant at compile time. An integer is not stored as a sequence of digits; it's just a value. You could print it as 5 (decimal), or as 101 (binary), or however you want.

You could certainly tell the program that you'd like to pad the number with leading 0s when you output it; I think setw() and setfill() are the methods for ostreams, while a format specifier like &#37;02d would do it for printf. But that's just a matter of formatting, it doesn't affect the underlying value of the variable.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•