Variables in Objective C

Walden Systems Geeks Corner Variables in Objective C tutorial programming Rutherford NJ New Jersey NYC New York North Bergen County
Objective-C defines a small but powerful set of extensions to the ANSI C programming language that enables sophisticated object-oriented programming. Objective-C is the native language for Cocoa programming—it’s the language that the frameworks are written in, and the language that most applications are written in. You can also use some other languages—such as Python and Ruby—to develop programs using the Cocoa frameworks. It’s useful, though, to have at least a basic understanding of Objective-C because Apple’s documentation and code samples are typically written in terms of this language.

A variable is a name given to a storage area that our programs can manipulate. Each variable in Objective-C has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Objective-C is case-sensitive.

Variable definition

A variable definition means to tell the compiler where and how much to create the storage for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type. type must be a valid Objective-C data type including char, w_char, int, float, double, bool or any user-defined object, etc., and variable_list may consist of one or more identifier names separated by commas. Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression. For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is undefined.

int      myInt1 ;
int      myInt2 = 2 ;


Variable declaration

A variable declaration tells the compiler that there is a variable that exists with the given type and name so that compiler can proceed with compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, compiler needs actual variable declaration at the time of linking of the program. A variable declaration is useful when we are using multiple files and we define our variable in one of the files, which will be available at the time of linking of the program. We use the extern keyword to declare a variable at any place.

// Variable declaration
extern int a, b;
extern int c;

int main () 
{
  /* variable definition: */
  int a, b;
  int c;
  float f;
 
  /* variable initialization */
  a = 10;
  b = 20;
  c = a + b ;
}

Lvalues and rvalues

There are two kinds of expressions in Objective-C, lvalue and rvalue. Expressions that refer to a memory location is called the lvalue expression. The lvalue may appear as either the left-hand or right-hand side of an assignment. An rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-side but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side.