How to use variables in Objective-C
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:
- Variable names can contain digits, letters, and underscores.
- Variable names can start with a letter or an underscore symbol.
- Objective-C is a case-sensitive language. Therefore, upper and lower-case alphabets are considered distinct.
Parameters
Primitive data types
Type | Details |
| It is a datatype with the size of one byte or 8 bits. |
| It contains the common size of integers of either 2 or 4 bytes. |
| It indicates the floating-point value with single precision. |
| It indicates the floating-point value with double precision. |
| 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;}
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, andz. - Line 2: We declare variable
aof thechardata type. - Line 3: We declare variable
bof thefloatdata type. - Line 4: We declare variable
cof thedoubledata type without any initialization.
We can use the assignment = operator in Objective-C to define or initialize the above variables.
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.
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;}
Explanation
- Lines 4 to 6: We declare
externvariables ofintandfloattype. Theexternkeyword in Objective-C helps us declare variables globally. - Lines 10 to 12: We define three variables of
intandfloattypes,x,y, andz, inside themain()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 functionint checkStatus();// Main functionint main() {//Calling the functionint status = checkStatus();}//Defining the functionint checkStatus() {printf("I love Educative!");return 1;}
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 arvalueto the variable status act aslvalue. - 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:
ArrayorListPointerStructure, 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
arrayNamecontains the consecutive memory locations of the same data types.