Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
int main(int argc, char* argv[])
{
  unsigned long long x=atoi(argv[1]);
  unsigned long long y=atoi(argv[2]);
  if(x>0 && x<=4294967295 && y>0 && y<=4294967295)
  {
    cout<<"done";
  }
  else
  {
    cout<<"not done";
  }
  return 0;
}


But when i insert x=6543848485 or any value greater than 2254354363(10 digit), it is automatically taking the default value of x=2147483647(automatically). I don't know why? and not entering in the else condition.

But when i run this code in windows i.e. in dev-cpp it is running. but i want to run it in unix.

help
Posted
Updated 4-Sep-11 22:40pm
v2

1 solution

When you asked this question an hour ago, you got answers. taking default value of unsigned long int in unix[^]

The answer hasn't changed because you post it again.

Go back, read the answers and go from there - do not re-post your question, aask for clarification of an answer if you don't understand it.
 
Share this answer
 
Comments
Trick7 5-Sep-11 4:42am    
Yes please clarify me y is it so???
OriginalGriff 5-Sep-11 4:53am    
Because atoi returns an integer (that's what the 'i' on the end means). If the number does not fit in an integer, the returned result is "undefined" - which means that the library writer is free to do what they think sensible - in this case, it returns the maximum value that can be stored in a 32 bit integer - 2147483647 or 7FFFFFFF in hex.

Try instead using atol - it returns a long int which may be bigger on your target. Note that the size of an integer is not fixed in C or C++ as it is in .NET (it could be 16 bits, 32 bits, 24 bits, 64 bits or whatever is suitable for the target OS / processor)


[edit]minor typo, missed a ')' - OriginalGriff[/edit]
Trick7 5-Sep-11 4:58am    
But it is runnning in dev-cpp compilor in windows... how???
OriginalGriff 5-Sep-11 5:10am    
Because Microsoft C++ also supports the Int64 datatype (http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.71).aspx) which you have probably got set as the default size, somewhere! Since this holds 64 bits rather than 32, your larger numbers fit in ok.
Your Unix compiler is using 32 bit intgers, which cannot contain a number bigger than 2147483647

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900