-
February 15th, 2010, 10:26 AM
#1
length of integer OR position of integer
for example:
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!!!
-
February 15th, 2010, 10:36 AM
#2
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.
-
February 15th, 2010, 10:37 AM
#3
Re: length of integer OR position of integer
Originally Posted by Johnseito
for example:
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.
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
-
February 15th, 2010, 10:39 AM
#4
Re: length of integer OR position of integer
Originally Posted by Johnseito
for example:
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 %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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|