Open In App

Variables in Objective-C

Last Updated : 16 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guidelines that must be followed in Objective-C while defining the variable’s name, the guidelines are:

  • The names of variables may also include underscores, letters, and numbers.
  • Names for variables can either begin with a letter or an underscore.
  • Objective C is considered a case-sensitive language. As a result, upper-case and lower-case alphabets are regarded as separate.

Basic Variable Types

Following are the basic variable types in Objective-C:

S.no Type Description
1 char It is an 8-bit datatype that is one byte in size.
2 int It includes integers that are typically 2 or 4 bytes in size.
3 float It provides a single-precision floating-point value indication.
4 double It provides a double-precision floating-point value indication.
5 void It demonstrates the lack of a type.

Objective-C also supports various other types of variables like Enumeration, Array, Pointer, Structure, Union, etc. 

Definition of Variable

When a variable is defined, the compiler is informed of where and how much storage space to allocate for the variable. A variable definition, which also comprises a list of one or more variables of that type, specifies a data type.

Syntax:

type var_name;

Here, the type must be a valid Objective-C data type including char, w char, int, float, double, bool or any user-defined object, etc., and the variable list may consist of one or more identifier names separated by commas. Here are several declarations that are legitimate :

int i;
char c;
float f;
double d;

Initialize the Variables

To define or initialize the variables in Objective-C, we can use the assignment = operator.

Syntax:

type var_name = value;

Example:

extern int x = 45;    // declaration of x; 
int x = 45;           // definition and initializing x. 
char y = ‘y’;               // the variable y has the value ‘y’.

Variables with static storage lifetime are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is indeterminate for definitions without an initializer.

Variable Declaration and Memory 

Declaring a variable helps the compiler know about the variable that already exists. To make a memory location more comprehensible for humans, we give it an alias using a variable. A variable must be declared for the compiler to use it while linking the program; otherwise, it is meaningless.
Anywhere we want to define a variable, we can do so by using the keyword extern. The variable can be declared more than once in the code. However, each function, file, and block of code can only have one definition of it.

Example 1:

Here is an illustration of the declaration and definition of a variable. The variables are defined and initialized both locally and globally in the following code, either inside or outside of the main() method.

ObjectiveC




// Objective-C program to illustrate the variables
#import <Foundation/Foundation.h>
  
// Variable declaration
extern int p, q;
extern int r;
  
int main () 
{
    // Variable definition 
    int p, q;
    int r;
      
    // Actual initialization 
    p = 7;
    q = 5;
    r = p * q;
    NSLog(@"value is : %d \n", r);
    return 0;
}


 Output:

value is : 35 

Explanation: Here, we declare int-type extern variables. In Objective-C, the extern keyword enables us to declare variables worldwide. Then in the main() method, three int-type variables, p, q, and r, are defined. Then initializing r=p+q, p=10, and q=20, and print the output on the console.

Example 2:

ObjectiveC




// Objective-C program to illustrate the variables
#import <Foundation/Foundation.h>
  
// Variable declaration:
extern float f;
  
int main () 
{
    // Variable definition: 
    float f;
      
    // Actual initialization 
    f = 70.0/4.0;
    NSLog(@"value of f : %f \n", f);
      
    return 0;
}


Output:

value of f : 17.500000 

Explanation: Here, we declare int-type extern variables. In Objective-C, the extern keyword enables us to declare variables worldwide. Now, in the main() method, int-type variable f is defined. Then initialize the value of f and print output on the console.

Lvalues and Rvalues 

In the Objective C, there are two types of expressions.

  • lvalue-Expressions that make a memory location reference are known as “lvalue” expressions. An assignment’s left or right side may both have a lvalue.
  • rvalue-The term “rvalue” refers to a data value that is kept in memory at a specific address. An rvalue can occur on the right side of an assignment statement but not the left because it is an expression that cannot have a value assigned to it.

Since variables are lvalues, they can be found on an assignment’s left side. Since numerical literals are rvalues, they cannot be assigned and cannot be used in left-side clauses. 

Example:

int x = 1;



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads