Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

variables
objective-c
objective c
communitycreator

How to use variables in Objective-C

Salman Yousaf

Variables

A variable is a storage area that our programs can manipulate. The variables in Objective-C contain a specific type that defines their size and layout. They also include the range of the values and operations that we can apply.

We must specify the name of the variable in Objective-C by following these rules:

  1. Variable names can contain digits, letters, and underscores.
  2. Variable names can start with a letter or an underscore symbol.
  3. Objective-C is a case-sensitive language. Therefore, upper and lower-case alphabets are considered distinct.

Parameters

Primitive data types

Type

Details

char

It is a datatype with the size of one byte or 8 bits.

int

It contains the common size of integers of either 2 or 4 bytes.

float

It indicates the floating-point value with single precision.

double

It indicates the floating-point value with double precision.

void

It shows the absence of a type.

Example

Below are some examples of the declaration of variables on Objective-C:

#include <stdio.h>

// Declaring the variable:
extern int x, y;
extern int z;
extern float ans;

int main () {
  // Defining the variable:
  int x,y;
  int z;
  float ans;
  /*Initializing the variable */
  x = 30;
  y = 6;
  z= x * y;
  printf("The answer is: %d \n", z);
  ans = 2.0/4.0;
  printf("The answer is: %f \n", ans);
  return 0;
}
Variable declaration in Objective-C

Explanation

  • Line 1: In Objective-C, we simultaneously define all variables of the same data type, separated by commas(,). In this line, we declare three variables of the same integer type: x, y, and z.
  • Line 2: We declare variable a of the char data type.
  • Line 3: We declare variable b of the float data type.
  • Line 4: We declare variable c of the double data type without any initialization.

We can use the assignment = operator in Objective-C to define or initialize the above variables.

Value assignment

Variable declaration and memory

Variable declaration helps inform the compiler about the existing variable. We use a variable as an alias for a memory location to make it understandable for humans. Compilers need the declaration of a variable while linking the program, and it has its meaning only at the compile time.

Memory in RAM

We can use the keyword extern to declare a variable at any place. We can declare the variable more than one time in the code. However, we can define it only one time in a function, file, and block of code.

Example

Here is an example of variable declaration and definition. In the following code, we define and initialize the variables locally and globally, i.e., inside or outside of the main() function.

#include <stdio.h>

// Declaring the variable:
extern int x, y;
extern int z;
extern float ans;

int main () {
  // Defining the variable:
  int x,y;
  int z;
  float ans;
  /*Initializing the variable */
  x = 30;
  y = 6;
  z= x * y;
  printf("The answer is: %d \n", z);
  ans = 2.0/4.0;
  printf("The answer is: %f \n", ans);
  return 0;
}
Local and global variables

Explanation

  • Lines 4 to 6: We declare extern variables of int and float type. The extern keyword in Objective-C helps us declare variables globally.
  • Lines 10 to 12: We define three variables of int and float types, x, y, and z, inside the main() method.
  • Lines 14 to 16: We initialize x=30, y=6, z=x*y, and print on the console.

Note: There are also some rules about the declaration of functions. According to these, we can specify the name of the function at the time of declaration, and it can be defined properly anywhere else.

Lvalue and Rvalue

There are two types of expressions in Objective-C:

  • lvalue: It is an expression that refers to the memory location. It may appear on either the left-hand or right-hand side of the assignment.
  • rvalue: It is the data value at some location or address of the memory.

Example

#include <stdio.h>
//Declaring the function
int checkStatus();

// Main function
int main() {
//Calling the function
  int status = checkStatus();
}

//Defining the function
int checkStatus() {
  printf("I love Educative!");
  return 1;
}
Value assignment

Explanation

  • Line 3: We declare the function to invoke the compiler about the existence of its definition.
  • Line 8: We inoke the checkStatus() method and return a rvalue to the variable status act as lvalue.
  • Line 12: We define the checkStatus() function.

Compound data types

Compound data types are those which can be constructed using primitive data types. Some compound data types in Objective-C are as follows:

  • Array or List
  • Pointer
  • Structure, etc.

Array/List

An array is a collection of unordered values of the same data types. The variable definition helps specify the location and size of the variable. It contains the type and alias to array type, followed by size in square brackets [,].


type arrayName[arraySize];

Note: We must use the correct primitive data type for the variable declaration. An array arrayName contains the consecutive memory locations of the same data types.

RELATED TAGS

variables
objective-c
objective c
communitycreator
RELATED COURSES

View all Courses

Keep Exploring