Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an int in Objective-C have a default value of 1?

I have this simple line of code:

int x;

x automatically has the value of 1. I don't set it to anything but when I debug, it shows that x is 1.

Does an int have a default value of 1?!

like image 334
aryaxt Avatar asked May 12 '11 15:05

aryaxt


People also ask

What is the default value for int in C?

Variables of type "int" have a default value of 0.

What is the default value of Bool in Objective C?

It is initialized to garbage. However, for a BOOL ivar, it will be initialized to NO , as the whole instance is filled with 0 on initialization. (Note: When ARC is enabled, local object pointers will always be have a default value nil , but local variables of non-object types like BOOL are still initialized to garbage.

Is ints 0 by default C++?

Well, int's are value types in both c++ and C#, they use X bytes of ram, and all 0 bits, they are 0. So whether you initialize them or not, in memory they are still 0.


1 Answers

No. int has an undefined default value. It just happens to be 1 in this case. It could just as easily be -18382 or 22 or 0xBAADF00D.

Always initialize your variables in C.

like image 67
Jonathan Grynspan Avatar answered Sep 21 '22 06:09

Jonathan Grynspan